@agentica/vector-selector 0.20.0 → 0.21.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/README.md +5 -4
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +5 -4
- package/lib/index.mjs.map +1 -1
- package/lib/strategy/index.d.ts +1 -0
- package/lib/strategy/index.js +3 -0
- package/lib/strategy/index.js.map +1 -1
- package/lib/strategy/sqlite.strategy.d.ts +8 -0
- package/lib/strategy/sqlite.strategy.js +105 -0
- package/lib/strategy/sqlite.strategy.js.map +1 -0
- package/lib/utils.d.ts +9 -0
- package/lib/utils.js +16 -0
- package/lib/utils.js.map +1 -1
- package/package.json +9 -4
- package/src/index.ts +12 -19
- package/src/strategy/index.ts +1 -0
- package/src/strategy/sqlite.strategy.ts +124 -0
- package/src/utils.test.ts +173 -1
- package/src/utils.ts +19 -0
- package/lib/embed.d.ts +0 -0
- package/lib/embed.js +0 -2
- package/lib/embed.js.map +0 -1
- package/src/embed.ts +0 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<!-- https://github.com/user-attachments/assets/5326cc59-5129-470d-abcb-c3f458b5c488 -->
|
|
4
4
|
|
|
5
|
-

|
|
6
6
|
|
|
7
7
|
[](https://github.com/wrtnlabs/agentica/blob/master/LICENSE)
|
|
8
8
|
[](https://www.npmjs.com/package/@agentica/core)
|
|
@@ -147,9 +147,10 @@ flowchart
|
|
|
147
147
|
|
|
148
148
|
Agentica enhances AI function calling by the following strategies:
|
|
149
149
|
|
|
150
|
-
- [**
|
|
151
|
-
- [**
|
|
152
|
-
- [**
|
|
150
|
+
- [**Compiler Driven Development**](https://wrtnlabs.io/agentica/docs/concepts/compiler-driven-development): constructs function calling schema automatically by compiler skills without hand-writing.
|
|
151
|
+
- [**JSON Schema Conversion**](https://wrtnlabs.io/agentica/docs/core/vendor/#schema-specification): automatically handles specification differences between LLM vendors, ensuring seamless integration regardless of your chosen AI model.
|
|
152
|
+
- [**Validation Feedback**](https://wrtnlabs.io/agentica/docs/concepts/function-calling#validation-feedback): detects and corrects AI mistakes in argument composition, dramatically reducing errors and improving reliability.
|
|
153
|
+
- [**Selector Agent**](https://wrtnlabs.io/agentica/docs/concepts/function-calling#orchestration-strategy): filtering candidate functions to minimize context usage, optimize performance, and reduce token consumption.
|
|
153
154
|
|
|
154
155
|
Thanks to these innovations, Agentica makes AI function calling easier, safer, and more accurate than before. Development becomes more intuitive since you only need to prepare functions relevant to your specific use case, and scaling your agent's capabilities is as simple as adding or removing functions.
|
|
155
156
|
|
package/lib/index.js
CHANGED
|
@@ -22,10 +22,7 @@ function BootAgenticaVectorSelector(props) {
|
|
|
22
22
|
yield embedContext({ ctx, setEmbedded: () => setEmbedded(ctx) });
|
|
23
23
|
}
|
|
24
24
|
const queries = yield (0, extract_query_1.extractQuery)(ctx);
|
|
25
|
-
const toolList = yield Promise.all(queries.map((query) => __awaiter(this, void 0, void 0, function* () { return searchTool(ctx, query); }))).then(res => res.
|
|
26
|
-
name: v.name,
|
|
27
|
-
description: v.description,
|
|
28
|
-
}))).map((v) => {
|
|
25
|
+
const toolList = yield Promise.all(queries.map((query) => __awaiter(this, void 0, void 0, function* () { return searchTool(ctx, query); }))).then(res => res.flat().map((v) => {
|
|
29
26
|
const op = ctx.operations.flat.get(v.name);
|
|
30
27
|
if (op === undefined || op.protocol !== "http") {
|
|
31
28
|
return v;
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAuBA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAuBA,gEAoCC;AAED,gDASC;AAnED,mDAA+C;AAC/C,qCAA0C;AAC1C,mCAAiC;AAkBjC,SAAgB,0BAA0B,CAAuC,KAA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E,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAE1C,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,IAAA,uBAAc,EAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,CAAC;QACxD,OAAO,OAAO,CAAC;IACjB,CAAC,CAAA,CAAC;IAEF,OAAO,eAAe,CAAC;AACzB,CAAC;AAED,SAAgB,kBAAkB;IAChC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,OAAO;QACL,UAAU,EAAE,CAAC,GAAiC,EAAE,EAAE,CAChD,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAC/C,WAAW,EAAE,CAAC,GAAiC,EAAE,EAAE;YACjD,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAChD,CAAC;KACO,CAAC;AACb,CAAC"}
|
package/lib/index.mjs
CHANGED
|
@@ -4,6 +4,10 @@ import { AgenticaDefaultPrompt } from "@agentica/core/src/constants/AgenticaDefa
|
|
|
4
4
|
|
|
5
5
|
import { AgenticaSystemPrompt } from "@agentica/core/src/constants/AgenticaSystemPrompt";
|
|
6
6
|
|
|
7
|
+
import "@noble/hashes/sha2";
|
|
8
|
+
|
|
9
|
+
import "@noble/hashes/utils";
|
|
10
|
+
|
|
7
11
|
const Tools = {
|
|
8
12
|
extract_query: {
|
|
9
13
|
type: "function",
|
|
@@ -271,10 +275,7 @@ function BootAgenticaVectorSelector(props) {
|
|
|
271
275
|
});
|
|
272
276
|
}
|
|
273
277
|
const queries = await extractQuery(ctx);
|
|
274
|
-
const toolList = await Promise.all(queries.map((async query => searchTool(ctx, query)))).then((res => res.
|
|
275
|
-
name: v.name,
|
|
276
|
-
description: v.description
|
|
277
|
-
}))))).map((v => {
|
|
278
|
+
const toolList = await Promise.all(queries.map((async query => searchTool(ctx, query)))).then((res => res.flat().map((v => {
|
|
278
279
|
const op = ctx.operations.flat.get(v.name);
|
|
279
280
|
if (op === undefined || op.protocol !== "http") {
|
|
280
281
|
return v;
|
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","flat","prompt","text","tool_choice","tools","chunks","utils","StreamUtil","readAll","completion","ChatGptCompletionMessageUtil","merge","queries","choices","message","tool_calls","flatMap","v","arg","JSON","parse","arguments","Array","isArray","selectFunction","props","toolList","prevFailures","restRetry","selectCompletion","AgenticaDefaultPrompt","write","config","id","stringify","tool_call_id","systemPrompt","select","AgenticaSystemPrompt","SELECT","emendMessages","parallel_tool_calls","then","toolCalls","filter","length","createTextHistory","failures","reduce","acc","cur","forEach","tc","errors","push","path","expected","value","idx","validation","data","feedback","Error","prompts","collection","selections","toJSON","s","operation","operations","get","undefined","selection","createOperationSelection","stack","dispatch","createSelectEvent","catch","f","uniqBy","array","selector","seen","Set","item","key","has","add","BootAgenticaVectorSelector","isEmbedded","setEmbedded","useEmbeddedContext","searchTool","embedContext","strategy","selectorExecute","Promise","all","res","
|
|
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","flat","prompt","text","tool_choice","tools","chunks","utils","StreamUtil","readAll","completion","ChatGptCompletionMessageUtil","merge","queries","choices","message","tool_calls","flatMap","v","arg","JSON","parse","arguments","Array","isArray","selectFunction","props","toolList","prevFailures","restRetry","selectCompletion","AgenticaDefaultPrompt","write","config","id","stringify","tool_call_id","systemPrompt","select","AgenticaSystemPrompt","SELECT","emendMessages","parallel_tool_calls","then","toolCalls","filter","length","createTextHistory","failures","reduce","acc","cur","forEach","tc","errors","push","path","expected","value","idx","validation","data","feedback","Error","prompts","collection","selections","toJSON","s","operation","operations","get","undefined","selection","createOperationSelection","stack","dispatch","createSelectEvent","catch","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;;;;;;ACjFZI,eAAeC,aAAmDC;IACvE,MAAMC,yBAAyBD,IAAIE,QAAQ,U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;IACd,MAAMa,MAAM,IAAIrB;IAChB,OAAO;QACLM,YAAanG,OACXkH,IAAIlB,IAAInE,KAAKe,UAAU5C,IAAI+E,WAAWW;QACxCU,aAAcpG;YACZkH,IAAIjB,IAAIpE,KAAKe,UAAU5C,IAAI+E,WAAWW;AAAO;;AAGnD;;"}
|
package/lib/strategy/index.d.ts
CHANGED
package/lib/strategy/index.js
CHANGED
|
@@ -14,5 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.configureSqliteStrategy = void 0;
|
|
17
18
|
__exportStar(require("./postgres.strategy"), exports);
|
|
19
|
+
var sqlite_strategy_1 = require("./sqlite.strategy");
|
|
20
|
+
Object.defineProperty(exports, "configureSqliteStrategy", { enumerable: true, get: function () { return sqlite_strategy_1.configureSqliteStrategy; } });
|
|
18
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/strategy/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/strategy/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,qDAA4D;AAAnD,0HAAA,uBAAuB,OAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
+
import type { Database } from "better-sqlite3";
|
|
3
|
+
import type { IAgenticaVectorSelectorStrategy } from "..";
|
|
4
|
+
export interface IAgenticaSqliteVectorSelectorStrategyProps {
|
|
5
|
+
db: Database;
|
|
6
|
+
cohereApiKey: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function configureSqliteStrategy<SchemaModel extends ILlmSchema.Model>(props: IAgenticaSqliteVectorSelectorStrategyProps): IAgenticaVectorSelectorStrategy<SchemaModel>;
|
|
@@ -0,0 +1,105 @@
|
|
|
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.configureSqliteStrategy = configureSqliteStrategy;
|
|
13
|
+
const cohere_ai_1 = require("cohere-ai");
|
|
14
|
+
const sqlite_vec_1 = require("sqlite-vec");
|
|
15
|
+
const utils_1 = require("../utils");
|
|
16
|
+
const retry = (0, utils_1.getRetry)(3);
|
|
17
|
+
const hashMemo = new Map();
|
|
18
|
+
function configureSqliteStrategy(props) {
|
|
19
|
+
const { db, cohereApiKey } = props;
|
|
20
|
+
(0, sqlite_vec_1.load)(db);
|
|
21
|
+
const cohere = new cohere_ai_1.CohereClientV2({
|
|
22
|
+
token: cohereApiKey,
|
|
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
|
+
)
|
|
32
|
+
`);
|
|
33
|
+
function embed(text, inputType) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
const result = yield retry(() => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
return cohere.embed({
|
|
37
|
+
texts: [text],
|
|
38
|
+
inputType,
|
|
39
|
+
model: "embed-multilingual-light-v3.0",
|
|
40
|
+
embeddingTypes: ["float"],
|
|
41
|
+
});
|
|
42
|
+
}));
|
|
43
|
+
if ((result.embeddings.float == null) || result.embeddings.float.length === 0) {
|
|
44
|
+
throw new Error("no float embeddings returned");
|
|
45
|
+
}
|
|
46
|
+
const vector = result.embeddings.float[0];
|
|
47
|
+
return vector;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// it's memoized to avoid generating the same hash for the same context
|
|
51
|
+
// if you know react, it's like useMemo
|
|
52
|
+
const getHash = (ctx) => {
|
|
53
|
+
if (hashMemo.has(ctx)) {
|
|
54
|
+
return hashMemo.get(ctx);
|
|
55
|
+
}
|
|
56
|
+
const hash = (0, utils_1.generateHashFromCtx)(ctx);
|
|
57
|
+
hashMemo.set(ctx, hash);
|
|
58
|
+
return hash;
|
|
59
|
+
};
|
|
60
|
+
// embed each operation in the context.opersation.array
|
|
61
|
+
function embedOperation(props) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const name = props.operation.function.name;
|
|
64
|
+
const embedding = yield retry(() => __awaiter(this, void 0, void 0, function* () { var _a; return embed((_a = props.operation.function.description) !== null && _a !== void 0 ? _a : name, "search_document"); }));
|
|
65
|
+
db
|
|
66
|
+
.prepare("INSERT INTO _agentica_vector_selector_embeddings (hash, name, description, vector) VALUES (?, ?, ?, vec_f32(?))")
|
|
67
|
+
.run(props.hash, name, props.operation.function.description, JSON.stringify(embedding));
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function embedContext(props) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
const hash = getHash(props.ctx);
|
|
73
|
+
const prepared = db.prepare(`SELECT name FROM _agentica_vector_selector_embeddings WHERE hash = ?`).all(hash);
|
|
74
|
+
if (prepared.length > 0) {
|
|
75
|
+
props.setEmbedded();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
yield (0, utils_1.groupByArray)(props.ctx.operations.array, 10).reduce((accPromise, cur) => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
yield accPromise;
|
|
80
|
+
yield Promise.all(cur.map((v) => __awaiter(this, void 0, void 0, function* () { return embedOperation({ hash, operation: v }); })));
|
|
81
|
+
return Promise.resolve();
|
|
82
|
+
}), Promise.resolve());
|
|
83
|
+
props.setEmbedded();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function searchTool(ctx, query) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const hash = getHash(ctx);
|
|
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
|
|
96
|
+
`).all(JSON.stringify(vector), hash);
|
|
97
|
+
return result;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
searchTool,
|
|
102
|
+
embedContext,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=sqlite.strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.strategy.js","sourceRoot":"","sources":["../../src/strategy/sqlite.strategy.ts"],"names":[],"mappings":";;;;;;;;;;;AAmBA,0DAwGC;AAtHD,yCAA2C;AAC3C,2CAAkC;AAIlC,oCAAuE;AAOvE,MAAM,KAAK,GAAG,IAAA,gBAAQ,EAAC,CAAC,CAAC,CAAC;AAC1B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;AAC3C,SAA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AAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC,CAAA,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;YACtB,KAAK,CAAC,WAAW,EAAE,CAAC;QACtB,CAAC;KAAA;IAED,SAAe,UAAU,CAAC,GAAiC,EAAE,KAAa;;YAIxE,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;YAC1B,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;YAElD,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC;;;;;;KAMzB,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,CAIhC,CAAC;YACJ,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;IAED,OAAO;QACL,UAAU;QACV,YAAY;KACb,CAAC;AACJ,CAAC"}
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
+
import type { ILlmSchema } from "@samchon/openapi";
|
|
1
3
|
import type { GreaterThan, Integer } from "type-fest";
|
|
2
4
|
/** type function to check if a number is greater than 0 */
|
|
3
5
|
type GreaterThanZeroInteger<T extends number> = GreaterThan<Integer<T>, 0> extends true ? T : never;
|
|
@@ -49,4 +51,11 @@ export declare function groupByArray<T, TCount extends number>(array: T[], count
|
|
|
49
51
|
* ```
|
|
50
52
|
*/
|
|
51
53
|
export declare function uniqBy<T, K>(array: T[], selector: (item: T) => K): T[];
|
|
54
|
+
/**
|
|
55
|
+
* Generates a hash from an Agentica ctx.operations.array.
|
|
56
|
+
*
|
|
57
|
+
* @param ctx - The Agentica context to generate a hash from
|
|
58
|
+
* @returns A hash of the Agentica context
|
|
59
|
+
*/
|
|
60
|
+
export declare function generateHashFromCtx<SchemaModel extends ILlmSchema.Model>(ctx: AgenticaContext<SchemaModel>): string;
|
|
52
61
|
export {};
|
package/lib/utils.js
CHANGED
|
@@ -12,6 +12,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.getRetry = getRetry;
|
|
13
13
|
exports.groupByArray = groupByArray;
|
|
14
14
|
exports.uniqBy = uniqBy;
|
|
15
|
+
exports.generateHashFromCtx = generateHashFromCtx;
|
|
16
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
17
|
+
const utils_1 = require("@noble/hashes/utils");
|
|
15
18
|
/**
|
|
16
19
|
* This function is used to get a retry function.
|
|
17
20
|
*
|
|
@@ -105,4 +108,17 @@ function uniqBy(array, selector) {
|
|
|
105
108
|
return true;
|
|
106
109
|
});
|
|
107
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Generates a hash from an Agentica ctx.operations.array.
|
|
113
|
+
*
|
|
114
|
+
* @param ctx - The Agentica context to generate a hash from
|
|
115
|
+
* @returns A hash of the Agentica context
|
|
116
|
+
*/
|
|
117
|
+
function generateHashFromCtx(ctx) {
|
|
118
|
+
const target = JSON.stringify(ctx.operations.array);
|
|
119
|
+
const bytes = (0, utils_1.utf8ToBytes)(target);
|
|
120
|
+
const hash = (0, sha2_1.sha256)(bytes);
|
|
121
|
+
const binary = String.fromCharCode(...hash);
|
|
122
|
+
return btoa(binary);
|
|
123
|
+
}
|
|
108
124
|
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;AAuBA,4BAsBC;AAcD,oCAkBC;AAsBD,wBAUC;AAQD,kDAMC;AAvHD,6CAA4C;AAC5C,+CAAkD;AAKlD;;;;;;;;;;;;GAYG;AACH,SAAgB,QAAQ,CAAwB,KAAqC;IACnF,IAAI,KAAK,GAAG,CAAC,E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GAmBG;AACH,SAAgB,MAAM,CAAO,KAAU,EAAE,QAAwB;IAC/D,MAAM,IAAI,GAAG,IAAI,GAAG,EAAK,CAAC;IAC1B,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAuC,GAAiC;IACzG,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACpD,MAAM,KAAK,GAAG,IAAA,mBAAW,EAAC,MAAM,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,IAAA,aAAM,EAAC,KAAK,CAAC,CAAC;IAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentica/vector-selector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"author": "Wrtn Technologies",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://wrtnlabs.io",
|
|
@@ -47,15 +47,20 @@
|
|
|
47
47
|
"src"
|
|
48
48
|
],
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@agentica/core": "^0.
|
|
50
|
+
"@agentica/core": "^0.21.0"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@
|
|
53
|
+
"@noble/hashes": "^1.8.0",
|
|
54
|
+
"@wrtnlabs/connector-hive-api": "^1.5.0",
|
|
55
|
+
"better-sqlite3": "^11.9.1",
|
|
56
|
+
"cohere-ai": "^7.17.1",
|
|
57
|
+
"sqlite-vec": "0.1.7-alpha.2"
|
|
54
58
|
},
|
|
55
59
|
"devDependencies": {
|
|
56
60
|
"@rollup/plugin-terser": "^0.4.4",
|
|
57
61
|
"@rollup/plugin-typescript": "^12.1.1",
|
|
58
62
|
"@samchon/openapi": "^4.2.0",
|
|
63
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
59
64
|
"@types/node": "^22.10.5",
|
|
60
65
|
"json-schema-to-ts": "^3.1.1",
|
|
61
66
|
"rimraf": "^6.0.1",
|
|
@@ -63,7 +68,7 @@
|
|
|
63
68
|
"type-fest": "^4.37.0",
|
|
64
69
|
"typescript": "~5.8.3",
|
|
65
70
|
"vitest": "^3.0.9",
|
|
66
|
-
"@agentica/core": "^0.
|
|
71
|
+
"@agentica/core": "^0.21.0"
|
|
67
72
|
},
|
|
68
73
|
"scripts": {
|
|
69
74
|
"build": "tsc -p ./tsconfig.build.json && rollup -c",
|
package/src/index.ts
CHANGED
|
@@ -34,26 +34,19 @@ export function BootAgenticaVectorSelector<SchemaModel extends ILlmSchema.Model>
|
|
|
34
34
|
const queries = await extractQuery(ctx);
|
|
35
35
|
const toolList = await Promise.all(
|
|
36
36
|
queries.map(async query => searchTool(ctx, query)),
|
|
37
|
-
).then(res =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
})),
|
|
43
|
-
).map((v) => {
|
|
44
|
-
const op = ctx.operations.flat.get(v.name);
|
|
45
|
-
if (op === undefined || op.protocol !== "http") {
|
|
46
|
-
return v;
|
|
47
|
-
}
|
|
37
|
+
).then(res => res.flat().map((v) => {
|
|
38
|
+
const op = ctx.operations.flat.get(v.name);
|
|
39
|
+
if (op === undefined || op.protocol !== "http") {
|
|
40
|
+
return v;
|
|
41
|
+
}
|
|
48
42
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
).then(arr => uniqBy(arr, v => v.name));
|
|
43
|
+
return {
|
|
44
|
+
...v,
|
|
45
|
+
method: op.function.method,
|
|
46
|
+
path: op.function.path,
|
|
47
|
+
tags: op.function.tags,
|
|
48
|
+
};
|
|
49
|
+
})).then(arr => uniqBy(arr, v => v.name));
|
|
57
50
|
|
|
58
51
|
if (toolList.length === 0) {
|
|
59
52
|
return [];
|
package/src/strategy/index.ts
CHANGED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { AgenticaContext, AgenticaOperation } from "@agentica/core";
|
|
2
|
+
import type { ILlmSchema } from "@samchon/openapi";
|
|
3
|
+
import type { Database } from "better-sqlite3";
|
|
4
|
+
import type { Cohere } from "cohere-ai";
|
|
5
|
+
|
|
6
|
+
import { CohereClientV2 } from "cohere-ai";
|
|
7
|
+
import { load } from "sqlite-vec";
|
|
8
|
+
|
|
9
|
+
import type { IAgenticaVectorSelectorStrategy } from "..";
|
|
10
|
+
|
|
11
|
+
import { generateHashFromCtx, getRetry, groupByArray } from "../utils";
|
|
12
|
+
|
|
13
|
+
export interface IAgenticaSqliteVectorSelectorStrategyProps {
|
|
14
|
+
db: Database;
|
|
15
|
+
cohereApiKey: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const retry = getRetry(3);
|
|
19
|
+
const hashMemo = new Map<object, string>();
|
|
20
|
+
export function configureSqliteStrategy<SchemaModel extends ILlmSchema.Model>(props: IAgenticaSqliteVectorSelectorStrategyProps): IAgenticaVectorSelectorStrategy<SchemaModel> {
|
|
21
|
+
const { db, cohereApiKey } = props;
|
|
22
|
+
load(db);
|
|
23
|
+
|
|
24
|
+
const cohere = new CohereClientV2({
|
|
25
|
+
token: cohereApiKey,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
db.exec(`
|
|
29
|
+
CREATE TABLE IF NOT EXISTS _agentica_vector_selector_embeddings (
|
|
30
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
31
|
+
hash TEXT NOT NULL,
|
|
32
|
+
name TEXT NOT NULL,
|
|
33
|
+
description TEXT,
|
|
34
|
+
vector BLOB NOT NULL
|
|
35
|
+
)
|
|
36
|
+
`);
|
|
37
|
+
|
|
38
|
+
async function embed(text: string, inputType: Cohere.EmbedInputType) {
|
|
39
|
+
const result = await retry(async () => cohere.embed({
|
|
40
|
+
texts: [text],
|
|
41
|
+
inputType,
|
|
42
|
+
model: "embed-multilingual-light-v3.0",
|
|
43
|
+
embeddingTypes: ["float"],
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
if ((result.embeddings.float == null) || result.embeddings.float.length === 0) {
|
|
47
|
+
throw new Error("no float embeddings returned");
|
|
48
|
+
}
|
|
49
|
+
const vector = result.embeddings.float[0]!;
|
|
50
|
+
return vector;
|
|
51
|
+
}
|
|
52
|
+
// it's memoized to avoid generating the same hash for the same context
|
|
53
|
+
// if you know react, it's like useMemo
|
|
54
|
+
const getHash = (ctx: AgenticaContext<SchemaModel>) => {
|
|
55
|
+
if (hashMemo.has(ctx)) {
|
|
56
|
+
return hashMemo.get(ctx)!;
|
|
57
|
+
}
|
|
58
|
+
const hash = generateHashFromCtx(ctx);
|
|
59
|
+
hashMemo.set(ctx, hash);
|
|
60
|
+
return hash;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// embed each operation in the context.opersation.array
|
|
64
|
+
async function embedOperation(props: {
|
|
65
|
+
hash: string;
|
|
66
|
+
operation: AgenticaOperation<SchemaModel>;
|
|
67
|
+
}): Promise<void> {
|
|
68
|
+
const name = props.operation.function.name;
|
|
69
|
+
|
|
70
|
+
const embedding = await retry(async () => embed(props.operation.function.description ?? name, "search_document"));
|
|
71
|
+
|
|
72
|
+
db
|
|
73
|
+
|
|
74
|
+
.prepare("INSERT INTO _agentica_vector_selector_embeddings (hash, name, description, vector) VALUES (?, ?, ?, vec_f32(?))")
|
|
75
|
+
|
|
76
|
+
.run(props.hash, name, props.operation.function.description, JSON.stringify(embedding));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function embedContext(props: {
|
|
80
|
+
ctx: AgenticaContext<SchemaModel>;
|
|
81
|
+
setEmbedded: () => void;
|
|
82
|
+
}): Promise<void> {
|
|
83
|
+
const hash = getHash(props.ctx);
|
|
84
|
+
|
|
85
|
+
const prepared = db.prepare(`SELECT name FROM _agentica_vector_selector_embeddings WHERE hash = ?`).all(hash);
|
|
86
|
+
if (prepared.length > 0) {
|
|
87
|
+
props.setEmbedded();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
await groupByArray(props.ctx.operations.array, 10).reduce(async (accPromise, cur) => {
|
|
92
|
+
await accPromise;
|
|
93
|
+
await Promise.all(cur.map(async v => embedOperation({ hash, operation: v })));
|
|
94
|
+
return Promise.resolve();
|
|
95
|
+
}, Promise.resolve());
|
|
96
|
+
props.setEmbedded();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function searchTool(ctx: AgenticaContext<SchemaModel>, query: string): Promise<{
|
|
100
|
+
name: string;
|
|
101
|
+
description: string | undefined;
|
|
102
|
+
}[]> {
|
|
103
|
+
const hash = getHash(ctx);
|
|
104
|
+
const vector = await embed(query, "search_query");
|
|
105
|
+
|
|
106
|
+
const result = db.prepare(`
|
|
107
|
+
SELECT name, description, vec_distance_L2(vector, ?) as distance
|
|
108
|
+
FROM _agentica_vector_selector_embeddings
|
|
109
|
+
WHERE hash = ?
|
|
110
|
+
ORDER BY distance
|
|
111
|
+
LIMIT 10
|
|
112
|
+
`).all(JSON.stringify(vector), hash) as {
|
|
113
|
+
name: string;
|
|
114
|
+
description: string;
|
|
115
|
+
distance: number;
|
|
116
|
+
}[];
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
searchTool,
|
|
122
|
+
embedContext,
|
|
123
|
+
};
|
|
124
|
+
}
|
package/src/utils.test.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
+
|
|
3
|
+
import { generateHashFromCtx, getRetry, groupByArray, uniqBy } from "./utils";
|
|
2
4
|
|
|
3
5
|
describe("getRetry", () => {
|
|
4
6
|
it("should throw error when count is less than 1", () => {
|
|
@@ -183,3 +185,173 @@ describe("groupByArray", () => {
|
|
|
183
185
|
expect(result[9]?.length).toBe(100);
|
|
184
186
|
});
|
|
185
187
|
});
|
|
188
|
+
|
|
189
|
+
describe("uniqBy", () => {
|
|
190
|
+
it("should remove duplicates based on selector function", () => {
|
|
191
|
+
const users = [
|
|
192
|
+
{ id: 1, name: "John" },
|
|
193
|
+
{ id: 2, name: "Jane" },
|
|
194
|
+
{ id: 1, name: "John" },
|
|
195
|
+
];
|
|
196
|
+
|
|
197
|
+
const uniqueUsers = uniqBy(users, user => user.id);
|
|
198
|
+
expect(uniqueUsers).toEqual([
|
|
199
|
+
{ id: 1, name: "John" },
|
|
200
|
+
{ id: 2, name: "Jane" },
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("should handle empty array", () => {
|
|
205
|
+
const result = uniqBy([], item => item);
|
|
206
|
+
expect(result).toEqual([]);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("should handle array with no duplicates", () => {
|
|
210
|
+
const numbers = [1, 2, 3, 4, 5];
|
|
211
|
+
const result = uniqBy(numbers, num => num);
|
|
212
|
+
expect(result).toEqual([1, 2, 3, 4, 5]);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("should handle complex objects with custom selector", () => {
|
|
216
|
+
const items = [
|
|
217
|
+
{ id: 1, data: { value: "a" } },
|
|
218
|
+
{ id: 2, data: { value: "b" } },
|
|
219
|
+
{ id: 1, data: { value: "c" } },
|
|
220
|
+
];
|
|
221
|
+
|
|
222
|
+
const result = uniqBy(items, item => item.id);
|
|
223
|
+
expect(result).toEqual([
|
|
224
|
+
{ id: 1, data: { value: "a" } },
|
|
225
|
+
{ id: 2, data: { value: "b" } },
|
|
226
|
+
]);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe("generateHashFromCtx", () => {
|
|
231
|
+
it("should generate consistent hash for same context", () => {
|
|
232
|
+
const ctx = {
|
|
233
|
+
operations: {
|
|
234
|
+
array: [
|
|
235
|
+
{ type: "operation1", data: "test1" },
|
|
236
|
+
{ type: "operation2", data: "test2" },
|
|
237
|
+
],
|
|
238
|
+
},
|
|
239
|
+
config: {},
|
|
240
|
+
histories: [],
|
|
241
|
+
stack: [],
|
|
242
|
+
prompt: "",
|
|
243
|
+
model: {},
|
|
244
|
+
tools: [],
|
|
245
|
+
messages: [],
|
|
246
|
+
state: {},
|
|
247
|
+
ready: true,
|
|
248
|
+
dispatch: () => {},
|
|
249
|
+
request: async () => Promise.resolve({}),
|
|
250
|
+
initialize: async () => Promise.resolve(),
|
|
251
|
+
} as unknown as AgenticaContext<any>;
|
|
252
|
+
|
|
253
|
+
const hash1 = generateHashFromCtx(ctx);
|
|
254
|
+
const hash2 = generateHashFromCtx(ctx);
|
|
255
|
+
expect(hash1).toBe(hash2);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("should generate different hashes for different contexts", () => {
|
|
259
|
+
const ctx1 = {
|
|
260
|
+
operations: {
|
|
261
|
+
array: [{ type: "operation1", data: "test1" }],
|
|
262
|
+
},
|
|
263
|
+
config: {},
|
|
264
|
+
histories: [],
|
|
265
|
+
stack: [],
|
|
266
|
+
prompt: "",
|
|
267
|
+
model: {},
|
|
268
|
+
tools: [],
|
|
269
|
+
messages: [],
|
|
270
|
+
state: {},
|
|
271
|
+
ready: true,
|
|
272
|
+
dispatch: () => {},
|
|
273
|
+
request: async () => Promise.resolve({}),
|
|
274
|
+
initialize: async () => Promise.resolve(),
|
|
275
|
+
} as unknown as AgenticaContext<any>;
|
|
276
|
+
|
|
277
|
+
const ctx2 = {
|
|
278
|
+
operations: {
|
|
279
|
+
array: [{ type: "operation1", data: "test2" }],
|
|
280
|
+
},
|
|
281
|
+
config: {},
|
|
282
|
+
histories: [],
|
|
283
|
+
stack: [],
|
|
284
|
+
prompt: "",
|
|
285
|
+
model: {},
|
|
286
|
+
tools: [],
|
|
287
|
+
messages: [],
|
|
288
|
+
state: {},
|
|
289
|
+
ready: true,
|
|
290
|
+
dispatch: () => {},
|
|
291
|
+
request: async () => Promise.resolve({}),
|
|
292
|
+
initialize: async () => Promise.resolve(),
|
|
293
|
+
} as unknown as AgenticaContext<any>;
|
|
294
|
+
|
|
295
|
+
const hash1 = generateHashFromCtx(ctx1);
|
|
296
|
+
const hash2 = generateHashFromCtx(ctx2);
|
|
297
|
+
expect(hash1).not.toBe(hash2);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("should handle empty operations array", () => {
|
|
301
|
+
const ctx = {
|
|
302
|
+
operations: {
|
|
303
|
+
array: [],
|
|
304
|
+
},
|
|
305
|
+
config: {},
|
|
306
|
+
histories: [],
|
|
307
|
+
stack: [],
|
|
308
|
+
prompt: "",
|
|
309
|
+
model: {},
|
|
310
|
+
tools: [],
|
|
311
|
+
messages: [],
|
|
312
|
+
state: {},
|
|
313
|
+
ready: true,
|
|
314
|
+
dispatch: () => {},
|
|
315
|
+
request: async () => Promise.resolve({}),
|
|
316
|
+
initialize: async () => Promise.resolve(),
|
|
317
|
+
} as unknown as AgenticaContext<any>;
|
|
318
|
+
|
|
319
|
+
const hash = generateHashFromCtx(ctx);
|
|
320
|
+
expect(typeof hash).toBe("string");
|
|
321
|
+
expect(hash.length).toBeGreaterThan(0);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("should handle complex operation objects", () => {
|
|
325
|
+
const ctx = {
|
|
326
|
+
operations: {
|
|
327
|
+
array: [
|
|
328
|
+
{
|
|
329
|
+
type: "complex",
|
|
330
|
+
data: {
|
|
331
|
+
nested: {
|
|
332
|
+
value: 123,
|
|
333
|
+
array: [1, 2, 3],
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
],
|
|
338
|
+
},
|
|
339
|
+
config: {},
|
|
340
|
+
histories: [],
|
|
341
|
+
stack: [],
|
|
342
|
+
prompt: "",
|
|
343
|
+
model: {},
|
|
344
|
+
tools: [],
|
|
345
|
+
messages: [],
|
|
346
|
+
state: {},
|
|
347
|
+
ready: true,
|
|
348
|
+
dispatch: () => {},
|
|
349
|
+
request: async () => Promise.resolve({}),
|
|
350
|
+
initialize: async () => Promise.resolve(),
|
|
351
|
+
} as unknown as AgenticaContext<any>;
|
|
352
|
+
|
|
353
|
+
const hash = generateHashFromCtx(ctx);
|
|
354
|
+
expect(typeof hash).toBe("string");
|
|
355
|
+
expect(hash.length).toBeGreaterThan(0);
|
|
356
|
+
});
|
|
357
|
+
});
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import type { AgenticaContext } from "@agentica/core";
|
|
2
|
+
import type { ILlmSchema } from "@samchon/openapi";
|
|
1
3
|
import type { GreaterThan, Integer } from "type-fest";
|
|
2
4
|
|
|
5
|
+
import { sha256 } from "@noble/hashes/sha2";
|
|
6
|
+
import { utf8ToBytes } from "@noble/hashes/utils";
|
|
7
|
+
|
|
3
8
|
/** type function to check if a number is greater than 0 */
|
|
4
9
|
type GreaterThanZeroInteger<T extends number> = GreaterThan<Integer<T>, 0> extends true ? T : never;
|
|
5
10
|
|
|
@@ -103,3 +108,17 @@ export function uniqBy<T, K>(array: T[], selector: (item: T) => K): T[] {
|
|
|
103
108
|
return true;
|
|
104
109
|
});
|
|
105
110
|
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Generates a hash from an Agentica ctx.operations.array.
|
|
114
|
+
*
|
|
115
|
+
* @param ctx - The Agentica context to generate a hash from
|
|
116
|
+
* @returns A hash of the Agentica context
|
|
117
|
+
*/
|
|
118
|
+
export function generateHashFromCtx<SchemaModel extends ILlmSchema.Model>(ctx: AgenticaContext<SchemaModel>): string {
|
|
119
|
+
const target = JSON.stringify(ctx.operations.array);
|
|
120
|
+
const bytes = utf8ToBytes(target);
|
|
121
|
+
const hash = sha256(bytes);
|
|
122
|
+
const binary = String.fromCharCode(...hash);
|
|
123
|
+
return btoa(binary);
|
|
124
|
+
}
|
package/lib/embed.d.ts
DELETED
|
File without changes
|
package/lib/embed.js
DELETED
package/lib/embed.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"embed.js","sourceRoot":"","sources":["../src/embed.ts"],"names":[],"mappings":""}
|
package/src/embed.ts
DELETED
|
File without changes
|