@agentica/vector-selector 0.43.3 → 0.44.0-dev.20260313
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/LICENSE +21 -21
- package/README.md +218 -218
- package/lib/index.mjs +28 -28
- package/lib/index.mjs.map +1 -1
- package/lib/select.js +5 -5
- package/lib/strategy/sqlite.strategy.js +14 -14
- package/lib/tools.js +29 -29
- package/package.json +4 -5
- package/src/extract_query.ts +51 -51
- package/src/index.ts +67 -67
- package/src/select.ts +212 -212
- package/src/strategy/index.ts +2 -2
- package/src/strategy/postgres.strategy.ts +116 -116
- package/src/strategy/sqlite.strategy.ts +123 -123
- package/src/tools.ts +99 -99
- package/src/utils.test.ts +357 -357
- package/src/utils.ts +123 -123
package/src/select.ts
CHANGED
|
@@ -1,212 +1,212 @@
|
|
|
1
|
-
import type { AgenticaAssistantMessageEvent, AgenticaContext, AgenticaOperationSelection } from "@agentica/core";
|
|
2
|
-
|
|
3
|
-
import { AgenticaDefaultPrompt, AgenticaSystemPrompt, factory, utils } from "@agentica/core";
|
|
4
|
-
|
|
5
|
-
import { Tools } from "./tools";
|
|
6
|
-
|
|
7
|
-
interface IFailure {
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
validation: {
|
|
11
|
-
data: string;
|
|
12
|
-
errors: string[];
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function selectFunction(props: {
|
|
17
|
-
ctx: AgenticaContext;
|
|
18
|
-
toolList: object[];
|
|
19
|
-
prevFailures?: IFailure[];
|
|
20
|
-
restRetry?: number;
|
|
21
|
-
}): Promise<void> {
|
|
22
|
-
const { ctx, toolList, prevFailures = [], restRetry = 5 } = props;
|
|
23
|
-
const selectCompletion = await ctx.request("select", {
|
|
24
|
-
messages: [
|
|
25
|
-
{
|
|
26
|
-
role: "assistant",
|
|
27
|
-
tool_calls: [
|
|
28
|
-
{
|
|
29
|
-
type: "function",
|
|
30
|
-
id: "getApiFunctions",
|
|
31
|
-
function: {
|
|
32
|
-
name: "getApiFunctions",
|
|
33
|
-
arguments: JSON.stringify({}),
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
],
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
role: "tool",
|
|
40
|
-
tool_call_id: "getApiFunctions",
|
|
41
|
-
content: JSON.stringify(toolList),
|
|
42
|
-
},
|
|
43
|
-
...ctx.histories.flatMap(factory.decodeHistory),
|
|
44
|
-
{
|
|
45
|
-
role: "user",
|
|
46
|
-
content: ctx.prompt.contents.map(factory.decodeUserMessageContent),
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
role: "system",
|
|
50
|
-
content: `${ctx.config?.systemPrompt?.select?.(ctx.histories)
|
|
51
|
-
?? AgenticaSystemPrompt.SELECT}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
When selecting functions, consider what the user can call from their perspective, and choose all the functions necessary to accomplish the task.
|
|
55
|
-
Select them in a logical sequence, taking into account the relationships between each function.
|
|
56
|
-
`,
|
|
57
|
-
},
|
|
58
|
-
...emendMessages(prevFailures),
|
|
59
|
-
{
|
|
60
|
-
role: "system",
|
|
61
|
-
content: AgenticaDefaultPrompt.write(ctx.config),
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
tool_choice: {
|
|
65
|
-
type: "function",
|
|
66
|
-
function: {
|
|
67
|
-
name: "select_functions",
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
// parallel_tool_calls: false,
|
|
71
|
-
tools: [Tools.select_functions],
|
|
72
|
-
})
|
|
73
|
-
.then(async (v) => {
|
|
74
|
-
if (v.type === "none-stream") {
|
|
75
|
-
return v.value;
|
|
76
|
-
}
|
|
77
|
-
return utils.ChatGptCompletionMessageUtil.merge(await utils.StreamUtil.readAll(v.value));
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const toolCalls = selectCompletion.choices
|
|
81
|
-
.filter(v => v.message.tool_calls != null);
|
|
82
|
-
|
|
83
|
-
if (toolCalls.length === 0) {
|
|
84
|
-
selectCompletion.choices.forEach((v) => {
|
|
85
|
-
if (v.message.content != null && v.message.content !== "") {
|
|
86
|
-
const event: AgenticaAssistantMessageEvent = factory.createAssistantMessageEvent({
|
|
87
|
-
stream: utils.toAsyncGenerator(v.message.content),
|
|
88
|
-
done: () => true,
|
|
89
|
-
get: () => v.message.content as string,
|
|
90
|
-
join: async () => (v.message.content as string),
|
|
91
|
-
});
|
|
92
|
-
void ctx.dispatch(event).catch(() => {});
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const failures = toolCalls.reduce<IFailure[]>((acc, cur) => {
|
|
98
|
-
cur.message.tool_calls?.filter(tc => tc.type === "function").forEach((tc) => {
|
|
99
|
-
const errors: string[] = [];
|
|
100
|
-
const arg = JSON.parse(tc.function.arguments) as Partial<{ reason: string; function_name: string }>[];
|
|
101
|
-
if (!Array.isArray(arg)) {
|
|
102
|
-
errors.push(JSON.stringify({
|
|
103
|
-
path: "$input",
|
|
104
|
-
expected: "array",
|
|
105
|
-
value: arg,
|
|
106
|
-
}));
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
arg.forEach((v, idx) => {
|
|
110
|
-
if (v.reason == null || typeof v.reason !== "string") {
|
|
111
|
-
errors.push(JSON.stringify({
|
|
112
|
-
path: `$$input[${idx}].reason`,
|
|
113
|
-
expected: "string",
|
|
114
|
-
value: v.reason,
|
|
115
|
-
}));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (v.function_name == null || typeof v.function_name !== "string") {
|
|
119
|
-
errors.push(JSON.stringify({
|
|
120
|
-
path: `$$input[${idx}].function_name`,
|
|
121
|
-
expected: "string",
|
|
122
|
-
value: v.function_name,
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
if (errors.length !== 0) {
|
|
128
|
-
acc.push({
|
|
129
|
-
id: tc.id,
|
|
130
|
-
name: tc.function.name,
|
|
131
|
-
validation: { data: tc.function.arguments, errors },
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
return acc;
|
|
136
|
-
}, []);
|
|
137
|
-
|
|
138
|
-
if (failures.length !== 0) {
|
|
139
|
-
const feedback = [...prevFailures, ...failures];
|
|
140
|
-
if (restRetry === 0) {
|
|
141
|
-
throw new Error(`Failed to select function after ${restRetry} retries\n${JSON.stringify(feedback)}`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return selectFunction({
|
|
145
|
-
ctx,
|
|
146
|
-
toolList,
|
|
147
|
-
prevFailures: feedback,
|
|
148
|
-
restRetry: restRetry - 1,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
toolCalls.forEach((v) => {
|
|
153
|
-
v.message.tool_calls!.filter(tc => tc.type === "function").forEach((tc) => {
|
|
154
|
-
const arg = JSON.parse(tc.function.arguments) as {
|
|
155
|
-
function_list: {
|
|
156
|
-
reason: string;
|
|
157
|
-
function_name: string;
|
|
158
|
-
}[];
|
|
159
|
-
};
|
|
160
|
-
arg.function_list.forEach((v) => {
|
|
161
|
-
const operation = ctx.operations.flat.get(v.function_name);
|
|
162
|
-
|
|
163
|
-
if (operation === undefined) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const selection: AgenticaOperationSelection
|
|
167
|
-
= factory.createOperationSelection({
|
|
168
|
-
reason: v.reason,
|
|
169
|
-
operation,
|
|
170
|
-
});
|
|
171
|
-
ctx.stack.push(selection);
|
|
172
|
-
void ctx.dispatch(
|
|
173
|
-
factory.createSelectEvent({
|
|
174
|
-
selection,
|
|
175
|
-
}),
|
|
176
|
-
).catch(() => {});
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function emendMessages(failures: IFailure[]): ReturnType<typeof factory.decodeHistory> {
|
|
183
|
-
return failures
|
|
184
|
-
.flatMap(f => [
|
|
185
|
-
{
|
|
186
|
-
role: "assistant",
|
|
187
|
-
tool_calls: [
|
|
188
|
-
{
|
|
189
|
-
type: "function",
|
|
190
|
-
id: f.id,
|
|
191
|
-
function: {
|
|
192
|
-
name: f.name,
|
|
193
|
-
arguments: JSON.stringify(f.validation.data),
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
],
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
role: "tool",
|
|
200
|
-
content: JSON.stringify(f.validation.errors),
|
|
201
|
-
tool_call_id: f.id,
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
role: "system",
|
|
205
|
-
content: [
|
|
206
|
-
"You A.I. assistant has composed wrong typed arguments.",
|
|
207
|
-
"",
|
|
208
|
-
"Correct it at the next function calling.",
|
|
209
|
-
].join("\n"),
|
|
210
|
-
},
|
|
211
|
-
]) satisfies ReturnType<typeof factory.decodeHistory>;
|
|
212
|
-
}
|
|
1
|
+
import type { AgenticaAssistantMessageEvent, AgenticaContext, AgenticaOperationSelection } from "@agentica/core";
|
|
2
|
+
|
|
3
|
+
import { AgenticaDefaultPrompt, AgenticaSystemPrompt, factory, utils } from "@agentica/core";
|
|
4
|
+
|
|
5
|
+
import { Tools } from "./tools";
|
|
6
|
+
|
|
7
|
+
interface IFailure {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
validation: {
|
|
11
|
+
data: string;
|
|
12
|
+
errors: string[];
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function selectFunction(props: {
|
|
17
|
+
ctx: AgenticaContext;
|
|
18
|
+
toolList: object[];
|
|
19
|
+
prevFailures?: IFailure[];
|
|
20
|
+
restRetry?: number;
|
|
21
|
+
}): Promise<void> {
|
|
22
|
+
const { ctx, toolList, prevFailures = [], restRetry = 5 } = props;
|
|
23
|
+
const selectCompletion = await ctx.request("select", {
|
|
24
|
+
messages: [
|
|
25
|
+
{
|
|
26
|
+
role: "assistant",
|
|
27
|
+
tool_calls: [
|
|
28
|
+
{
|
|
29
|
+
type: "function",
|
|
30
|
+
id: "getApiFunctions",
|
|
31
|
+
function: {
|
|
32
|
+
name: "getApiFunctions",
|
|
33
|
+
arguments: JSON.stringify({}),
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
role: "tool",
|
|
40
|
+
tool_call_id: "getApiFunctions",
|
|
41
|
+
content: JSON.stringify(toolList),
|
|
42
|
+
},
|
|
43
|
+
...ctx.histories.flatMap(factory.decodeHistory),
|
|
44
|
+
{
|
|
45
|
+
role: "user",
|
|
46
|
+
content: ctx.prompt.contents.map(factory.decodeUserMessageContent),
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
role: "system",
|
|
50
|
+
content: `${ctx.config?.systemPrompt?.select?.(ctx.histories)
|
|
51
|
+
?? AgenticaSystemPrompt.SELECT}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
When selecting functions, consider what the user can call from their perspective, and choose all the functions necessary to accomplish the task.
|
|
55
|
+
Select them in a logical sequence, taking into account the relationships between each function.
|
|
56
|
+
`,
|
|
57
|
+
},
|
|
58
|
+
...emendMessages(prevFailures),
|
|
59
|
+
{
|
|
60
|
+
role: "system",
|
|
61
|
+
content: AgenticaDefaultPrompt.write(ctx.config),
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
tool_choice: {
|
|
65
|
+
type: "function",
|
|
66
|
+
function: {
|
|
67
|
+
name: "select_functions",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
// parallel_tool_calls: false,
|
|
71
|
+
tools: [Tools.select_functions],
|
|
72
|
+
})
|
|
73
|
+
.then(async (v) => {
|
|
74
|
+
if (v.type === "none-stream") {
|
|
75
|
+
return v.value;
|
|
76
|
+
}
|
|
77
|
+
return utils.ChatGptCompletionMessageUtil.merge(await utils.StreamUtil.readAll(v.value));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const toolCalls = selectCompletion.choices
|
|
81
|
+
.filter(v => v.message.tool_calls != null);
|
|
82
|
+
|
|
83
|
+
if (toolCalls.length === 0) {
|
|
84
|
+
selectCompletion.choices.forEach((v) => {
|
|
85
|
+
if (v.message.content != null && v.message.content !== "") {
|
|
86
|
+
const event: AgenticaAssistantMessageEvent = factory.createAssistantMessageEvent({
|
|
87
|
+
stream: utils.toAsyncGenerator(v.message.content),
|
|
88
|
+
done: () => true,
|
|
89
|
+
get: () => v.message.content as string,
|
|
90
|
+
join: async () => (v.message.content as string),
|
|
91
|
+
});
|
|
92
|
+
void ctx.dispatch(event).catch(() => {});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const failures = toolCalls.reduce<IFailure[]>((acc, cur) => {
|
|
98
|
+
cur.message.tool_calls?.filter(tc => tc.type === "function").forEach((tc) => {
|
|
99
|
+
const errors: string[] = [];
|
|
100
|
+
const arg = JSON.parse(tc.function.arguments) as Partial<{ reason: string; function_name: string }>[];
|
|
101
|
+
if (!Array.isArray(arg)) {
|
|
102
|
+
errors.push(JSON.stringify({
|
|
103
|
+
path: "$input",
|
|
104
|
+
expected: "array",
|
|
105
|
+
value: arg,
|
|
106
|
+
}));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
arg.forEach((v, idx) => {
|
|
110
|
+
if (v.reason == null || typeof v.reason !== "string") {
|
|
111
|
+
errors.push(JSON.stringify({
|
|
112
|
+
path: `$$input[${idx}].reason`,
|
|
113
|
+
expected: "string",
|
|
114
|
+
value: v.reason,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (v.function_name == null || typeof v.function_name !== "string") {
|
|
119
|
+
errors.push(JSON.stringify({
|
|
120
|
+
path: `$$input[${idx}].function_name`,
|
|
121
|
+
expected: "string",
|
|
122
|
+
value: v.function_name,
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (errors.length !== 0) {
|
|
128
|
+
acc.push({
|
|
129
|
+
id: tc.id,
|
|
130
|
+
name: tc.function.name,
|
|
131
|
+
validation: { data: tc.function.arguments, errors },
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
return acc;
|
|
136
|
+
}, []);
|
|
137
|
+
|
|
138
|
+
if (failures.length !== 0) {
|
|
139
|
+
const feedback = [...prevFailures, ...failures];
|
|
140
|
+
if (restRetry === 0) {
|
|
141
|
+
throw new Error(`Failed to select function after ${restRetry} retries\n${JSON.stringify(feedback)}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return selectFunction({
|
|
145
|
+
ctx,
|
|
146
|
+
toolList,
|
|
147
|
+
prevFailures: feedback,
|
|
148
|
+
restRetry: restRetry - 1,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
toolCalls.forEach((v) => {
|
|
153
|
+
v.message.tool_calls!.filter(tc => tc.type === "function").forEach((tc) => {
|
|
154
|
+
const arg = JSON.parse(tc.function.arguments) as {
|
|
155
|
+
function_list: {
|
|
156
|
+
reason: string;
|
|
157
|
+
function_name: string;
|
|
158
|
+
}[];
|
|
159
|
+
};
|
|
160
|
+
arg.function_list.forEach((v) => {
|
|
161
|
+
const operation = ctx.operations.flat.get(v.function_name);
|
|
162
|
+
|
|
163
|
+
if (operation === undefined) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const selection: AgenticaOperationSelection
|
|
167
|
+
= factory.createOperationSelection({
|
|
168
|
+
reason: v.reason,
|
|
169
|
+
operation,
|
|
170
|
+
});
|
|
171
|
+
ctx.stack.push(selection);
|
|
172
|
+
void ctx.dispatch(
|
|
173
|
+
factory.createSelectEvent({
|
|
174
|
+
selection,
|
|
175
|
+
}),
|
|
176
|
+
).catch(() => {});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function emendMessages(failures: IFailure[]): ReturnType<typeof factory.decodeHistory> {
|
|
183
|
+
return failures
|
|
184
|
+
.flatMap(f => [
|
|
185
|
+
{
|
|
186
|
+
role: "assistant",
|
|
187
|
+
tool_calls: [
|
|
188
|
+
{
|
|
189
|
+
type: "function",
|
|
190
|
+
id: f.id,
|
|
191
|
+
function: {
|
|
192
|
+
name: f.name,
|
|
193
|
+
arguments: JSON.stringify(f.validation.data),
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
role: "tool",
|
|
200
|
+
content: JSON.stringify(f.validation.errors),
|
|
201
|
+
tool_call_id: f.id,
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
role: "system",
|
|
205
|
+
content: [
|
|
206
|
+
"You A.I. assistant has composed wrong typed arguments.",
|
|
207
|
+
"",
|
|
208
|
+
"Correct it at the next function calling.",
|
|
209
|
+
].join("\n"),
|
|
210
|
+
},
|
|
211
|
+
]) satisfies ReturnType<typeof factory.decodeHistory>;
|
|
212
|
+
}
|
package/src/strategy/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./postgres.strategy";
|
|
2
|
-
export { configureSqliteStrategy } from "./sqlite.strategy";
|
|
1
|
+
export * from "./postgres.strategy";
|
|
2
|
+
export { configureSqliteStrategy } from "./sqlite.strategy";
|
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import type { AgenticaContext, AgenticaOperation } from "@agentica/core";
|
|
2
|
-
import type { IConnection } from "@wrtnlabs/connector-hive-api";
|
|
3
|
-
import type { IApplicationConnectorRetrieval } from "@wrtnlabs/connector-hive-api/lib/structures/connector/IApplicationConnectorRetrieval";
|
|
4
|
-
|
|
5
|
-
import { functional, HttpError } from "@wrtnlabs/connector-hive-api";
|
|
6
|
-
|
|
7
|
-
import type { IAgenticaVectorSelectorStrategy } from "..";
|
|
8
|
-
|
|
9
|
-
import { getRetry, groupByArray } from "../utils";
|
|
10
|
-
|
|
11
|
-
const retry = getRetry(3);
|
|
12
|
-
|
|
13
|
-
const filterMap = new Map<string, IApplicationConnectorRetrieval.IFilter>();
|
|
14
|
-
function searchTool(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy["searchTool"] {
|
|
15
|
-
return async (ctx: AgenticaContext, query: string) => {
|
|
16
|
-
const filter = filterMap.get(JSON.stringify(ctx.operations.array));
|
|
17
|
-
return retry(async () =>
|
|
18
|
-
functional.connector_retrievals.createRetrievalRequest(connection, {
|
|
19
|
-
query,
|
|
20
|
-
limit: 10,
|
|
21
|
-
filter,
|
|
22
|
-
}),
|
|
23
|
-
);
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function embedOperation(connection: IConnection<object | undefined>) {
|
|
28
|
-
return async (
|
|
29
|
-
controllerName: string,
|
|
30
|
-
opList: AgenticaOperation[],
|
|
31
|
-
) => {
|
|
32
|
-
const application = await retry(async () =>
|
|
33
|
-
functional.applications.create(connection, {
|
|
34
|
-
name: controllerName,
|
|
35
|
-
description: undefined,
|
|
36
|
-
}),
|
|
37
|
-
).catch(async (e) => {
|
|
38
|
-
if (!(e instanceof HttpError)) {
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
if (e.status !== 409) {
|
|
42
|
-
throw e;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return retry(async () =>
|
|
46
|
-
functional.applications.by_names.getByName(
|
|
47
|
-
connection,
|
|
48
|
-
controllerName,
|
|
49
|
-
),
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const version = await retry(async () =>
|
|
54
|
-
functional.applications.by_ids.versions.create(
|
|
55
|
-
connection,
|
|
56
|
-
application.id,
|
|
57
|
-
{},
|
|
58
|
-
),
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
// concurrency request count
|
|
62
|
-
await groupByArray(opList, 10).reduce(async (accPromise, cur) => {
|
|
63
|
-
await accPromise;
|
|
64
|
-
await Promise.all(
|
|
65
|
-
cur.map(async v =>
|
|
66
|
-
retry(async () =>
|
|
67
|
-
functional.application_versions.by_ids.connectors.create(
|
|
68
|
-
connection,
|
|
69
|
-
version.id,
|
|
70
|
-
{ name: v.name, description: v.function.description ?? "" },
|
|
71
|
-
),
|
|
72
|
-
),
|
|
73
|
-
),
|
|
74
|
-
);
|
|
75
|
-
return Promise.resolve();
|
|
76
|
-
}, Promise.resolve());
|
|
77
|
-
|
|
78
|
-
return { version, applicationId: application.id };
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function embedContext(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy["embedContext"] {
|
|
83
|
-
return async (props: { ctx: AgenticaContext; setEmbedded: () => void }) => {
|
|
84
|
-
const { ctx, setEmbedded } = props;
|
|
85
|
-
const filter = await Promise.all(
|
|
86
|
-
Array.from(ctx.operations.group.entries()).map(
|
|
87
|
-
async ([key, value]: [
|
|
88
|
-
string,
|
|
89
|
-
Map<string, AgenticaOperation>,
|
|
90
|
-
]) => {
|
|
91
|
-
const result = await embedOperation(connection)(
|
|
92
|
-
key,
|
|
93
|
-
Array.from(value.values()),
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
id: result.applicationId,
|
|
98
|
-
version: result.version.version,
|
|
99
|
-
type: "byId",
|
|
100
|
-
} satisfies IApplicationConnectorRetrieval.IFilterApplicationById;
|
|
101
|
-
},
|
|
102
|
-
),
|
|
103
|
-
);
|
|
104
|
-
filterMap.set(JSON.stringify(ctx.operations.array), {
|
|
105
|
-
applications: filter,
|
|
106
|
-
});
|
|
107
|
-
setEmbedded();
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function configurePostgresStrategy(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy {
|
|
112
|
-
return {
|
|
113
|
-
searchTool: searchTool(connection),
|
|
114
|
-
embedContext: embedContext(connection),
|
|
115
|
-
};
|
|
116
|
-
}
|
|
1
|
+
import type { AgenticaContext, AgenticaOperation } from "@agentica/core";
|
|
2
|
+
import type { IConnection } from "@wrtnlabs/connector-hive-api";
|
|
3
|
+
import type { IApplicationConnectorRetrieval } from "@wrtnlabs/connector-hive-api/lib/structures/connector/IApplicationConnectorRetrieval";
|
|
4
|
+
|
|
5
|
+
import { functional, HttpError } from "@wrtnlabs/connector-hive-api";
|
|
6
|
+
|
|
7
|
+
import type { IAgenticaVectorSelectorStrategy } from "..";
|
|
8
|
+
|
|
9
|
+
import { getRetry, groupByArray } from "../utils";
|
|
10
|
+
|
|
11
|
+
const retry = getRetry(3);
|
|
12
|
+
|
|
13
|
+
const filterMap = new Map<string, IApplicationConnectorRetrieval.IFilter>();
|
|
14
|
+
function searchTool(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy["searchTool"] {
|
|
15
|
+
return async (ctx: AgenticaContext, query: string) => {
|
|
16
|
+
const filter = filterMap.get(JSON.stringify(ctx.operations.array));
|
|
17
|
+
return retry(async () =>
|
|
18
|
+
functional.connector_retrievals.createRetrievalRequest(connection, {
|
|
19
|
+
query,
|
|
20
|
+
limit: 10,
|
|
21
|
+
filter,
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function embedOperation(connection: IConnection<object | undefined>) {
|
|
28
|
+
return async (
|
|
29
|
+
controllerName: string,
|
|
30
|
+
opList: AgenticaOperation[],
|
|
31
|
+
) => {
|
|
32
|
+
const application = await retry(async () =>
|
|
33
|
+
functional.applications.create(connection, {
|
|
34
|
+
name: controllerName,
|
|
35
|
+
description: undefined,
|
|
36
|
+
}),
|
|
37
|
+
).catch(async (e) => {
|
|
38
|
+
if (!(e instanceof HttpError)) {
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
if (e.status !== 409) {
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return retry(async () =>
|
|
46
|
+
functional.applications.by_names.getByName(
|
|
47
|
+
connection,
|
|
48
|
+
controllerName,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const version = await retry(async () =>
|
|
54
|
+
functional.applications.by_ids.versions.create(
|
|
55
|
+
connection,
|
|
56
|
+
application.id,
|
|
57
|
+
{},
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// concurrency request count
|
|
62
|
+
await groupByArray(opList, 10).reduce(async (accPromise, cur) => {
|
|
63
|
+
await accPromise;
|
|
64
|
+
await Promise.all(
|
|
65
|
+
cur.map(async v =>
|
|
66
|
+
retry(async () =>
|
|
67
|
+
functional.application_versions.by_ids.connectors.create(
|
|
68
|
+
connection,
|
|
69
|
+
version.id,
|
|
70
|
+
{ name: v.name, description: v.function.description ?? "" },
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
return Promise.resolve();
|
|
76
|
+
}, Promise.resolve());
|
|
77
|
+
|
|
78
|
+
return { version, applicationId: application.id };
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function embedContext(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy["embedContext"] {
|
|
83
|
+
return async (props: { ctx: AgenticaContext; setEmbedded: () => void }) => {
|
|
84
|
+
const { ctx, setEmbedded } = props;
|
|
85
|
+
const filter = await Promise.all(
|
|
86
|
+
Array.from(ctx.operations.group.entries()).map(
|
|
87
|
+
async ([key, value]: [
|
|
88
|
+
string,
|
|
89
|
+
Map<string, AgenticaOperation>,
|
|
90
|
+
]) => {
|
|
91
|
+
const result = await embedOperation(connection)(
|
|
92
|
+
key,
|
|
93
|
+
Array.from(value.values()),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
id: result.applicationId,
|
|
98
|
+
version: result.version.version,
|
|
99
|
+
type: "byId",
|
|
100
|
+
} satisfies IApplicationConnectorRetrieval.IFilterApplicationById;
|
|
101
|
+
},
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
filterMap.set(JSON.stringify(ctx.operations.array), {
|
|
105
|
+
applications: filter,
|
|
106
|
+
});
|
|
107
|
+
setEmbedded();
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function configurePostgresStrategy(connection: IConnection<object | undefined>): IAgenticaVectorSelectorStrategy {
|
|
112
|
+
return {
|
|
113
|
+
searchTool: searchTool(connection),
|
|
114
|
+
embedContext: embedContext(connection),
|
|
115
|
+
};
|
|
116
|
+
}
|