@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/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/tools.ts","../src/extract_query.ts","../src/select.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":[null,null,null,null,null],"names":["Tools","extract_query","type","function","name","description","parameters","properties","query_list","items","reason","query","required","select_functions","function_list","function_name","async","extractQuery","ctx","completionStream","request","messages","role","content","join","histories","map","factory","decodeHistory","flat","prompt","contents","decodeUserMessageContent","tool_choice","tools","completion","value","utils","ChatGptCompletionMessageUtil","merge","StreamUtil","readAll","queries","choices","message","tool_calls","filter","tc","flatMap","v","arg","JSON","parse","arguments","Array","isArray","selectFunction","props","toolList","prevFailures","restRetry","selectCompletion","id","stringify","tool_call_id","config","systemPrompt","select","AgenticaSystemPrompt","SELECT","emendMessages","AgenticaDefaultPrompt","write","then","toolCalls","length","forEach","event","createAssistantMessageEvent","stream","toAsyncGenerator","done","get","dispatch","catch","failures","reduce","acc","cur","errors","push","path","expected","idx","validation","data","feedback","Error","operation","operations","undefined","selection","createOperationSelection","stack","createSelectEvent","f","uniqBy","array","selector","seen","Set","item","key","has","add","BootAgenticaVectorSelector","isEmbedded","setEmbedded","useEmbeddedContext","searchTool","embedContext","strategy","selectorExecute","Promise","all","res","op","protocol","method","tags","arr","set"],"mappings":";;;;;;AAEO,MAAMA,QAAQ;IACnBC,eAAe;QACbC,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,aAAa;YACbC,YAAY;gBACVJ,MAAM;gBACNK,YAAY;oBACVC,YAAY;wBACVN,MAAM;wBACNO,OAAO;4BACLP,MAAM;4BACNK,YAAY;gCACVG,QAAQ;oCACNR,MAAM;oCACNG,aAAa;;gCAEfM,OAAO;oCACLT,MAAM;oCACNG,aAAa;;;4BAGjBO,UAAU,EAAC,UAAU;;;;gBAI3BA,UAAU,EAAC;;;;IAKjBC,kBAAkB;QAChBX,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,aAAa;YA8BbC,YAAY;gBACVJ,MAAM;gBACNK,YAAY;oBACVO,eAAe;wBACbZ,MAAM;wBACNO,OAAO;4BACLP,MAAM;4BACNK,YAAY;gCACVG,QAAQ;oCACNR,MAAM;oCACNG,aAAa;;gCAEfU,eAAe;oCACbb,MAAM;oCACNG,aAAa;;;4BAGjBO,UAAU,EAAC,UAAU;;;;gBAI3BA,UAAU,EAAC;;;;;;AClFZI,eAAeC,aAAaC;IACjC,MAAMC,yBAAyBD,IAAIE,QAAQ,UAAU;QACnDC,UAAU,EACR;YACEC,MAAM;YACNC,SAAS,EACP,uIACA,kFACA,0EACA,8IACA,qCACAC,KAAK;cAENN,IAAIO,UACJC,IAAIC,QAAQC,eACZC,QACH;YACEP,MAAM;YACNC,SAASL,IAAIY,OAAOC,SAASL,IAAIC,QAAQK;;QAG7CC,aAAa;QAEbC,OAAO,EAAClC,MAAMC;;IAGhB,MAAMkC,mBAAmB;QACvB,IAAIhB,iBAAiBjB,SAAS,eAAe;YAC3C,OAAOiB,iBAAiBiB
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/tools.ts","../src/extract_query.ts","../src/select.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":[null,null,null,null,null],"names":["Tools","extract_query","type","function","name","description","parameters","properties","query_list","items","reason","query","required","select_functions","function_list","function_name","async","extractQuery","ctx","completionStream","request","messages","role","content","join","histories","map","factory","decodeHistory","flat","prompt","contents","decodeUserMessageContent","tool_choice","tools","completion","value","utils","ChatGptCompletionMessageUtil","merge","StreamUtil","readAll","queries","choices","message","tool_calls","filter","tc","flatMap","v","arg","JSON","parse","arguments","Array","isArray","selectFunction","props","toolList","prevFailures","restRetry","selectCompletion","id","stringify","tool_call_id","config","systemPrompt","select","AgenticaSystemPrompt","SELECT","emendMessages","AgenticaDefaultPrompt","write","then","toolCalls","length","forEach","event","createAssistantMessageEvent","stream","toAsyncGenerator","done","get","dispatch","catch","failures","reduce","acc","cur","errors","push","path","expected","idx","validation","data","feedback","Error","operation","operations","undefined","selection","createOperationSelection","stack","createSelectEvent","f","uniqBy","array","selector","seen","Set","item","key","has","add","BootAgenticaVectorSelector","isEmbedded","setEmbedded","useEmbeddedContext","searchTool","embedContext","strategy","selectorExecute","Promise","all","res","op","protocol","method","tags","arr","set"],"mappings":";;;;;;AAEO,MAAMA,QAAQ;IACnBC,eAAe;QACbC,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,aAAa;YACbC,YAAY;gBACVJ,MAAM;gBACNK,YAAY;oBACVC,YAAY;wBACVN,MAAM;wBACNO,OAAO;4BACLP,MAAM;4BACNK,YAAY;gCACVG,QAAQ;oCACNR,MAAM;oCACNG,aAAa;;gCAEfM,OAAO;oCACLT,MAAM;oCACNG,aAAa;;;4BAGjBO,UAAU,EAAC,UAAU;;;;gBAI3BA,UAAU,EAAC;;;;IAKjBC,kBAAkB;QAChBX,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,aAAa;YA8BbC,YAAY;gBACVJ,MAAM;gBACNK,YAAY;oBACVO,eAAe;wBACbZ,MAAM;wBACNO,OAAO;4BACLP,MAAM;4BACNK,YAAY;gCACVG,QAAQ;oCACNR,MAAM;oCACNG,aAAa;;gCAEfU,eAAe;oCACbb,MAAM;oCACNG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C,GAAG3H,SAASwF;gBAClBsC,MAAMH,GAAG3H,SAAS8H;;YAElBxD,KAAKyD,OAAOxB,OAAOwB,KAAKjF,KAAKA,EAAE7C;QAEnC,IAAIsD,SAASiB,WAAW,GAAG;YACzB;AACF;cACMnB,eAAe;YAAEtC;YAAKwC;;;IAE9B,OAAOgE;AACT;;SAEgBJ;IACd,MAAMa,MAAM,IAAIrB;IAChB,OAAO;QACLM,YAAalG,OACXiH,IAAIlB,IAAI9D,KAAKY,UAAU7C,IAAIiF,WAAWQ;QACxCU,aAAcnG;YACZiH,IAAIjB,IAAI/D,KAAKY,UAAU7C,IAAIiF,WAAWQ;;;AAG5C;;"}
|
package/lib/select.js
CHANGED
|
@@ -43,11 +43,11 @@ function selectFunction(props) {
|
|
|
43
43
|
},
|
|
44
44
|
{
|
|
45
45
|
role: "system",
|
|
46
|
-
content: `${(_d = (_c = (_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.select) === null || _c === void 0 ? void 0 : _c.call(_b, ctx.histories)) !== null && _d !== void 0 ? _d : core_1.AgenticaSystemPrompt.SELECT}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
When selecting functions, consider what the user can call from their perspective, and choose all the functions necessary to accomplish the task.
|
|
50
|
-
Select them in a logical sequence, taking into account the relationships between each function.
|
|
46
|
+
content: `${(_d = (_c = (_b = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.systemPrompt) === null || _b === void 0 ? void 0 : _b.select) === null || _c === void 0 ? void 0 : _c.call(_b, ctx.histories)) !== null && _d !== void 0 ? _d : core_1.AgenticaSystemPrompt.SELECT}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
When selecting functions, consider what the user can call from their perspective, and choose all the functions necessary to accomplish the task.
|
|
50
|
+
Select them in a logical sequence, taking into account the relationships between each function.
|
|
51
51
|
`,
|
|
52
52
|
},
|
|
53
53
|
...emendMessages(prevFailures),
|
|
@@ -21,14 +21,14 @@ function configureSqliteStrategy(props) {
|
|
|
21
21
|
const cohere = new cohere_ai_1.CohereClientV2({
|
|
22
22
|
token: cohereApiKey,
|
|
23
23
|
});
|
|
24
|
-
db.exec(`
|
|
25
|
-
CREATE TABLE IF NOT EXISTS _agentica_vector_selector_embeddings (
|
|
26
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
27
|
-
hash TEXT NOT NULL,
|
|
28
|
-
name TEXT NOT NULL,
|
|
29
|
-
description TEXT,
|
|
30
|
-
vector BLOB NOT NULL
|
|
31
|
-
)
|
|
24
|
+
db.exec(`
|
|
25
|
+
CREATE TABLE IF NOT EXISTS _agentica_vector_selector_embeddings (
|
|
26
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
27
|
+
hash TEXT NOT NULL,
|
|
28
|
+
name TEXT NOT NULL,
|
|
29
|
+
description TEXT,
|
|
30
|
+
vector BLOB NOT NULL
|
|
31
|
+
)
|
|
32
32
|
`);
|
|
33
33
|
function embed(text, inputType) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -87,12 +87,12 @@ function configureSqliteStrategy(props) {
|
|
|
87
87
|
return __awaiter(this, void 0, void 0, function* () {
|
|
88
88
|
const hash = getHash(ctx);
|
|
89
89
|
const vector = yield embed(query, "search_query");
|
|
90
|
-
const result = db.prepare(`
|
|
91
|
-
SELECT name, description, vec_distance_L2(vector, ?) as distance
|
|
92
|
-
FROM _agentica_vector_selector_embeddings
|
|
93
|
-
WHERE hash = ?
|
|
94
|
-
ORDER BY distance
|
|
95
|
-
LIMIT 10
|
|
90
|
+
const result = db.prepare(`
|
|
91
|
+
SELECT name, description, vec_distance_L2(vector, ?) as distance
|
|
92
|
+
FROM _agentica_vector_selector_embeddings
|
|
93
|
+
WHERE hash = ?
|
|
94
|
+
ORDER BY distance
|
|
95
|
+
LIMIT 10
|
|
96
96
|
`).all(JSON.stringify(vector), hash);
|
|
97
97
|
return result;
|
|
98
98
|
});
|
package/lib/tools.js
CHANGED
|
@@ -36,35 +36,35 @@ exports.Tools = {
|
|
|
36
36
|
type: "function",
|
|
37
37
|
function: {
|
|
38
38
|
name: "select_functions",
|
|
39
|
-
description: `Select proper API functions to call.
|
|
40
|
-
|
|
41
|
-
If you A.I. agent has found some proper API functions to call
|
|
42
|
-
from the conversation with user, please select the API functions
|
|
43
|
-
just by calling this function.
|
|
44
|
-
|
|
45
|
-
When user wants to call a same function multiply, you A.I. agent must
|
|
46
|
-
list up it multiply in the \`functions\` property. Otherwise the user has
|
|
47
|
-
requested to call many different functions, you A.I. agent have to assign
|
|
48
|
-
them all into the \`functions\` property.
|
|
49
|
-
|
|
50
|
-
Also, if you A.I. agent can't specify a specific function to call due to lack
|
|
51
|
-
of specificity or homogeneity of candidate functions, just assign all of them
|
|
52
|
-
by in the \`functions\` property too. Instead, when you A.I. agent can specify
|
|
53
|
-
a specific function to call, the others would be eliminated.
|
|
54
|
-
|
|
55
|
-
@example
|
|
56
|
-
\`\`\`json
|
|
57
|
-
[
|
|
58
|
-
{
|
|
59
|
-
"reason": "The user wants to call the function multiply.",
|
|
60
|
-
"function_name": "get_user_info"
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
"reason": "The user wants to modify the user info.",
|
|
64
|
-
"function_name": "modify_user_info"
|
|
65
|
-
}
|
|
66
|
-
]
|
|
67
|
-
\`\`\`
|
|
39
|
+
description: `Select proper API functions to call.
|
|
40
|
+
|
|
41
|
+
If you A.I. agent has found some proper API functions to call
|
|
42
|
+
from the conversation with user, please select the API functions
|
|
43
|
+
just by calling this function.
|
|
44
|
+
|
|
45
|
+
When user wants to call a same function multiply, you A.I. agent must
|
|
46
|
+
list up it multiply in the \`functions\` property. Otherwise the user has
|
|
47
|
+
requested to call many different functions, you A.I. agent have to assign
|
|
48
|
+
them all into the \`functions\` property.
|
|
49
|
+
|
|
50
|
+
Also, if you A.I. agent can't specify a specific function to call due to lack
|
|
51
|
+
of specificity or homogeneity of candidate functions, just assign all of them
|
|
52
|
+
by in the \`functions\` property too. Instead, when you A.I. agent can specify
|
|
53
|
+
a specific function to call, the others would be eliminated.
|
|
54
|
+
|
|
55
|
+
@example
|
|
56
|
+
\`\`\`json
|
|
57
|
+
[
|
|
58
|
+
{
|
|
59
|
+
"reason": "The user wants to call the function multiply.",
|
|
60
|
+
"function_name": "get_user_info"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"reason": "The user wants to modify the user info.",
|
|
64
|
+
"function_name": "modify_user_info"
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
\`\`\`
|
|
68
68
|
`,
|
|
69
69
|
parameters: {
|
|
70
70
|
type: "object",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentica/vector-selector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.0-dev.20260313",
|
|
4
4
|
"author": "Wrtn Technologies",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://wrtnlabs.io",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"src"
|
|
48
48
|
],
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@agentica/core": "^0.
|
|
50
|
+
"@agentica/core": "^0.44.0-dev.20260313"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@noble/hashes": "^1.8.0",
|
|
@@ -60,16 +60,15 @@
|
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@rollup/plugin-terser": "^0.4.4",
|
|
62
62
|
"@rollup/plugin-typescript": "^12.1.1",
|
|
63
|
-
"@samchon/openapi": "^6.0.1",
|
|
64
63
|
"@types/better-sqlite3": "^7.6.13",
|
|
65
|
-
"@types/node": "^
|
|
64
|
+
"@types/node": "^25.3.0",
|
|
66
65
|
"json-schema-to-ts": "^3.1.1",
|
|
67
66
|
"rimraf": "^6.0.1",
|
|
68
67
|
"rollup": "^4.29.1",
|
|
69
68
|
"type-fest": "^4.37.0",
|
|
70
69
|
"typescript": "~5.9.3",
|
|
71
70
|
"vitest": "^3.0.9",
|
|
72
|
-
"@agentica/core": "^0.
|
|
71
|
+
"@agentica/core": "^0.44.0-dev.20260313"
|
|
73
72
|
},
|
|
74
73
|
"scripts": {
|
|
75
74
|
"build": "tsc -p ./tsconfig.build.json && rollup -c",
|
package/src/extract_query.ts
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
-
import type { FromSchema } from "json-schema-to-ts";
|
|
3
|
-
|
|
4
|
-
import { factory, utils } from "@agentica/core";
|
|
5
|
-
|
|
6
|
-
import { Tools } from "./tools";
|
|
7
|
-
|
|
8
|
-
export async function extractQuery(ctx: AgenticaContext) {
|
|
9
|
-
const completionStream = await ctx.request("select", {
|
|
10
|
-
messages: [
|
|
11
|
-
{
|
|
12
|
-
role: "system",
|
|
13
|
-
content: [
|
|
14
|
-
"You are a function searcher. You will extract search queries from the user's message, and the query results will be function names.",
|
|
15
|
-
"A query is a 2–3 sentence description of the action the user needs to perform.",
|
|
16
|
-
"Therefore, the extracted queries must be suitable for function search.",
|
|
17
|
-
"You need to identify the actions required to achieve what the user wants and extract queries that can be used to search for those actions.",
|
|
18
|
-
"Extract only one query per task.",
|
|
19
|
-
].join("\n"),
|
|
20
|
-
},
|
|
21
|
-
...ctx.histories
|
|
22
|
-
.map(factory.decodeHistory)
|
|
23
|
-
.flat(),
|
|
24
|
-
{
|
|
25
|
-
role: "user",
|
|
26
|
-
content: ctx.prompt.contents.map(factory.decodeUserMessageContent),
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
tool_choice: "required",
|
|
30
|
-
|
|
31
|
-
tools: [Tools.extract_query],
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const completion = await (async () => {
|
|
35
|
-
if (completionStream.type === "none-stream") {
|
|
36
|
-
return completionStream.value;
|
|
37
|
-
}
|
|
38
|
-
return utils.ChatGptCompletionMessageUtil.merge(await utils.StreamUtil.readAll(completionStream.value));
|
|
39
|
-
})();
|
|
40
|
-
|
|
41
|
-
const queries = completion.choices[0]?.message.tool_calls?.filter(tc => tc.type === "function").flatMap((v) => {
|
|
42
|
-
const arg = JSON.parse(v.function.arguments) as Partial<FromSchema<typeof Tools.extract_query.function.parameters>>;
|
|
43
|
-
if (!Array.isArray(arg.query_list)) {
|
|
44
|
-
return [];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return arg.query_list.map(v => v.query);
|
|
48
|
-
}) ?? [];
|
|
49
|
-
|
|
50
|
-
return queries;
|
|
51
|
-
}
|
|
1
|
+
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
+
import type { FromSchema } from "json-schema-to-ts";
|
|
3
|
+
|
|
4
|
+
import { factory, utils } from "@agentica/core";
|
|
5
|
+
|
|
6
|
+
import { Tools } from "./tools";
|
|
7
|
+
|
|
8
|
+
export async function extractQuery(ctx: AgenticaContext) {
|
|
9
|
+
const completionStream = await ctx.request("select", {
|
|
10
|
+
messages: [
|
|
11
|
+
{
|
|
12
|
+
role: "system",
|
|
13
|
+
content: [
|
|
14
|
+
"You are a function searcher. You will extract search queries from the user's message, and the query results will be function names.",
|
|
15
|
+
"A query is a 2–3 sentence description of the action the user needs to perform.",
|
|
16
|
+
"Therefore, the extracted queries must be suitable for function search.",
|
|
17
|
+
"You need to identify the actions required to achieve what the user wants and extract queries that can be used to search for those actions.",
|
|
18
|
+
"Extract only one query per task.",
|
|
19
|
+
].join("\n"),
|
|
20
|
+
},
|
|
21
|
+
...ctx.histories
|
|
22
|
+
.map(factory.decodeHistory)
|
|
23
|
+
.flat(),
|
|
24
|
+
{
|
|
25
|
+
role: "user",
|
|
26
|
+
content: ctx.prompt.contents.map(factory.decodeUserMessageContent),
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
tool_choice: "required",
|
|
30
|
+
|
|
31
|
+
tools: [Tools.extract_query],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const completion = await (async () => {
|
|
35
|
+
if (completionStream.type === "none-stream") {
|
|
36
|
+
return completionStream.value;
|
|
37
|
+
}
|
|
38
|
+
return utils.ChatGptCompletionMessageUtil.merge(await utils.StreamUtil.readAll(completionStream.value));
|
|
39
|
+
})();
|
|
40
|
+
|
|
41
|
+
const queries = completion.choices[0]?.message.tool_calls?.filter(tc => tc.type === "function").flatMap((v) => {
|
|
42
|
+
const arg = JSON.parse(v.function.arguments) as Partial<FromSchema<typeof Tools.extract_query.function.parameters>>;
|
|
43
|
+
if (!Array.isArray(arg.query_list)) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return arg.query_list.map(v => v.query);
|
|
48
|
+
}) ?? [];
|
|
49
|
+
|
|
50
|
+
return queries;
|
|
51
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
-
|
|
3
|
-
import { extractQuery } from "./extract_query";
|
|
4
|
-
import { selectFunction } from "./select";
|
|
5
|
-
import { uniqBy } from "./utils";
|
|
6
|
-
|
|
7
|
-
export interface IAgenticaVectorSelectorBootProps {
|
|
8
|
-
strategy: IAgenticaVectorSelectorStrategy;
|
|
9
|
-
}
|
|
10
|
-
export interface IAgenticaVectorSelectorStrategy {
|
|
11
|
-
searchTool: (ctx: AgenticaContext, query: string) => Promise<{
|
|
12
|
-
name: string;
|
|
13
|
-
description: string | undefined;
|
|
14
|
-
}[]>;
|
|
15
|
-
embedContext: (
|
|
16
|
-
props: {
|
|
17
|
-
ctx: AgenticaContext;
|
|
18
|
-
setEmbedded: () => void;
|
|
19
|
-
}
|
|
20
|
-
) => Promise<void>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function BootAgenticaVectorSelector(props: IAgenticaVectorSelectorBootProps) {
|
|
24
|
-
const { isEmbedded, setEmbedded } = useEmbeddedContext();
|
|
25
|
-
const { searchTool, embedContext } = props.strategy;
|
|
26
|
-
const selectorExecute = async (
|
|
27
|
-
ctx: AgenticaContext,
|
|
28
|
-
): Promise<void> => {
|
|
29
|
-
if (!isEmbedded(ctx)) {
|
|
30
|
-
await embedContext({ ctx, setEmbedded: () => setEmbedded(ctx) });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const queries = await extractQuery(ctx);
|
|
34
|
-
const toolList = await Promise.all(
|
|
35
|
-
queries.map(async query => searchTool(ctx, query)),
|
|
36
|
-
).then(res => res.flat().map((v) => {
|
|
37
|
-
const op = ctx.operations.flat.get(v.name);
|
|
38
|
-
if (op === undefined || op.protocol !== "http") {
|
|
39
|
-
return v;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
...v,
|
|
44
|
-
method: op.function.method,
|
|
45
|
-
path: op.function.path,
|
|
46
|
-
tags: op.function.tags,
|
|
47
|
-
};
|
|
48
|
-
})).then(arr => uniqBy(arr, v => v.name));
|
|
49
|
-
|
|
50
|
-
if (toolList.length === 0) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
await selectFunction({ ctx, toolList });
|
|
54
|
-
};
|
|
55
|
-
return selectorExecute;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function useEmbeddedContext() {
|
|
59
|
-
const set = new Set<string>();
|
|
60
|
-
return {
|
|
61
|
-
isEmbedded: (ctx: AgenticaContext) =>
|
|
62
|
-
set.has(JSON.stringify(ctx.operations.array)),
|
|
63
|
-
setEmbedded: (ctx: AgenticaContext) => {
|
|
64
|
-
set.add(JSON.stringify(ctx.operations.array));
|
|
65
|
-
},
|
|
66
|
-
} as const;
|
|
67
|
-
}
|
|
1
|
+
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
+
|
|
3
|
+
import { extractQuery } from "./extract_query";
|
|
4
|
+
import { selectFunction } from "./select";
|
|
5
|
+
import { uniqBy } from "./utils";
|
|
6
|
+
|
|
7
|
+
export interface IAgenticaVectorSelectorBootProps {
|
|
8
|
+
strategy: IAgenticaVectorSelectorStrategy;
|
|
9
|
+
}
|
|
10
|
+
export interface IAgenticaVectorSelectorStrategy {
|
|
11
|
+
searchTool: (ctx: AgenticaContext, query: string) => Promise<{
|
|
12
|
+
name: string;
|
|
13
|
+
description: string | undefined;
|
|
14
|
+
}[]>;
|
|
15
|
+
embedContext: (
|
|
16
|
+
props: {
|
|
17
|
+
ctx: AgenticaContext;
|
|
18
|
+
setEmbedded: () => void;
|
|
19
|
+
},
|
|
20
|
+
) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function BootAgenticaVectorSelector(props: IAgenticaVectorSelectorBootProps) {
|
|
24
|
+
const { isEmbedded, setEmbedded } = useEmbeddedContext();
|
|
25
|
+
const { searchTool, embedContext } = props.strategy;
|
|
26
|
+
const selectorExecute = async (
|
|
27
|
+
ctx: AgenticaContext,
|
|
28
|
+
): Promise<void> => {
|
|
29
|
+
if (!isEmbedded(ctx)) {
|
|
30
|
+
await embedContext({ ctx, setEmbedded: () => setEmbedded(ctx) });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const queries = await extractQuery(ctx);
|
|
34
|
+
const toolList = await Promise.all(
|
|
35
|
+
queries.map(async query => searchTool(ctx, query)),
|
|
36
|
+
).then(res => res.flat().map((v) => {
|
|
37
|
+
const op = ctx.operations.flat.get(v.name);
|
|
38
|
+
if (op === undefined || op.protocol !== "http") {
|
|
39
|
+
return v;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
...v,
|
|
44
|
+
method: op.function.method,
|
|
45
|
+
path: op.function.path,
|
|
46
|
+
tags: op.function.tags,
|
|
47
|
+
};
|
|
48
|
+
})).then(arr => uniqBy(arr, v => v.name));
|
|
49
|
+
|
|
50
|
+
if (toolList.length === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await selectFunction({ ctx, toolList });
|
|
54
|
+
};
|
|
55
|
+
return selectorExecute;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useEmbeddedContext() {
|
|
59
|
+
const set = new Set<string>();
|
|
60
|
+
return {
|
|
61
|
+
isEmbedded: (ctx: AgenticaContext) =>
|
|
62
|
+
set.has(JSON.stringify(ctx.operations.array)),
|
|
63
|
+
setEmbedded: (ctx: AgenticaContext) => {
|
|
64
|
+
set.add(JSON.stringify(ctx.operations.array));
|
|
65
|
+
},
|
|
66
|
+
} as const;
|
|
67
|
+
}
|