@vectorx/agent-runtime 0.0.0-beta-20251112071515
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/agent-runner.d.ts +14 -0
- package/lib/agent-runner.js +123 -0
- package/lib/agent-types.d.ts +118 -0
- package/lib/agent-types.js +2 -0
- package/lib/agent.d.ts +40 -0
- package/lib/agent.js +785 -0
- package/lib/codes.d.ts +6 -0
- package/lib/codes.js +9 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +23 -0
- package/lib/schema/conversation.schema.d.ts +126 -0
- package/lib/schema/conversation.schema.js +35 -0
- package/lib/schema/file.schema.d.ts +18 -0
- package/lib/schema/file.schema.js +10 -0
- package/lib/schema/index.d.ts +6 -0
- package/lib/schema/index.js +22 -0
- package/lib/schema/knowledge.schema.d.ts +37 -0
- package/lib/schema/knowledge.schema.js +13 -0
- package/lib/schema/memory.schema.d.ts +35 -0
- package/lib/schema/memory.schema.js +15 -0
- package/lib/schema/message.schema.d.ts +1050 -0
- package/lib/schema/message.schema.js +79 -0
- package/lib/schema/task.schema.d.ts +9 -0
- package/lib/schema/task.schema.js +7 -0
- package/lib/sse-sender.d.ts +32 -0
- package/lib/sse-sender.js +33 -0
- package/lib/types.d.ts +71 -0
- package/lib/types.js +2 -0
- package/lib/utils/agent-helper.d.ts +9 -0
- package/lib/utils/agent-helper.js +111 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +17 -0
- package/lib/utils/integration-response.d.ts +20 -0
- package/lib/utils/integration-response.js +17 -0
- package/package.json +47 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IAgent } from "@vectorx/ai-sdk";
|
|
2
|
+
import type { RcbContext } from "@vectorx/functions-framework";
|
|
3
|
+
export declare class AgentDriver {
|
|
4
|
+
readonly agent: IAgent;
|
|
5
|
+
static run(event: unknown, context: RcbContext, agent: IAgent): Promise<unknown>;
|
|
6
|
+
constructor(agent: IAgent);
|
|
7
|
+
run(event: unknown, context: RcbContext): Promise<unknown>;
|
|
8
|
+
private sendMessageFunc;
|
|
9
|
+
private getHistoryMessagesFunc;
|
|
10
|
+
private getAgentInfoFunc;
|
|
11
|
+
private getConversationsFunc;
|
|
12
|
+
private queryTasksFunc;
|
|
13
|
+
private uploadFileFunc;
|
|
14
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AgentDriver = void 0;
|
|
13
|
+
const codes_1 = require("./codes");
|
|
14
|
+
const schema_1 = require("./schema");
|
|
15
|
+
const agent_helper_1 = require("./utils/agent-helper");
|
|
16
|
+
const integration_response_1 = require("./utils/integration-response");
|
|
17
|
+
class AgentDriver {
|
|
18
|
+
static run(event, context, agent) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
return new AgentDriver(agent).run(event, context);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
constructor(agent) {
|
|
24
|
+
this.agent = agent;
|
|
25
|
+
}
|
|
26
|
+
run(event, context) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
if (!context.httpContext) {
|
|
29
|
+
return (0, integration_response_1.createIntegrationResponse)(400, codes_1.CODES.INVALID_HTTP_CONTEXT, "Invalid HTTP context");
|
|
30
|
+
}
|
|
31
|
+
const apiFuncs = {
|
|
32
|
+
"POST:send-message": this.sendMessageFunc.bind(this),
|
|
33
|
+
"POST:upload-openai-file": this.uploadFileFunc.bind(this),
|
|
34
|
+
"GET:query-tasks": this.queryTasksFunc.bind(this),
|
|
35
|
+
"GET:messages": this.getHistoryMessagesFunc.bind(this),
|
|
36
|
+
"GET:info": this.getAgentInfoFunc.bind(this),
|
|
37
|
+
"GET:conversations": this.getConversationsFunc.bind(this),
|
|
38
|
+
"GET:health": () => __awaiter(this, void 0, void 0, function* () { return ({ message: integration_response_1.OK_MESSAGE, timestamp: new Date().toISOString() }); }),
|
|
39
|
+
};
|
|
40
|
+
const httpMethod = context.httpContext.httpMethod;
|
|
41
|
+
const apiName = (0, agent_helper_1.parseApiName)(context.httpContext.url);
|
|
42
|
+
if (!apiName) {
|
|
43
|
+
return (0, integration_response_1.createIntegrationResponse)(404, codes_1.CODES.AGENT_API_NOT_FOUND, `Invalid url : ${context.httpContext.url}`);
|
|
44
|
+
}
|
|
45
|
+
const apiFunc = apiFuncs[`${httpMethod}:${apiName}`];
|
|
46
|
+
if (apiFunc) {
|
|
47
|
+
return apiFunc(event, context);
|
|
48
|
+
}
|
|
49
|
+
return (0, integration_response_1.createIntegrationResponse)(404, codes_1.CODES.AGENT_API_NOT_FOUND, `Agent API ${httpMethod}:${apiName} has not been supported`);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
sendMessageFunc(event, context) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
try {
|
|
55
|
+
const parsedEvent = schema_1.sendMessageInputSchema.passthrough().parse(event);
|
|
56
|
+
return yield this.agent.sendMessage(parsedEvent);
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'sendMessage' failed, message: ${e}`);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
getHistoryMessagesFunc(event, context) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
try {
|
|
66
|
+
const query = (0, agent_helper_1.parseQueryFromCtx)(context);
|
|
67
|
+
const parsedEvent = schema_1.getHistoryMessagesParamsSchema.passthrough().parse(query);
|
|
68
|
+
return yield this.agent.getHistoryMessages(parsedEvent);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getHistoryMessages' failed, message: ${e}`);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
getAgentInfoFunc() {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
try {
|
|
78
|
+
return yield this.agent.getAgentInfo();
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getAgentInfo' failed, message: ${e}`);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
getConversationsFunc() {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
try {
|
|
88
|
+
return yield this.agent.getConversations();
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getConversations' failed, message: ${e}`);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
queryTasksFunc(event, context) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
var _a, _b;
|
|
98
|
+
try {
|
|
99
|
+
const pathTaskId = (0, agent_helper_1.parseTaskIdFromPath)(context.httpContext.url);
|
|
100
|
+
const query = (0, agent_helper_1.parseQueryFromCtx)(context);
|
|
101
|
+
const parsed = schema_1.queryTasksParamsSchema.passthrough().parse({
|
|
102
|
+
task_id: query.task_id || pathTaskId,
|
|
103
|
+
});
|
|
104
|
+
return yield ((_b = (_a = this.agent).queryTasks) === null || _b === void 0 ? void 0 : _b.call(_a, parsed.task_id));
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'queryTasks' failed, message: ${e}`);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
uploadFileFunc(event, context) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
var _a, _b;
|
|
114
|
+
try {
|
|
115
|
+
return yield ((_b = (_a = this.agent).uploadFile) === null || _b === void 0 ? void 0 : _b.call(_a, event));
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'uploadFile' failed, message: ${e}`);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.AgentDriver = AgentDriver;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { MessageType } from "@vectorx/ai-sdk";
|
|
2
|
+
import type { BaseResponse } from "./utils/integration-response";
|
|
3
|
+
export interface UserRecord {
|
|
4
|
+
content: string;
|
|
5
|
+
role: "user";
|
|
6
|
+
}
|
|
7
|
+
export interface AssistantRecord {
|
|
8
|
+
content: string;
|
|
9
|
+
role: "assistant";
|
|
10
|
+
reasoningContent: string;
|
|
11
|
+
type: MessageType;
|
|
12
|
+
}
|
|
13
|
+
export type CreateRecordPairResponse = BaseResponse;
|
|
14
|
+
export interface ParticipantInfo {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
desc: string;
|
|
18
|
+
avatar_url: string;
|
|
19
|
+
}
|
|
20
|
+
export interface GetHistoryMessagesData {
|
|
21
|
+
message_list: (UserRecord | AssistantRecord)[];
|
|
22
|
+
conversation_id: string;
|
|
23
|
+
participant_info_map: Record<string, ParticipantInfo>;
|
|
24
|
+
next_cursor: number;
|
|
25
|
+
has_more: boolean;
|
|
26
|
+
}
|
|
27
|
+
export type GetHistoryMessagesResponse = BaseResponse<GetHistoryMessagesData>;
|
|
28
|
+
export interface Conversation {
|
|
29
|
+
id: string;
|
|
30
|
+
createAt: string;
|
|
31
|
+
}
|
|
32
|
+
export interface GetConversationsData {
|
|
33
|
+
conversations: Conversation[];
|
|
34
|
+
}
|
|
35
|
+
export type GetConversationsResponse = BaseResponse<GetConversationsData>;
|
|
36
|
+
export type ChatRecordBase = {};
|
|
37
|
+
export interface GetChatRecordsOutput {
|
|
38
|
+
recordList: ChatRecordBase[];
|
|
39
|
+
total: number;
|
|
40
|
+
}
|
|
41
|
+
export interface UserProfile {
|
|
42
|
+
tags?: string[];
|
|
43
|
+
preferences?: Record<string, any>;
|
|
44
|
+
}
|
|
45
|
+
export interface LoneMemoryRecord extends ChatRecordBase {
|
|
46
|
+
userProfile?: UserProfile;
|
|
47
|
+
}
|
|
48
|
+
export interface GetLoneMemoryOutput {
|
|
49
|
+
recordList: LoneMemoryRecord[];
|
|
50
|
+
total: number;
|
|
51
|
+
}
|
|
52
|
+
export interface KnowledgeResultMetaData {
|
|
53
|
+
score: number;
|
|
54
|
+
source_type: string;
|
|
55
|
+
doc_type: string;
|
|
56
|
+
content_length: number;
|
|
57
|
+
}
|
|
58
|
+
export interface KnowledgeResult {
|
|
59
|
+
id: string;
|
|
60
|
+
content: string;
|
|
61
|
+
chunk_id: string;
|
|
62
|
+
meta_data: KnowledgeResultMetaData;
|
|
63
|
+
}
|
|
64
|
+
export interface KnowledgeSearchOutput {
|
|
65
|
+
knowledge_results: KnowledgeResult[];
|
|
66
|
+
total_tokens: number;
|
|
67
|
+
}
|
|
68
|
+
export type KnowledgeBaseRetrieveResponse = BaseResponse<KnowledgeSearchOutput>;
|
|
69
|
+
export interface AgentInfo {
|
|
70
|
+
agentId: string;
|
|
71
|
+
version: string;
|
|
72
|
+
desc: string;
|
|
73
|
+
}
|
|
74
|
+
export interface GetAgentInfoResponse {
|
|
75
|
+
code: number;
|
|
76
|
+
success: boolean;
|
|
77
|
+
msg: string;
|
|
78
|
+
data: {
|
|
79
|
+
agentInfo: AgentInfo;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export interface QueryTasksData {
|
|
83
|
+
request_id: string;
|
|
84
|
+
output: {
|
|
85
|
+
task_id: string;
|
|
86
|
+
task_status: "PENDING" | "RUNNING" | "SUCCEEDED" | "FAILED" | "CANCELED" | "UNKNOWN";
|
|
87
|
+
results?: Array<{
|
|
88
|
+
url?: string;
|
|
89
|
+
[key: string]: any;
|
|
90
|
+
}>;
|
|
91
|
+
task_metrics?: {
|
|
92
|
+
TOTAL: number;
|
|
93
|
+
SUCCEEDED: number;
|
|
94
|
+
FAILED: number;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
usage?: {
|
|
98
|
+
image_count?: number;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export type RawQueryTasksResponse = QueryTasksData;
|
|
102
|
+
export type QueryTasksResponse = BaseResponse<QueryTasksData>;
|
|
103
|
+
export interface UploadFileInput {
|
|
104
|
+
file: File | Buffer | string;
|
|
105
|
+
filename: string;
|
|
106
|
+
purpose?: string;
|
|
107
|
+
}
|
|
108
|
+
export interface UploadFileData {
|
|
109
|
+
id: string;
|
|
110
|
+
object: string;
|
|
111
|
+
bytes: number;
|
|
112
|
+
created_at: number;
|
|
113
|
+
filename: string;
|
|
114
|
+
purpose: string;
|
|
115
|
+
status: string;
|
|
116
|
+
status_details?: any;
|
|
117
|
+
}
|
|
118
|
+
export type UploadFileResponse = BaseResponse<UploadFileData>;
|
package/lib/agent.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type AI, type models } from "@vectorx/ai-sdk";
|
|
2
|
+
import { type RcbContext } from "@vectorx/functions-framework";
|
|
3
|
+
import type { CreateRecordPairResponse, GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesResponse, KnowledgeBaseRetrieveResponse, QueryTasksResponse, UploadFileResponse } from "./agent-types";
|
|
4
|
+
import { type CreateRecordPairParams, type GetHistoryMessagesParams, type KnowledgeSearchInput } from "./schema";
|
|
5
|
+
import { SSESender } from "./sse-sender";
|
|
6
|
+
export declare enum AgentType {
|
|
7
|
+
AI_SDK_REQUEST_START = "AI_SDK_REQUEST_START",
|
|
8
|
+
AI_SDK_REQUEST_SUCCESS = "AI_SDK_REQUEST_SUCCESS",
|
|
9
|
+
AI_SDK_REQUEST_ERROR = "AI_SDK_REQUEST_ERROR",
|
|
10
|
+
AI_SDK_CREATE_MODEL = "AI_SDK_CREATE_MODEL",
|
|
11
|
+
AI_SDK_CREATE_RECORD_PAIR = "AI_SDK_CREATE_RECORD_PAIR",
|
|
12
|
+
AI_SDK_GET_HISTORY_MESSAGES = "AI_SDK_GET_HISTORY_MESSAGES",
|
|
13
|
+
AI_SDK_GET_CONVERSATIONS = "AI_SDK_GET_CONVERSATIONS",
|
|
14
|
+
Ai_SDK_POST_KNOWLEDGE_BASE = "Ai_SDK_POST_KNOWLEDGE_BASE",
|
|
15
|
+
AI_SDK_QUERY_TASKS = "AI_SDK_QUERY_TASKS"
|
|
16
|
+
}
|
|
17
|
+
export declare enum AgentEventType {
|
|
18
|
+
FRAMEWORK_EVENT = "FRAMEWORK_EVENT",
|
|
19
|
+
HTTP_ACCESS = "HTTP_ACCESS"
|
|
20
|
+
}
|
|
21
|
+
export declare class AgentRuntime {
|
|
22
|
+
readonly context: RcbContext;
|
|
23
|
+
sseSender: SSESender<unknown>;
|
|
24
|
+
protected ai: AI;
|
|
25
|
+
protected request: RcbContext["request"];
|
|
26
|
+
protected logger: RcbContext["logger"];
|
|
27
|
+
get agentTag(): string;
|
|
28
|
+
get agentId(): string;
|
|
29
|
+
get version(): string;
|
|
30
|
+
constructor(context: RcbContext);
|
|
31
|
+
private createLoggedRequest;
|
|
32
|
+
protected createModel(modelName: models.ModelName): models.ReActModel;
|
|
33
|
+
protected createRecordPair(params: CreateRecordPairParams): Promise<CreateRecordPairResponse>;
|
|
34
|
+
protected getHistoryMessages(params: GetHistoryMessagesParams): Promise<GetHistoryMessagesResponse>;
|
|
35
|
+
protected getConversations(): Promise<GetConversationsResponse>;
|
|
36
|
+
getAgentInfo(): Promise<GetAgentInfoResponse>;
|
|
37
|
+
knowledgeBaseRetrieve(params: KnowledgeSearchInput): Promise<KnowledgeBaseRetrieveResponse>;
|
|
38
|
+
queryTasks(task_id?: string): Promise<QueryTasksResponse>;
|
|
39
|
+
uploadFile(input: any): Promise<UploadFileResponse>;
|
|
40
|
+
}
|