@alexein/sdk 0.0.0-stage → 0.3.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.
@@ -0,0 +1,101 @@
1
+ import {
2
+ AlexeinError,
3
+ Memory,
4
+ NotFoundError
5
+ } from "./chunk-UNVZ22EN.js";
6
+
7
+ // src/integrations/shared.ts
8
+ var UNAVAILABLE = "Memory is unavailable right now; carry on without it.";
9
+ var NOTHING_FOUND = "Nothing remembered about that.";
10
+ var NOTHING_YET = "Nothing has been remembered for this user yet.";
11
+ var SAVED = "Saved.";
12
+ var HEADING = "What you remember about this user from earlier conversations. Use it where it helps; the conversation itself takes precedence where they differ.";
13
+ var MAX_QUERY = 2e3;
14
+ var TIMEOUT = 1e4;
15
+ var LazyClient = class {
16
+ constructor(given, integration, strict, timeout = TIMEOUT) {
17
+ this.integration = integration;
18
+ this.strict = strict;
19
+ this.timeout = timeout;
20
+ this.#client = given;
21
+ }
22
+ integration;
23
+ strict;
24
+ timeout;
25
+ #client;
26
+ get client() {
27
+ this.#client ??= new Memory({
28
+ failOpen: !this.strict,
29
+ integration: this.integration,
30
+ timeout: this.timeout
31
+ });
32
+ return this.#client;
33
+ }
34
+ };
35
+ function isClient(given) {
36
+ return typeof given?.user === "function";
37
+ }
38
+ async function searchFor(user, query, fast) {
39
+ try {
40
+ return await user.search(query.slice(-MAX_QUERY), { fast });
41
+ } catch (error) {
42
+ if (error instanceof NotFoundError) return void 0;
43
+ throw error;
44
+ }
45
+ }
46
+ function searchAnswer(found, maxChars) {
47
+ if (found?.failed) return UNAVAILABLE;
48
+ const said = found?.context(maxChars) ?? "";
49
+ if (!found || !said) return NOTHING_FOUND;
50
+ const refs = [...new Set(found.refs)];
51
+ if (!refs.length) return said;
52
+ return `${said}
53
+
54
+ To read one whole, memory_open:
55
+ ${refs.map((r) => `- ${r}`).join("\n")}`;
56
+ }
57
+ function described(memory) {
58
+ return memory.type === "excerpt" && memory.date ? `${memory.date.slice(0, 10)}: ${memory.text}` : memory.text;
59
+ }
60
+ function openAnswer(opened) {
61
+ if (!opened.sources.length) return described(opened.memory);
62
+ const from = opened.sources.map((s) => `- ${described(s)}`).join("\n");
63
+ return `${described(opened.memory)}
64
+
65
+ Learned from:
66
+ ${from}`;
67
+ }
68
+ function notFoundAnswer(error) {
69
+ return /\bref\b/.test(error.detail) ? "Memory holds nothing by that ref." : NOTHING_YET;
70
+ }
71
+ function outageAnswer(error, strict) {
72
+ if (strict || !(error instanceof AlexeinError) || !error.outage) throw error;
73
+ console.warn(`Alexein is unavailable: ${error.message}`);
74
+ return UNAVAILABLE;
75
+ }
76
+ function promptBlock(found) {
77
+ return found ? `${HEADING}
78
+
79
+ ${found}` : "";
80
+ }
81
+ async function stableId(...parts) {
82
+ const bytes = new TextEncoder().encode(parts.map((p) => p ?? "").join("|"));
83
+ const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
84
+ return Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("").slice(0, 24);
85
+ }
86
+
87
+ export {
88
+ UNAVAILABLE,
89
+ NOTHING_FOUND,
90
+ NOTHING_YET,
91
+ SAVED,
92
+ LazyClient,
93
+ isClient,
94
+ searchFor,
95
+ searchAnswer,
96
+ openAnswer,
97
+ notFoundAnswer,
98
+ outageAnswer,
99
+ promptBlock,
100
+ stableId
101
+ };
@@ -0,0 +1,101 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+ var _chunk7KPDU3YVcjs = require('./chunk-7KPDU3YV.cjs');
6
+
7
+ // src/integrations/shared.ts
8
+ var UNAVAILABLE = "Memory is unavailable right now; carry on without it.";
9
+ var NOTHING_FOUND = "Nothing remembered about that.";
10
+ var NOTHING_YET = "Nothing has been remembered for this user yet.";
11
+ var SAVED = "Saved.";
12
+ var HEADING = "What you remember about this user from earlier conversations. Use it where it helps; the conversation itself takes precedence where they differ.";
13
+ var MAX_QUERY = 2e3;
14
+ var TIMEOUT = 1e4;
15
+ var LazyClient = class {
16
+ constructor(given, integration, strict, timeout = TIMEOUT) {
17
+ this.integration = integration;
18
+ this.strict = strict;
19
+ this.timeout = timeout;
20
+ this.#client = given;
21
+ }
22
+
23
+
24
+
25
+ #client;
26
+ get client() {
27
+ this.#client ??= new (0, _chunk7KPDU3YVcjs.Memory)({
28
+ failOpen: !this.strict,
29
+ integration: this.integration,
30
+ timeout: this.timeout
31
+ });
32
+ return this.#client;
33
+ }
34
+ };
35
+ function isClient(given) {
36
+ return typeof _optionalChain([given, 'optionalAccess', _ => _.user]) === "function";
37
+ }
38
+ async function searchFor(user, query, fast) {
39
+ try {
40
+ return await user.search(query.slice(-MAX_QUERY), { fast });
41
+ } catch (error) {
42
+ if (error instanceof _chunk7KPDU3YVcjs.NotFoundError) return void 0;
43
+ throw error;
44
+ }
45
+ }
46
+ function searchAnswer(found, maxChars) {
47
+ if (_optionalChain([found, 'optionalAccess', _2 => _2.failed])) return UNAVAILABLE;
48
+ const said = _nullishCoalesce(_optionalChain([found, 'optionalAccess', _3 => _3.context, 'call', _4 => _4(maxChars)]), () => ( ""));
49
+ if (!found || !said) return NOTHING_FOUND;
50
+ const refs = [...new Set(found.refs)];
51
+ if (!refs.length) return said;
52
+ return `${said}
53
+
54
+ To read one whole, memory_open:
55
+ ${refs.map((r) => `- ${r}`).join("\n")}`;
56
+ }
57
+ function described(memory) {
58
+ return memory.type === "excerpt" && memory.date ? `${memory.date.slice(0, 10)}: ${memory.text}` : memory.text;
59
+ }
60
+ function openAnswer(opened) {
61
+ if (!opened.sources.length) return described(opened.memory);
62
+ const from = opened.sources.map((s) => `- ${described(s)}`).join("\n");
63
+ return `${described(opened.memory)}
64
+
65
+ Learned from:
66
+ ${from}`;
67
+ }
68
+ function notFoundAnswer(error) {
69
+ return /\bref\b/.test(error.detail) ? "Memory holds nothing by that ref." : NOTHING_YET;
70
+ }
71
+ function outageAnswer(error, strict) {
72
+ if (strict || !(error instanceof _chunk7KPDU3YVcjs.AlexeinError) || !error.outage) throw error;
73
+ console.warn(`Alexein is unavailable: ${error.message}`);
74
+ return UNAVAILABLE;
75
+ }
76
+ function promptBlock(found) {
77
+ return found ? `${HEADING}
78
+
79
+ ${found}` : "";
80
+ }
81
+ async function stableId(...parts) {
82
+ const bytes = new TextEncoder().encode(parts.map((p) => _nullishCoalesce(p, () => ( ""))).join("|"));
83
+ const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
84
+ return Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("").slice(0, 24);
85
+ }
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ exports.UNAVAILABLE = UNAVAILABLE; exports.NOTHING_FOUND = NOTHING_FOUND; exports.NOTHING_YET = NOTHING_YET; exports.SAVED = SAVED; exports.LazyClient = LazyClient; exports.isClient = isClient; exports.searchFor = searchFor; exports.searchAnswer = searchAnswer; exports.openAnswer = openAnswer; exports.notFoundAnswer = notFoundAnswer; exports.outageAnswer = outageAnswer; exports.promptBlock = promptBlock; exports.stableId = stableId;