@heyditto/cli 3.22.3 → 3.24.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 +102 -0
- package/dist/api.d.ts +32 -0
- package/dist/api.js +10 -0
- package/dist/api.js.map +1 -1
- package/dist/cli.js +4 -1
- package/dist/cli.js.map +1 -1
- package/dist/gamma/btauth.d.ts +84 -0
- package/dist/gamma/btauth.js +175 -0
- package/dist/gamma/btauth.js.map +1 -0
- package/dist/gamma/client.d.ts +145 -0
- package/dist/gamma/client.js +551 -0
- package/dist/gamma/client.js.map +1 -0
- package/dist/gamma/commands.d.ts +4 -0
- package/dist/gamma/commands.js +194 -0
- package/dist/gamma/commands.js.map +1 -0
- package/dist/gamma/hotkey.d.ts +51 -0
- package/dist/gamma/hotkey.js +241 -0
- package/dist/gamma/hotkey.js.map +1 -0
- package/dist/mcp-refresh-command.d.ts +8 -0
- package/dist/mcp-refresh-command.js +194 -0
- package/dist/mcp-refresh-command.js.map +1 -0
- package/dist/org-commands.js +13 -0
- package/dist/org-commands.js.map +1 -1
- package/dist/tools-commands.js +14 -0
- package/dist/tools-commands.js.map +1 -1
- package/dist/update-lock.json +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bittensor btauth/1 hotkey-signed HTTP requests (bt.http_auth in the
|
|
3
|
+
* Python SDK), the credential of the Gamma Integration Standard V1.3: the
|
|
4
|
+
* signing hotkey is the whole account, so a signed request carries no API key.
|
|
5
|
+
*
|
|
6
|
+
* The signature is over the UTF-8 bytes of exactly eight lines joined by "\n"
|
|
7
|
+
* with no trailing newline: `btauth/1`, the scheme, the uppercase method, the
|
|
8
|
+
* request target exactly as sent (path plus ?query, percent-encoded), the
|
|
9
|
+
* lowercase hex sha256 of the raw body bytes, the nonce (unix nanoseconds,
|
|
10
|
+
* decimal), the sender SS58 and the receiver SS58. sr25519 signs with the
|
|
11
|
+
* `substrate` signing context, ed25519 is plain RFC 8032, and the payload is
|
|
12
|
+
* signed as-is, never `<Bytes>`-wrapped.
|
|
13
|
+
*
|
|
14
|
+
* `verifyRequest` ports the receiving side's rules (shape, receiver,
|
|
15
|
+
* freshness, signature, replay) so tests and local mocks check exactly what
|
|
16
|
+
* Ditto's gateway checks. Nothing here touches the network or stores keys.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash } from "node:crypto";
|
|
19
|
+
import { u8aToHex, hexToU8a } from "@polkadot/util";
|
|
20
|
+
import { decodeAddress, ed25519Verify, sr25519Verify } from "@polkadot/util-crypto";
|
|
21
|
+
export const BTAUTH_TAG = "btauth/1";
|
|
22
|
+
export const BTAUTH_VERSION = "1";
|
|
23
|
+
export const HEADER_VERSION = "X-Bittensor-Version";
|
|
24
|
+
export const HEADER_HOTKEY = "X-Bittensor-Hotkey";
|
|
25
|
+
export const HEADER_RECEIVER = "X-Bittensor-Receiver";
|
|
26
|
+
export const HEADER_NONCE = "X-Bittensor-Nonce";
|
|
27
|
+
export const HEADER_CRYPTO = "X-Bittensor-Crypto";
|
|
28
|
+
export const HEADER_SIGNATURE = "X-Bittensor-Signature";
|
|
29
|
+
/** Lowercase hex sha256 of the raw body bytes (of the empty string when there is no body). */
|
|
30
|
+
export function bodyHash(body) {
|
|
31
|
+
return createHash("sha256").update(body ?? "").digest("hex");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The request target exactly as an HTTP client sends it: the URL's
|
|
35
|
+
* percent-encoded path plus `?query`. Never decoded.
|
|
36
|
+
*/
|
|
37
|
+
export function requestTarget(url) {
|
|
38
|
+
const u = typeof url === "string" ? new URL(url) : url;
|
|
39
|
+
return `${u.pathname}${u.search}`;
|
|
40
|
+
}
|
|
41
|
+
/** The exact signed bytes. */
|
|
42
|
+
export function buildPayload(f) {
|
|
43
|
+
return new TextEncoder().encode([
|
|
44
|
+
BTAUTH_TAG, f.scheme, f.method.toUpperCase(), f.target, f.bodySha256, f.nonce, f.sender, f.receiver,
|
|
45
|
+
].join("\n"));
|
|
46
|
+
}
|
|
47
|
+
let lastNonce = 0n;
|
|
48
|
+
/**
|
|
49
|
+
* A fresh unix-nanosecond nonce, strictly increasing within this process so
|
|
50
|
+
* two signatures in the same millisecond never share one.
|
|
51
|
+
*/
|
|
52
|
+
export function freshNonce(nowMs = Date.now()) {
|
|
53
|
+
const sub = process.hrtime.bigint() % 1000000n;
|
|
54
|
+
let n = BigInt(Math.trunc(nowMs)) * 1000000n + sub;
|
|
55
|
+
if (n <= lastNonce)
|
|
56
|
+
n = lastNonce + 1n;
|
|
57
|
+
lastNonce = n;
|
|
58
|
+
return n.toString();
|
|
59
|
+
}
|
|
60
|
+
/** The btauth/1 headers for one request attempt. Call it again for any retry. */
|
|
61
|
+
export function signRequest(signer, input) {
|
|
62
|
+
if (!input.receiver || input.receiver === "-")
|
|
63
|
+
throw new Error("btauth/1: a receiver hotkey is required");
|
|
64
|
+
const nonce = input.nonce ?? freshNonce();
|
|
65
|
+
const payload = buildPayload({
|
|
66
|
+
scheme: signer.scheme,
|
|
67
|
+
method: input.method,
|
|
68
|
+
target: requestTarget(input.url),
|
|
69
|
+
bodySha256: bodyHash(input.body),
|
|
70
|
+
nonce,
|
|
71
|
+
sender: signer.address,
|
|
72
|
+
receiver: input.receiver,
|
|
73
|
+
});
|
|
74
|
+
const headers = {
|
|
75
|
+
[HEADER_VERSION]: BTAUTH_VERSION,
|
|
76
|
+
[HEADER_HOTKEY]: signer.address,
|
|
77
|
+
[HEADER_RECEIVER]: input.receiver,
|
|
78
|
+
[HEADER_NONCE]: nonce,
|
|
79
|
+
[HEADER_SIGNATURE]: u8aToHex(signer.sign(payload)),
|
|
80
|
+
};
|
|
81
|
+
if (signer.scheme === "ed25519")
|
|
82
|
+
headers[HEADER_CRYPTO] = "ed25519";
|
|
83
|
+
return headers;
|
|
84
|
+
}
|
|
85
|
+
/** Verifies one raw signature under exactly one scheme; never falls back to another. */
|
|
86
|
+
export function verifySignature(scheme, message, signature, address) {
|
|
87
|
+
try {
|
|
88
|
+
const pub = decodeAddress(address);
|
|
89
|
+
return scheme === "ed25519" ? ed25519Verify(message, signature, pub) : sr25519Verify(message, signature, pub);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class BtauthRefusal extends Error {
|
|
96
|
+
kind;
|
|
97
|
+
constructor(kind, message) {
|
|
98
|
+
super(message);
|
|
99
|
+
this.kind = kind;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The receiving side's checks, in the normative order (shape, receiver,
|
|
104
|
+
* freshness, signature, replay), matching Ditto's gateway. Returns the
|
|
105
|
+
* authenticated sender SS58 or throws BtauthRefusal.
|
|
106
|
+
*/
|
|
107
|
+
export function verifyRequest(o) {
|
|
108
|
+
const get = (name) => {
|
|
109
|
+
const v = o.headers[name.toLowerCase()] ?? o.headers[name];
|
|
110
|
+
return Array.isArray(v) ? (v[0] ?? "") : (v ?? "");
|
|
111
|
+
};
|
|
112
|
+
if (get(HEADER_VERSION).trim() !== BTAUTH_VERSION)
|
|
113
|
+
throw new BtauthRefusal("malformed_auth", "X-Bittensor-Version must be 1");
|
|
114
|
+
const crypto = get(HEADER_CRYPTO);
|
|
115
|
+
let scheme;
|
|
116
|
+
if (crypto === "")
|
|
117
|
+
scheme = "sr25519";
|
|
118
|
+
else if (crypto === "ed25519")
|
|
119
|
+
scheme = "ed25519";
|
|
120
|
+
else if (crypto === "hashed" || crypto === "hashed-ed25519")
|
|
121
|
+
throw new BtauthRefusal("unsupported_scheme", "hashed hotkeys are not supported");
|
|
122
|
+
else
|
|
123
|
+
throw new BtauthRefusal("malformed_auth", "X-Bittensor-Crypto must be omitted (sr25519) or ed25519");
|
|
124
|
+
const hotkey = get(HEADER_HOTKEY);
|
|
125
|
+
try {
|
|
126
|
+
decodeAddress(hotkey);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
throw new BtauthRefusal("malformed_auth", "X-Bittensor-Hotkey is not a valid SS58 address");
|
|
130
|
+
}
|
|
131
|
+
const nonceRaw = get(HEADER_NONCE);
|
|
132
|
+
if (!/^[1-9][0-9]{0,18}$/.test(nonceRaw) || BigInt(nonceRaw) > 9223372036854775807n) {
|
|
133
|
+
throw new BtauthRefusal("malformed_auth", "X-Bittensor-Nonce must be unix time in nanoseconds, as a decimal string");
|
|
134
|
+
}
|
|
135
|
+
const sigRaw = get(HEADER_SIGNATURE);
|
|
136
|
+
if (!/^0x[0-9a-fA-F]{128}$/.test(sigRaw))
|
|
137
|
+
throw new BtauthRefusal("malformed_auth", "X-Bittensor-Signature must be 0x followed by 128 hex digits");
|
|
138
|
+
const receiver = get(HEADER_RECEIVER);
|
|
139
|
+
if (receiver === "" || receiver === "-")
|
|
140
|
+
throw new BtauthRefusal("wrong_receiver", "X-Bittensor-Receiver is required and must be this service's hotkey");
|
|
141
|
+
if (o.trusted && o.trusted !== scheme)
|
|
142
|
+
throw new BtauthRefusal("bad_signature", "the signature scheme does not match the one this hotkey first signed with");
|
|
143
|
+
let receiverKey;
|
|
144
|
+
try {
|
|
145
|
+
receiverKey = u8aToHex(decodeAddress(receiver));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
throw new BtauthRefusal("wrong_receiver", "X-Bittensor-Receiver must be this service's hotkey");
|
|
149
|
+
}
|
|
150
|
+
// An undecodable configured receiver is a non-match, never a raw decoder error.
|
|
151
|
+
const matches = (r) => { try {
|
|
152
|
+
return u8aToHex(decodeAddress(r)) === receiverKey;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return false;
|
|
156
|
+
} };
|
|
157
|
+
if (!o.receivers.some(matches)) {
|
|
158
|
+
throw new BtauthRefusal("wrong_receiver", "X-Bittensor-Receiver must be this service's hotkey");
|
|
159
|
+
}
|
|
160
|
+
const nowNs = BigInt(Math.trunc(o.nowMs ?? Date.now())) * 1000000n;
|
|
161
|
+
const signedAt = BigInt(nonceRaw);
|
|
162
|
+
if (nowNs - signedAt > BigInt(o.maxAgeMs ?? 10_000) * 1000000n)
|
|
163
|
+
throw new BtauthRefusal("stale_request", "the request is too old; sign a fresh nonce for every attempt");
|
|
164
|
+
if (signedAt - nowNs > BigInt(o.skewMs ?? 2_000) * 1000000n)
|
|
165
|
+
throw new BtauthRefusal("stale_request", "the request nonce is in the future; check the client clock");
|
|
166
|
+
const payload = buildPayload({ scheme, method: o.method, target: o.target, bodySha256: bodyHash(o.body), nonce: nonceRaw, sender: hotkey, receiver });
|
|
167
|
+
if (!verifySignature(scheme, payload, hexToU8a(sigRaw), hotkey))
|
|
168
|
+
throw new BtauthRefusal("bad_signature", "the signature does not match this request");
|
|
169
|
+
const key = `${u8aToHex(decodeAddress(hotkey))}:${nonceRaw}`;
|
|
170
|
+
if (o.seen.has(key))
|
|
171
|
+
throw new BtauthRefusal("replayed_request", "this request was already accepted; sign a fresh nonce for every attempt");
|
|
172
|
+
o.seen.add(key);
|
|
173
|
+
return { hotkey, scheme };
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=btauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"btauth.js","sourceRoot":"","sources":["../../src/gamma/btauth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEpF,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;AACrC,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAClD,MAAM,CAAC,M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AAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IACtJ,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QAAE,MAAM,IAAI,aAAa,CAAC,eAAe,EAAE,2CAA2C,CAAC,CAAC;IACvJ,MAAM,GAAG,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,IAAI,QAAQ,EAAE,CAAC;IAC7D,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,aAAa,CAAC,kBAAkB,EAAE,yEAAyE,CAAC,CAAC;IAC5I,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { type HotkeySigner } from "./btauth.js";
|
|
2
|
+
export declare const SN118_NETUID = 118;
|
|
3
|
+
/** The services document Ditto's SN118 `set_service` points at. */
|
|
4
|
+
export declare const DEFAULT_SERVICES_URL = "https://api.heyditto.ai/api/v5/chain-gamma/public/services.json";
|
|
5
|
+
export declare const SESSION_HEADER = "X-Ditto-Session-Id";
|
|
6
|
+
/** Exit status when the hotkey's balance cannot pay (HTTP 402). */
|
|
7
|
+
export declare const EXIT_INSUFFICIENT_BALANCE = 3;
|
|
8
|
+
export interface Discovery {
|
|
9
|
+
servicesUrl: string;
|
|
10
|
+
netuid: number;
|
|
11
|
+
name?: string;
|
|
12
|
+
publishers: string[];
|
|
13
|
+
/** The publisher hotkey requests are signed for. */
|
|
14
|
+
receiver: string;
|
|
15
|
+
openapiUrl: string;
|
|
16
|
+
apiBase: string;
|
|
17
|
+
balanceUrl: string;
|
|
18
|
+
pricingUrl?: string;
|
|
19
|
+
paths: string[];
|
|
20
|
+
/** The user passed --allow-untrusted-origin: hosts outside heyditto.ai may be signed for. */
|
|
21
|
+
allowUntrustedOrigin: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare class GammaHTTPError extends Error {
|
|
24
|
+
readonly status: number;
|
|
25
|
+
readonly code?: string | undefined;
|
|
26
|
+
readonly exitCode: number;
|
|
27
|
+
constructor(message: string, status: number, code?: string | undefined, exitCode?: number);
|
|
28
|
+
}
|
|
29
|
+
/** HTTP 402: the hotkey's balance is empty or the hotkey has never spent gamma here. */
|
|
30
|
+
export declare class InsufficientBalanceError extends GammaHTTPError {
|
|
31
|
+
readonly serverMessage: string;
|
|
32
|
+
constructor(message: string, serverMessage: string);
|
|
33
|
+
}
|
|
34
|
+
/** Discovered URLs must be https (plain http only for a loopback test server). */
|
|
35
|
+
export declare function checkedURL(raw: unknown, what: string, base?: string): URL;
|
|
36
|
+
export interface DiscoverOptions {
|
|
37
|
+
servicesUrl?: string;
|
|
38
|
+
netuid?: number;
|
|
39
|
+
/** A specific publisher hotkey to sign for; it must be listed. */
|
|
40
|
+
receiver?: string;
|
|
41
|
+
/** Allow signing for (and reading balances from) hosts outside heyditto.ai. */
|
|
42
|
+
allowUntrustedOrigin?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Env var equivalent of --allow-untrusted-origin. */
|
|
45
|
+
export declare const ALLOW_UNTRUSTED_ORIGIN_ENV = "DITTO_GAMMA_ALLOW_UNTRUSTED_ORIGIN";
|
|
46
|
+
/** The only origins requests are signed for by default: https heyditto.ai and its subdomains. */
|
|
47
|
+
export declare function isTrustedOrigin(url: URL | string): boolean;
|
|
48
|
+
export declare class UntrustedOriginError extends Error {
|
|
49
|
+
}
|
|
50
|
+
/** Fetches and validates the services document and the OpenAPI spec it links. */
|
|
51
|
+
export declare function discover(o?: DiscoverOptions, env?: NodeJS.ProcessEnv): Promise<Discovery>;
|
|
52
|
+
export interface Balance {
|
|
53
|
+
hotkey: string;
|
|
54
|
+
credited_usd: string;
|
|
55
|
+
used_usd: string;
|
|
56
|
+
remaining_usd: string;
|
|
57
|
+
}
|
|
58
|
+
/** GET <balance>?hotkey=<ss58>. Public; an unknown hotkey answers zeros. */
|
|
59
|
+
export declare function fetchBalance(d: Discovery, hotkey: string): Promise<Balance>;
|
|
60
|
+
/** The btcli commands that fund a hotkey at Ditto. */
|
|
61
|
+
export declare function fundingHelp(d: Pick<Discovery, "netuid" | "receiver">, hotkeyName: string): string;
|
|
62
|
+
export interface SignedClient {
|
|
63
|
+
discovery: Discovery;
|
|
64
|
+
signer: HotkeySigner;
|
|
65
|
+
/** The btcli hotkey name for hints (falls back to the SS58). */
|
|
66
|
+
hotkeyName: string;
|
|
67
|
+
}
|
|
68
|
+
export interface SignedResponse {
|
|
69
|
+
status: number;
|
|
70
|
+
headers: Headers;
|
|
71
|
+
text: string;
|
|
72
|
+
body?: Response;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* One btauth/1-signed request. Signs the exact bytes sent with a fresh nonce;
|
|
76
|
+
* a caller that retries calls this again, so every attempt is re-signed.
|
|
77
|
+
*/
|
|
78
|
+
export declare function signedFetch(c: SignedClient, method: string, url: URL, body?: unknown, extra?: Record<string, string>, stream?: boolean, signal?: AbortSignal): Promise<SignedResponse>;
|
|
79
|
+
export declare function apiURL(d: Discovery, pathname: string): URL;
|
|
80
|
+
/** Signed GET /v1/models. */
|
|
81
|
+
export declare function listModels(c: SignedClient): Promise<{
|
|
82
|
+
raw: unknown;
|
|
83
|
+
ids: string[];
|
|
84
|
+
}>;
|
|
85
|
+
/** The pricing document's default model, the model a request should name when the user named none. */
|
|
86
|
+
export declare function defaultModel(d: Discovery): Promise<string | undefined>;
|
|
87
|
+
export declare function validSessionId(id: string): boolean;
|
|
88
|
+
/** A streamed answer may take this long in all (15 minutes) by default. */
|
|
89
|
+
export declare const DEFAULT_STREAM_TOTAL_MS = 900000;
|
|
90
|
+
/** A streamed answer may go this long without a byte (2 minutes) by default. */
|
|
91
|
+
export declare const DEFAULT_STREAM_IDLE_MS = 120000;
|
|
92
|
+
export interface StreamLimits {
|
|
93
|
+
totalMs: number;
|
|
94
|
+
idleMs: number;
|
|
95
|
+
}
|
|
96
|
+
/** Node's setTimeout maximum; a longer delay silently fires after 1 ms. */
|
|
97
|
+
export declare const MAX_TIMER_MS = 2147483647;
|
|
98
|
+
/** Throws unless ms is a whole number of milliseconds a timer can hold (1 to MAX_TIMER_MS). */
|
|
99
|
+
export declare function checkTimerMs(ms: number, what: string): number;
|
|
100
|
+
/**
|
|
101
|
+
* A timer limit given in `unit` (1 for milliseconds, 1000 for seconds), as
|
|
102
|
+
* whole milliseconds. A value below 1 ms or above the timer maximum is an
|
|
103
|
+
* error rather than a silent 0 or overflow.
|
|
104
|
+
*/
|
|
105
|
+
export declare function parseTimerLimit(raw: string, unitMs: number, what: string): number;
|
|
106
|
+
/** Stream limits from DITTO_GAMMA_STREAM_TIMEOUT_MS and DITTO_GAMMA_STREAM_IDLE_MS, or the defaults. */
|
|
107
|
+
export declare function streamLimits(env?: NodeJS.ProcessEnv): StreamLimits;
|
|
108
|
+
export declare class StreamTimeoutError extends Error {
|
|
109
|
+
}
|
|
110
|
+
export interface ChatOptions {
|
|
111
|
+
model: string;
|
|
112
|
+
prompt: string;
|
|
113
|
+
session: string;
|
|
114
|
+
stream: boolean;
|
|
115
|
+
/** Overall and between-chunk limits for a streamed answer (default: streamLimits()). */
|
|
116
|
+
limits?: StreamLimits;
|
|
117
|
+
/** Receives streamed text as it arrives. */
|
|
118
|
+
onDelta?: (text: string) => void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Signed POST /v1/chat/completions. The session header groups turns into one
|
|
122
|
+
* thread of the hotkey account, so repeated chats build that account's history.
|
|
123
|
+
*/
|
|
124
|
+
export declare function chat(c: SignedClient, o: ChatOptions): Promise<{
|
|
125
|
+
text: string;
|
|
126
|
+
raw?: unknown;
|
|
127
|
+
model?: string;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Yields each SSE event's joined `data:` lines. When the consumer stops early
|
|
131
|
+
* (a `[DONE]`, an error, a timeout) the body is cancelled, so an SSE response
|
|
132
|
+
* the server leaves open never keeps the request alive. `idleMs` bounds the
|
|
133
|
+
* wait for each chunk.
|
|
134
|
+
*/
|
|
135
|
+
export declare function sseData(res: Response, opts?: {
|
|
136
|
+
idleMs?: number;
|
|
137
|
+
}): AsyncGenerator<string>;
|
|
138
|
+
export declare const MEMORY_SEARCH_TOOL = "search_memories";
|
|
139
|
+
export declare class MemoryToolUnavailableError extends Error {
|
|
140
|
+
}
|
|
141
|
+
/** Searches the hotkey account's memories with the Router's memory search tool. */
|
|
142
|
+
export declare function searchMemories(c: SignedClient, query: string, limit?: number): Promise<{
|
|
143
|
+
text: string;
|
|
144
|
+
raw: unknown;
|
|
145
|
+
}>;
|