@aigne/core 1.15.0 → 1.17.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/CHANGELOG.md +22 -0
- package/README.md +9 -7
- package/README.zh.md +9 -7
- package/lib/cjs/agents/agent.d.ts +168 -29
- package/lib/cjs/agents/agent.js +152 -56
- package/lib/cjs/agents/ai-agent.d.ts +7 -7
- package/lib/cjs/agents/ai-agent.js +19 -15
- package/lib/{esm/models → cjs/agents}/chat-model.d.ts +29 -19
- package/lib/cjs/{models → agents}/chat-model.js +56 -15
- package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
- package/lib/cjs/agents/guide-rail-agent.js +14 -0
- package/lib/cjs/agents/mcp-agent.d.ts +3 -4
- package/lib/cjs/agents/mcp-agent.js +11 -11
- package/lib/cjs/agents/team-agent.d.ts +7 -8
- package/lib/cjs/agents/team-agent.js +10 -10
- package/lib/cjs/agents/user-agent.d.ts +4 -4
- package/lib/cjs/agents/user-agent.js +10 -10
- package/lib/cjs/aigne/aigne.d.ts +13 -11
- package/lib/cjs/aigne/aigne.js +7 -6
- package/lib/cjs/aigne/context.d.ts +19 -8
- package/lib/cjs/aigne/context.js +27 -7
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/loader/agent-yaml.d.ts +2 -2
- package/lib/cjs/loader/index.d.ts +18 -11
- package/lib/cjs/loader/index.js +8 -27
- package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
- package/lib/cjs/memory/default-memory/index.d.ts +10 -0
- package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
- package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
- package/lib/cjs/memory/default-memory/storage.js +6 -0
- package/lib/cjs/memory/memory.d.ts +3 -2
- package/lib/cjs/memory/memory.js +1 -1
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +10 -4
- package/lib/cjs/prompt/prompt-builder.js +4 -4
- package/lib/cjs/prompt/template.d.ts +3 -3
- package/lib/cjs/utils/json-schema.js +1 -1
- package/lib/cjs/utils/logger.d.ts +33 -8
- package/lib/cjs/utils/logger.js +63 -5
- package/lib/cjs/utils/model-utils.d.ts +1 -1
- package/lib/cjs/utils/stream-utils.d.ts +3 -1
- package/lib/cjs/utils/stream-utils.js +31 -1
- package/lib/cjs/utils/type-utils.d.ts +7 -2
- package/lib/dts/agents/agent.d.ts +168 -29
- package/lib/dts/agents/ai-agent.d.ts +7 -7
- package/lib/{cjs/models → dts/agents}/chat-model.d.ts +29 -19
- package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
- package/lib/dts/agents/mcp-agent.d.ts +3 -4
- package/lib/dts/agents/team-agent.d.ts +7 -8
- package/lib/dts/agents/user-agent.d.ts +4 -4
- package/lib/dts/aigne/aigne.d.ts +13 -11
- package/lib/dts/aigne/context.d.ts +19 -8
- package/lib/dts/index.d.ts +2 -1
- package/lib/dts/loader/agent-yaml.d.ts +2 -2
- package/lib/dts/loader/index.d.ts +18 -11
- package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/dts/memory/default-memory/index.d.ts +10 -0
- package/lib/dts/memory/default-memory/storage.d.ts +13 -0
- package/lib/dts/memory/memory.d.ts +3 -2
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +10 -4
- package/lib/dts/prompt/template.d.ts +3 -3
- package/lib/dts/utils/logger.d.ts +33 -8
- package/lib/dts/utils/model-utils.d.ts +1 -1
- package/lib/dts/utils/stream-utils.d.ts +3 -1
- package/lib/dts/utils/type-utils.d.ts +7 -2
- package/lib/esm/agents/agent.d.ts +168 -29
- package/lib/esm/agents/agent.js +152 -56
- package/lib/esm/agents/ai-agent.d.ts +7 -7
- package/lib/esm/agents/ai-agent.js +18 -14
- package/lib/{dts/models → esm/agents}/chat-model.d.ts +29 -19
- package/lib/esm/{models → agents}/chat-model.js +56 -15
- package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
- package/lib/esm/agents/guide-rail-agent.js +11 -0
- package/lib/esm/agents/mcp-agent.d.ts +3 -4
- package/lib/esm/agents/mcp-agent.js +11 -11
- package/lib/esm/agents/team-agent.d.ts +7 -8
- package/lib/esm/agents/team-agent.js +10 -10
- package/lib/esm/agents/user-agent.d.ts +4 -4
- package/lib/esm/agents/user-agent.js +10 -10
- package/lib/esm/aigne/aigne.d.ts +13 -11
- package/lib/esm/aigne/aigne.js +7 -6
- package/lib/esm/aigne/context.d.ts +19 -8
- package/lib/esm/aigne/context.js +28 -8
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/loader/agent-yaml.d.ts +2 -2
- package/lib/esm/loader/index.d.ts +18 -11
- package/lib/esm/loader/index.js +8 -27
- package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
- package/lib/esm/memory/default-memory/index.d.ts +10 -0
- package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
- package/lib/esm/memory/default-memory/storage.d.ts +13 -0
- package/lib/esm/memory/default-memory/storage.js +2 -0
- package/lib/esm/memory/memory.d.ts +3 -2
- package/lib/esm/memory/memory.js +2 -2
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +10 -4
- package/lib/esm/prompt/prompt-builder.js +4 -4
- package/lib/esm/prompt/template.d.ts +3 -3
- package/lib/esm/utils/json-schema.js +1 -1
- package/lib/esm/utils/logger.d.ts +33 -8
- package/lib/esm/utils/logger.js +61 -4
- package/lib/esm/utils/model-utils.d.ts +1 -1
- package/lib/esm/utils/stream-utils.d.ts +3 -1
- package/lib/esm/utils/stream-utils.js +29 -1
- package/lib/esm/utils/type-utils.d.ts +7 -2
- package/package.json +4 -20
- package/lib/cjs/client/client.d.ts +0 -97
- package/lib/cjs/client/client.js +0 -87
- package/lib/cjs/client/index.d.ts +0 -1
- package/lib/cjs/client/index.js +0 -17
- package/lib/cjs/memory/default-memory.d.ts +0 -16
- package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
- package/lib/cjs/models/bedrock-chat-model.js +0 -303
- package/lib/cjs/models/claude-chat-model.d.ts +0 -114
- package/lib/cjs/models/claude-chat-model.js +0 -317
- package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
- package/lib/cjs/models/deepseek-chat-model.js +0 -35
- package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
- package/lib/cjs/models/gemini-chat-model.js +0 -35
- package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
- package/lib/cjs/models/ollama-chat-model.js +0 -34
- package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
- package/lib/cjs/models/open-router-chat-model.js +0 -34
- package/lib/cjs/models/openai-chat-model.d.ts +0 -166
- package/lib/cjs/models/openai-chat-model.js +0 -415
- package/lib/cjs/models/xai-chat-model.d.ts +0 -21
- package/lib/cjs/models/xai-chat-model.js +0 -33
- package/lib/cjs/server/error.d.ts +0 -15
- package/lib/cjs/server/error.js +0 -22
- package/lib/cjs/server/index.d.ts +0 -2
- package/lib/cjs/server/index.js +0 -18
- package/lib/cjs/server/server.d.ts +0 -135
- package/lib/cjs/server/server.js +0 -187
- package/lib/dts/client/client.d.ts +0 -97
- package/lib/dts/client/index.d.ts +0 -1
- package/lib/dts/memory/default-memory.d.ts +0 -16
- package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
- package/lib/dts/models/claude-chat-model.d.ts +0 -114
- package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
- package/lib/dts/models/gemini-chat-model.d.ts +0 -23
- package/lib/dts/models/ollama-chat-model.d.ts +0 -22
- package/lib/dts/models/open-router-chat-model.d.ts +0 -22
- package/lib/dts/models/openai-chat-model.d.ts +0 -166
- package/lib/dts/models/xai-chat-model.d.ts +0 -21
- package/lib/dts/server/error.d.ts +0 -15
- package/lib/dts/server/index.d.ts +0 -2
- package/lib/dts/server/server.d.ts +0 -135
- package/lib/esm/client/client.d.ts +0 -97
- package/lib/esm/client/client.js +0 -83
- package/lib/esm/client/index.d.ts +0 -1
- package/lib/esm/client/index.js +0 -1
- package/lib/esm/memory/default-memory.d.ts +0 -16
- package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
- package/lib/esm/models/bedrock-chat-model.js +0 -298
- package/lib/esm/models/claude-chat-model.d.ts +0 -114
- package/lib/esm/models/claude-chat-model.js +0 -310
- package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
- package/lib/esm/models/deepseek-chat-model.js +0 -31
- package/lib/esm/models/gemini-chat-model.d.ts +0 -23
- package/lib/esm/models/gemini-chat-model.js +0 -31
- package/lib/esm/models/ollama-chat-model.d.ts +0 -22
- package/lib/esm/models/ollama-chat-model.js +0 -30
- package/lib/esm/models/open-router-chat-model.d.ts +0 -22
- package/lib/esm/models/open-router-chat-model.js +0 -30
- package/lib/esm/models/openai-chat-model.d.ts +0 -166
- package/lib/esm/models/openai-chat-model.js +0 -405
- package/lib/esm/models/xai-chat-model.d.ts +0 -21
- package/lib/esm/models/xai-chat-model.js +0 -29
- package/lib/esm/server/error.d.ts +0 -15
- package/lib/esm/server/error.js +0 -18
- package/lib/esm/server/index.d.ts +0 -2
- package/lib/esm/server/index.js +0 -2
- package/lib/esm/server/server.d.ts +0 -135
- package/lib/esm/server/server.js +0 -180
package/lib/cjs/client/client.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Client module used to interact with the AIGNE framework.
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AIGNEClient = void 0;
|
|
7
|
-
const event_stream_js_1 = require("../utils/event-stream.js");
|
|
8
|
-
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
9
|
-
/**
|
|
10
|
-
* Client for interacting with a remote AIGNE server.
|
|
11
|
-
* AIGNEClient provides a client-side interface that matches the AIGNE API,
|
|
12
|
-
* allowing applications to invoke agents and receive responses from a remote AIGNE instance.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* Here's a simple example of how to use AIGNEClient:
|
|
16
|
-
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* Here's an example of how to use AIGNEClient with streaming response:
|
|
20
|
-
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
|
|
21
|
-
*/
|
|
22
|
-
class AIGNEClient {
|
|
23
|
-
options;
|
|
24
|
-
/**
|
|
25
|
-
* Creates a new AIGNEClient instance.
|
|
26
|
-
*
|
|
27
|
-
* @param options - Configuration options for connecting to the AIGNE server
|
|
28
|
-
*/
|
|
29
|
-
constructor(options) {
|
|
30
|
-
this.options = options;
|
|
31
|
-
}
|
|
32
|
-
async invoke(agent, input, options) {
|
|
33
|
-
// Send the agent invocation request to the AIGNE server
|
|
34
|
-
const response = await this.fetch(this.options.url, {
|
|
35
|
-
...options?.fetchOptions,
|
|
36
|
-
method: "POST",
|
|
37
|
-
headers: {
|
|
38
|
-
"Content-Type": "application/json",
|
|
39
|
-
...options?.fetchOptions?.headers,
|
|
40
|
-
},
|
|
41
|
-
body: JSON.stringify({ agent, input, options }),
|
|
42
|
-
});
|
|
43
|
-
// For non-streaming responses, simply parse the JSON response and return it
|
|
44
|
-
if (!options?.streaming) {
|
|
45
|
-
return await response.json();
|
|
46
|
-
}
|
|
47
|
-
// For streaming responses, set up the streaming pipeline
|
|
48
|
-
const stream = response.body;
|
|
49
|
-
if (!stream)
|
|
50
|
-
throw new Error("Response body is not a stream");
|
|
51
|
-
// Process the stream through a series of transforms:
|
|
52
|
-
// 1. Convert bytes to text
|
|
53
|
-
// 2. Parse SSE format into structured events
|
|
54
|
-
// 3. Convert events into a standardized agent response stream
|
|
55
|
-
return stream
|
|
56
|
-
.pipeThrough(new TextDecoderStream())
|
|
57
|
-
.pipeThrough(new event_stream_js_1.EventStreamParser())
|
|
58
|
-
.pipeThrough(new event_stream_js_1.AgentResponseStreamParser());
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Enhanced fetch method that handles error responses from the AIGNE server.
|
|
62
|
-
* This method wraps the standard fetch API to provide better error handling and reporting.
|
|
63
|
-
*
|
|
64
|
-
* @param args - Standard fetch API arguments (url and options)
|
|
65
|
-
* @returns A Response object if the request was successful
|
|
66
|
-
* @throws Error with detailed information if the request failed
|
|
67
|
-
*
|
|
68
|
-
* @private
|
|
69
|
-
*/
|
|
70
|
-
async fetch(...args) {
|
|
71
|
-
const result = await globalThis.fetch(...args);
|
|
72
|
-
if (!result.ok) {
|
|
73
|
-
let message;
|
|
74
|
-
try {
|
|
75
|
-
const text = await result.text();
|
|
76
|
-
const json = (0, type_utils_js_1.tryOrThrow)(() => JSON.parse(text));
|
|
77
|
-
message = json?.error?.message || text;
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
// ignore
|
|
81
|
-
}
|
|
82
|
-
throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
exports.AIGNEClient = AIGNEClient;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./client.js";
|
package/lib/cjs/client/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./client.js"), exports);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { type Memory, MemoryAgent, type MemoryAgentOptions } from "./memory.js";
|
|
2
|
-
export declare const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
3
|
-
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
4
|
-
}
|
|
5
|
-
export declare class DefaultMemory extends MemoryAgent {
|
|
6
|
-
constructor(options?: DefaultMemoryOptions);
|
|
7
|
-
storage: Memory[];
|
|
8
|
-
search(options?: {
|
|
9
|
-
limit?: number;
|
|
10
|
-
}): Promise<{
|
|
11
|
-
result: Memory[];
|
|
12
|
-
}>;
|
|
13
|
-
create(memory: Pick<Memory, "content">): Promise<{
|
|
14
|
-
result: Memory;
|
|
15
|
-
}>;
|
|
16
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import type { AgentResponse } from "../agents/agent.js";
|
|
4
|
-
import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
|
|
5
|
-
/**
|
|
6
|
-
* @hidden
|
|
7
|
-
*/
|
|
8
|
-
export declare function extractLastJsonObject(text: string): string | null;
|
|
9
|
-
export interface BedrockChatModelOptions {
|
|
10
|
-
accessKeyId?: string;
|
|
11
|
-
secretAccessKey?: string;
|
|
12
|
-
region?: string;
|
|
13
|
-
model?: string;
|
|
14
|
-
modelOptions?: ChatModelOptions;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* @hidden
|
|
18
|
-
*/
|
|
19
|
-
export declare const bedrockChatModelOptionsSchema: z.ZodObject<{
|
|
20
|
-
region: z.ZodOptional<z.ZodString>;
|
|
21
|
-
model: z.ZodOptional<z.ZodString>;
|
|
22
|
-
modelOptions: z.ZodOptional<z.ZodObject<{
|
|
23
|
-
model: z.ZodOptional<z.ZodString>;
|
|
24
|
-
temperature: z.ZodOptional<z.ZodNumber>;
|
|
25
|
-
topP: z.ZodOptional<z.ZodNumber>;
|
|
26
|
-
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
27
|
-
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
28
|
-
parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
29
|
-
}, "strip", z.ZodTypeAny, {
|
|
30
|
-
parallelToolCalls: boolean;
|
|
31
|
-
model?: string | undefined;
|
|
32
|
-
temperature?: number | undefined;
|
|
33
|
-
topP?: number | undefined;
|
|
34
|
-
frequencyPenalty?: number | undefined;
|
|
35
|
-
presencePenalty?: number | undefined;
|
|
36
|
-
}, {
|
|
37
|
-
model?: string | undefined;
|
|
38
|
-
temperature?: number | undefined;
|
|
39
|
-
topP?: number | undefined;
|
|
40
|
-
frequencyPenalty?: number | undefined;
|
|
41
|
-
presencePenalty?: number | undefined;
|
|
42
|
-
parallelToolCalls?: boolean | undefined;
|
|
43
|
-
}>>;
|
|
44
|
-
}, "strip", z.ZodTypeAny, {
|
|
45
|
-
modelOptions?: {
|
|
46
|
-
parallelToolCalls: boolean;
|
|
47
|
-
model?: string | undefined;
|
|
48
|
-
temperature?: number | undefined;
|
|
49
|
-
topP?: number | undefined;
|
|
50
|
-
frequencyPenalty?: number | undefined;
|
|
51
|
-
presencePenalty?: number | undefined;
|
|
52
|
-
} | undefined;
|
|
53
|
-
model?: string | undefined;
|
|
54
|
-
region?: string | undefined;
|
|
55
|
-
}, {
|
|
56
|
-
modelOptions?: {
|
|
57
|
-
model?: string | undefined;
|
|
58
|
-
temperature?: number | undefined;
|
|
59
|
-
topP?: number | undefined;
|
|
60
|
-
frequencyPenalty?: number | undefined;
|
|
61
|
-
presencePenalty?: number | undefined;
|
|
62
|
-
parallelToolCalls?: boolean | undefined;
|
|
63
|
-
} | undefined;
|
|
64
|
-
model?: string | undefined;
|
|
65
|
-
region?: string | undefined;
|
|
66
|
-
}>;
|
|
67
|
-
export declare class BedrockChatModel extends ChatModel {
|
|
68
|
-
options?: BedrockChatModelOptions | undefined;
|
|
69
|
-
constructor(options?: BedrockChatModelOptions | undefined);
|
|
70
|
-
/**
|
|
71
|
-
* @hidden
|
|
72
|
-
*/
|
|
73
|
-
protected _client?: BedrockRuntimeClient;
|
|
74
|
-
get client(): BedrockRuntimeClient;
|
|
75
|
-
get modelOptions(): ChatModelOptions | undefined;
|
|
76
|
-
process(input: ChatModelInput): Promise<AgentResponse<ChatModelOutput>>;
|
|
77
|
-
private extractResultFromStream;
|
|
78
|
-
private requestStructuredOutput;
|
|
79
|
-
}
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BedrockChatModel = exports.bedrockChatModelOptionsSchema = void 0;
|
|
4
|
-
exports.extractLastJsonObject = extractLastJsonObject;
|
|
5
|
-
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
|
|
6
|
-
const nanoid_1 = require("nanoid");
|
|
7
|
-
const zod_1 = require("zod");
|
|
8
|
-
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
9
|
-
const model_utils_js_1 = require("../utils/model-utils.js");
|
|
10
|
-
const prompts_js_1 = require("../utils/prompts.js");
|
|
11
|
-
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
12
|
-
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
13
|
-
const chat_model_js_1 = require("./chat-model.js");
|
|
14
|
-
/**
|
|
15
|
-
* @hidden
|
|
16
|
-
*/
|
|
17
|
-
function extractLastJsonObject(text) {
|
|
18
|
-
return text.replace(/<thinking>[\s\S]*?<\/thinking>/g, "").trim();
|
|
19
|
-
}
|
|
20
|
-
const BEDROCK_DEFAULT_CHAT_MODEL = "us.amazon.nova-lite-v1:0";
|
|
21
|
-
/**
|
|
22
|
-
* @hidden
|
|
23
|
-
*/
|
|
24
|
-
exports.bedrockChatModelOptionsSchema = zod_1.z.object({
|
|
25
|
-
region: zod_1.z.string().optional(),
|
|
26
|
-
model: zod_1.z.string().optional(),
|
|
27
|
-
modelOptions: zod_1.z
|
|
28
|
-
.object({
|
|
29
|
-
model: zod_1.z.string().optional(),
|
|
30
|
-
temperature: zod_1.z.number().optional(),
|
|
31
|
-
topP: zod_1.z.number().optional(),
|
|
32
|
-
frequencyPenalty: zod_1.z.number().optional(),
|
|
33
|
-
presencePenalty: zod_1.z.number().optional(),
|
|
34
|
-
parallelToolCalls: zod_1.z.boolean().optional().default(true),
|
|
35
|
-
})
|
|
36
|
-
.optional(),
|
|
37
|
-
});
|
|
38
|
-
class BedrockChatModel extends chat_model_js_1.ChatModel {
|
|
39
|
-
options;
|
|
40
|
-
constructor(options) {
|
|
41
|
-
if (options)
|
|
42
|
-
(0, type_utils_js_1.checkArguments)("BedrockChatModel", exports.bedrockChatModelOptionsSchema, options);
|
|
43
|
-
super();
|
|
44
|
-
this.options = options;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* @hidden
|
|
48
|
-
*/
|
|
49
|
-
_client;
|
|
50
|
-
get client() {
|
|
51
|
-
const credentials = this.options?.accessKeyId && this.options?.secretAccessKey
|
|
52
|
-
? {
|
|
53
|
-
accessKeyId: this.options.accessKeyId,
|
|
54
|
-
secretAccessKey: this.options.secretAccessKey,
|
|
55
|
-
}
|
|
56
|
-
: undefined;
|
|
57
|
-
this._client ??= new client_bedrock_runtime_1.BedrockRuntimeClient({
|
|
58
|
-
region: this.options?.region,
|
|
59
|
-
credentials,
|
|
60
|
-
});
|
|
61
|
-
return this._client;
|
|
62
|
-
}
|
|
63
|
-
get modelOptions() {
|
|
64
|
-
return this.options?.modelOptions;
|
|
65
|
-
}
|
|
66
|
-
async process(input) {
|
|
67
|
-
const modelId = input.modelOptions?.model ?? this.modelOptions?.model ?? BEDROCK_DEFAULT_CHAT_MODEL;
|
|
68
|
-
const { messages, system } = getRunMessages(input);
|
|
69
|
-
const toolConfig = convertTools(input);
|
|
70
|
-
const body = {
|
|
71
|
-
modelId,
|
|
72
|
-
messages,
|
|
73
|
-
system,
|
|
74
|
-
toolConfig,
|
|
75
|
-
inferenceConfig: {
|
|
76
|
-
temperature: input.modelOptions?.temperature ?? this.modelOptions?.temperature,
|
|
77
|
-
topP: input.modelOptions?.topP ?? this.modelOptions?.topP,
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
const command = new client_bedrock_runtime_1.ConverseStreamCommand(body);
|
|
81
|
-
const response = await this.client.send(command);
|
|
82
|
-
const jsonMode = input.responseFormat?.type === "json_schema";
|
|
83
|
-
if (!jsonMode) {
|
|
84
|
-
return this.extractResultFromStream(response.stream, modelId, true);
|
|
85
|
-
}
|
|
86
|
-
const result = await this.extractResultFromStream(response.stream, modelId, false);
|
|
87
|
-
if (!result.toolCalls?.length && jsonMode && result.text) {
|
|
88
|
-
const output = await this.requestStructuredOutput(body, input.responseFormat);
|
|
89
|
-
return { ...output, usage: (0, model_utils_js_1.mergeUsage)(result.usage, output.usage) };
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
async extractResultFromStream(stream, modelId, streaming) {
|
|
94
|
-
if (!stream)
|
|
95
|
-
throw new Error("Unable to get AI model response.");
|
|
96
|
-
const result = new ReadableStream({
|
|
97
|
-
start: async (controller) => {
|
|
98
|
-
try {
|
|
99
|
-
controller.enqueue({ delta: { json: { model: modelId } } });
|
|
100
|
-
const toolCalls = [];
|
|
101
|
-
let usage;
|
|
102
|
-
for await (const chunk of stream) {
|
|
103
|
-
if (chunk.contentBlockStart?.start?.toolUse) {
|
|
104
|
-
const toolUse = chunk.contentBlockStart.start.toolUse;
|
|
105
|
-
if (!toolUse.name)
|
|
106
|
-
throw new Error("Tool use is invalid");
|
|
107
|
-
if (chunk.contentBlockStart.contentBlockIndex === undefined)
|
|
108
|
-
throw new Error("Tool use content block index is required");
|
|
109
|
-
toolCalls[chunk.contentBlockStart.contentBlockIndex] = {
|
|
110
|
-
type: "function",
|
|
111
|
-
id: toolUse.toolUseId || (0, nanoid_1.nanoid)(),
|
|
112
|
-
function: {
|
|
113
|
-
name: toolUse.name,
|
|
114
|
-
arguments: {},
|
|
115
|
-
},
|
|
116
|
-
args: "",
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
if (chunk.contentBlockDelta) {
|
|
120
|
-
const block = chunk.contentBlockDelta;
|
|
121
|
-
const delta = block.delta;
|
|
122
|
-
if (delta?.text) {
|
|
123
|
-
controller.enqueue({ delta: { text: { text: delta.text } } });
|
|
124
|
-
}
|
|
125
|
-
if (delta?.toolUse) {
|
|
126
|
-
if (block.contentBlockIndex === undefined)
|
|
127
|
-
throw new Error("Content block index is required");
|
|
128
|
-
const call = toolCalls[block.contentBlockIndex];
|
|
129
|
-
if (!call)
|
|
130
|
-
throw new Error("Tool call not found");
|
|
131
|
-
call.args += delta.toolUse.input;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (chunk.metadata) {
|
|
135
|
-
usage = chunk.metadata.usage;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (toolCalls.length) {
|
|
139
|
-
controller.enqueue({
|
|
140
|
-
delta: {
|
|
141
|
-
json: {
|
|
142
|
-
toolCalls: toolCalls
|
|
143
|
-
.map(({ args, ...c }) => ({
|
|
144
|
-
...c,
|
|
145
|
-
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
146
|
-
}))
|
|
147
|
-
.filter(type_utils_js_1.isNonNullable),
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
controller.enqueue({ delta: { json: { usage } } });
|
|
153
|
-
controller.close();
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
controller.error(error);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
161
|
-
}
|
|
162
|
-
async requestStructuredOutput(body, responseFormat) {
|
|
163
|
-
if (responseFormat?.type !== "json_schema") {
|
|
164
|
-
throw new Error("Expected json_schema response format");
|
|
165
|
-
}
|
|
166
|
-
const system = [
|
|
167
|
-
...(body.system ?? []),
|
|
168
|
-
{
|
|
169
|
-
text: `Use the generate_json tool to generate a json result. ${(0, prompts_js_1.getJsonToolInputPrompt)(responseFormat.jsonSchema.schema)}`,
|
|
170
|
-
},
|
|
171
|
-
];
|
|
172
|
-
const toolConfig = {
|
|
173
|
-
tools: [
|
|
174
|
-
{
|
|
175
|
-
toolSpec: {
|
|
176
|
-
name: "generate_json",
|
|
177
|
-
description: "Generate a json result by given context",
|
|
178
|
-
inputSchema: { json: responseFormat.jsonSchema.schema },
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
toolChoice: { tool: { name: "generate_json" } },
|
|
183
|
-
};
|
|
184
|
-
const command = new client_bedrock_runtime_1.ConverseCommand({ ...body, system, toolConfig });
|
|
185
|
-
const response = await this.client.send(command);
|
|
186
|
-
const jsonTool = response.output?.message?.content?.find((i) => i.toolUse?.name === "generate_json");
|
|
187
|
-
if (!jsonTool)
|
|
188
|
-
throw new Error("Json tool not found");
|
|
189
|
-
return {
|
|
190
|
-
json: jsonTool.toolUse?.input,
|
|
191
|
-
model: body.modelId,
|
|
192
|
-
usage: response.usage,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
exports.BedrockChatModel = BedrockChatModel;
|
|
197
|
-
const getRunMessages = ({ messages: msgs, }) => {
|
|
198
|
-
const system = [];
|
|
199
|
-
const messages = [];
|
|
200
|
-
for (const msg of msgs) {
|
|
201
|
-
if (msg.role === "system") {
|
|
202
|
-
if (typeof msg.content !== "string")
|
|
203
|
-
throw new Error("System message must have content");
|
|
204
|
-
system.push({ text: msg.content });
|
|
205
|
-
}
|
|
206
|
-
else if (msg.role === "tool") {
|
|
207
|
-
if (!msg.toolCallId)
|
|
208
|
-
throw new Error("Tool message must have toolCallId");
|
|
209
|
-
if (typeof msg.content !== "string")
|
|
210
|
-
throw new Error("Tool message must have string content");
|
|
211
|
-
if (messages.at(-1)?.role === "user") {
|
|
212
|
-
messages.at(-1)?.content?.push({
|
|
213
|
-
toolResult: { toolUseId: msg.toolCallId, content: [{ json: (0, json_schema_js_1.parseJSON)(msg.content) }] },
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
messages.push({
|
|
218
|
-
role: "user",
|
|
219
|
-
content: [
|
|
220
|
-
{
|
|
221
|
-
toolResult: {
|
|
222
|
-
toolUseId: msg.toolCallId,
|
|
223
|
-
content: [{ json: (0, json_schema_js_1.parseJSON)(msg.content) }],
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
],
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
else if (msg.role === "user") {
|
|
231
|
-
if (!msg.content)
|
|
232
|
-
throw new Error("User message must have content");
|
|
233
|
-
messages.push({ role: "user", content: convertContent(msg.content) });
|
|
234
|
-
}
|
|
235
|
-
else if (msg.role === "agent") {
|
|
236
|
-
if (msg.toolCalls?.length) {
|
|
237
|
-
messages.push({
|
|
238
|
-
role: "assistant",
|
|
239
|
-
content: msg.toolCalls.map((i) => ({
|
|
240
|
-
toolUse: {
|
|
241
|
-
toolUseId: i.id,
|
|
242
|
-
name: i.function.name,
|
|
243
|
-
input: i.function.arguments,
|
|
244
|
-
},
|
|
245
|
-
})),
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
else if (msg.content) {
|
|
249
|
-
messages.push({ role: "assistant", content: convertContent(msg.content) });
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
throw new Error("Agent message must have content or toolCalls");
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
if (messages.at(0)?.role !== "user") {
|
|
257
|
-
messages.unshift({ role: "user", content: [{ text: "." }] });
|
|
258
|
-
}
|
|
259
|
-
return { messages, system };
|
|
260
|
-
};
|
|
261
|
-
function convertContent(content) {
|
|
262
|
-
if (typeof content === "string")
|
|
263
|
-
return [{ text: content }];
|
|
264
|
-
if (Array.isArray(content)) {
|
|
265
|
-
const blocks = [];
|
|
266
|
-
for (const item of content) {
|
|
267
|
-
if (item.type === "text")
|
|
268
|
-
blocks.push({ text: item.text });
|
|
269
|
-
}
|
|
270
|
-
return blocks;
|
|
271
|
-
}
|
|
272
|
-
throw new Error("Invalid chat message content");
|
|
273
|
-
}
|
|
274
|
-
function convertTools({ tools, toolChoice }) {
|
|
275
|
-
if (!tools?.length || toolChoice === "none")
|
|
276
|
-
return undefined;
|
|
277
|
-
let choice;
|
|
278
|
-
if (typeof toolChoice === "object" && "type" in toolChoice && toolChoice.type === "function") {
|
|
279
|
-
choice = { tool: { name: toolChoice.function.name } };
|
|
280
|
-
}
|
|
281
|
-
else if (toolChoice === "required") {
|
|
282
|
-
choice = { any: {} };
|
|
283
|
-
}
|
|
284
|
-
else if (toolChoice === "auto") {
|
|
285
|
-
choice = { auto: {} };
|
|
286
|
-
}
|
|
287
|
-
return {
|
|
288
|
-
tools: tools.map((i) => {
|
|
289
|
-
const parameters = i.function.parameters;
|
|
290
|
-
if (Object.keys(parameters).length === 0) {
|
|
291
|
-
parameters.type = "object";
|
|
292
|
-
}
|
|
293
|
-
return {
|
|
294
|
-
toolSpec: {
|
|
295
|
-
name: i.function.name,
|
|
296
|
-
description: i.function.description,
|
|
297
|
-
inputSchema: { json: parameters },
|
|
298
|
-
},
|
|
299
|
-
};
|
|
300
|
-
}),
|
|
301
|
-
toolChoice: choice,
|
|
302
|
-
};
|
|
303
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import type { AgentProcessResult } from "../agents/agent.js";
|
|
4
|
-
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
|
-
import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
|
|
6
|
-
/**
|
|
7
|
-
* Configuration options for Claude Chat Model
|
|
8
|
-
*/
|
|
9
|
-
export interface ClaudeChatModelOptions {
|
|
10
|
-
/**
|
|
11
|
-
* API key for Anthropic's Claude API
|
|
12
|
-
*
|
|
13
|
-
* If not provided, will look for ANTHROPIC_API_KEY or CLAUDE_API_KEY in environment variables
|
|
14
|
-
*/
|
|
15
|
-
apiKey?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Claude model to use
|
|
18
|
-
*
|
|
19
|
-
* Defaults to 'claude-3-7-sonnet-latest'
|
|
20
|
-
*/
|
|
21
|
-
model?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Additional model options to control behavior
|
|
24
|
-
*/
|
|
25
|
-
modelOptions?: ChatModelOptions;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @hidden
|
|
29
|
-
*/
|
|
30
|
-
export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
31
|
-
apiKey: z.ZodOptional<z.ZodString>;
|
|
32
|
-
model: z.ZodOptional<z.ZodString>;
|
|
33
|
-
modelOptions: z.ZodOptional<z.ZodObject<{
|
|
34
|
-
model: z.ZodOptional<z.ZodString>;
|
|
35
|
-
temperature: z.ZodOptional<z.ZodNumber>;
|
|
36
|
-
topP: z.ZodOptional<z.ZodNumber>;
|
|
37
|
-
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
38
|
-
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
39
|
-
parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
40
|
-
}, "strip", z.ZodTypeAny, {
|
|
41
|
-
parallelToolCalls: boolean;
|
|
42
|
-
model?: string | undefined;
|
|
43
|
-
temperature?: number | undefined;
|
|
44
|
-
topP?: number | undefined;
|
|
45
|
-
frequencyPenalty?: number | undefined;
|
|
46
|
-
presencePenalty?: number | undefined;
|
|
47
|
-
}, {
|
|
48
|
-
model?: string | undefined;
|
|
49
|
-
temperature?: number | undefined;
|
|
50
|
-
topP?: number | undefined;
|
|
51
|
-
frequencyPenalty?: number | undefined;
|
|
52
|
-
presencePenalty?: number | undefined;
|
|
53
|
-
parallelToolCalls?: boolean | undefined;
|
|
54
|
-
}>>;
|
|
55
|
-
}, "strip", z.ZodTypeAny, {
|
|
56
|
-
modelOptions?: {
|
|
57
|
-
parallelToolCalls: boolean;
|
|
58
|
-
model?: string | undefined;
|
|
59
|
-
temperature?: number | undefined;
|
|
60
|
-
topP?: number | undefined;
|
|
61
|
-
frequencyPenalty?: number | undefined;
|
|
62
|
-
presencePenalty?: number | undefined;
|
|
63
|
-
} | undefined;
|
|
64
|
-
model?: string | undefined;
|
|
65
|
-
apiKey?: string | undefined;
|
|
66
|
-
}, {
|
|
67
|
-
modelOptions?: {
|
|
68
|
-
model?: string | undefined;
|
|
69
|
-
temperature?: number | undefined;
|
|
70
|
-
topP?: number | undefined;
|
|
71
|
-
frequencyPenalty?: number | undefined;
|
|
72
|
-
presencePenalty?: number | undefined;
|
|
73
|
-
parallelToolCalls?: boolean | undefined;
|
|
74
|
-
} | undefined;
|
|
75
|
-
model?: string | undefined;
|
|
76
|
-
apiKey?: string | undefined;
|
|
77
|
-
}>;
|
|
78
|
-
/**
|
|
79
|
-
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
80
|
-
*
|
|
81
|
-
* This model provides access to Claude's capabilities including:
|
|
82
|
-
* - Text generation
|
|
83
|
-
* - Tool use
|
|
84
|
-
* - JSON structured output
|
|
85
|
-
*
|
|
86
|
-
* Default model: 'claude-3-7-sonnet-latest'
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* Here's how to create and use a Claude chat model:
|
|
90
|
-
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model}
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* Here's an example with streaming response:
|
|
94
|
-
* {@includeCode ../../test/models/claude-chat-model.test.ts#example-claude-chat-model-streaming-async-generator}
|
|
95
|
-
*/
|
|
96
|
-
export declare class ClaudeChatModel extends ChatModel {
|
|
97
|
-
options?: ClaudeChatModelOptions | undefined;
|
|
98
|
-
constructor(options?: ClaudeChatModelOptions | undefined);
|
|
99
|
-
/**
|
|
100
|
-
* @hidden
|
|
101
|
-
*/
|
|
102
|
-
protected _client?: Anthropic;
|
|
103
|
-
get client(): Anthropic;
|
|
104
|
-
get modelOptions(): ChatModelOptions | undefined;
|
|
105
|
-
/**
|
|
106
|
-
* Process the input using Claude's chat model
|
|
107
|
-
* @param input - The input to process
|
|
108
|
-
* @returns The processed output from the model
|
|
109
|
-
*/
|
|
110
|
-
process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
111
|
-
private _process;
|
|
112
|
-
private extractResultFromClaudeStream;
|
|
113
|
-
private requestStructuredOutput;
|
|
114
|
-
}
|