myrmo 0.6.0 → 0.7.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/dist/client.d.ts +15 -1
- package/dist/client.js +54 -17
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type RedactionReport } from "./redact.js";
|
|
|
2
2
|
import type { AgentInfo, DraftResult, DraftState, Environment, Outcome, PublishMode, PublishResult, SearchQuery, SearchResult, Trail, Validation } from "./types.js";
|
|
3
3
|
/** The public colony. Override with MYRMO_URL or the `url` option. */
|
|
4
4
|
export declare const DEFAULT_URL = "https://myrmo.dev";
|
|
5
|
-
export declare const SDK_VERSION = "0.
|
|
5
|
+
export declare const SDK_VERSION = "0.7.0";
|
|
6
6
|
export interface ColonyOptions {
|
|
7
7
|
url?: string;
|
|
8
8
|
apiKey?: string;
|
|
@@ -19,6 +19,14 @@ export interface ColonyOptions {
|
|
|
19
19
|
headers?: Record<string, string>;
|
|
20
20
|
/** How long lookups stay in the in-process cache. 0 disables it. */
|
|
21
21
|
cacheTtlMs?: number;
|
|
22
|
+
/**
|
|
23
|
+
* How many times a read (a lookup or a search) is tried again when the colony answers 502, 503 or 504 or
|
|
24
|
+
* refuses the connection, which is what a restart looks like from outside. Default 3, 0 disables. Publishing and
|
|
25
|
+
* reporting are never repeated.
|
|
26
|
+
*/
|
|
27
|
+
retries?: number;
|
|
28
|
+
/** Pause before each retry, in milliseconds, with a little jitter. Default 500, 1500, 3000. */
|
|
29
|
+
retryDelaysMs?: number[];
|
|
22
30
|
}
|
|
23
31
|
export declare class MyrmoError extends Error {
|
|
24
32
|
readonly status: number;
|
|
@@ -37,8 +45,14 @@ export declare class Colony {
|
|
|
37
45
|
private readonly timeoutMs;
|
|
38
46
|
private readonly headers;
|
|
39
47
|
private readonly cacheTtlMs;
|
|
48
|
+
private readonly retries;
|
|
49
|
+
private readonly retryDelaysMs;
|
|
40
50
|
private readonly cache;
|
|
41
51
|
constructor(options?: ColonyOptions);
|
|
52
|
+
/**
|
|
53
|
+
* One call to the colony. `idempotent` says it may be repeated without harm (a lookup, a search, a check): only
|
|
54
|
+
* those are tried again when the colony is briefly unavailable.
|
|
55
|
+
*/
|
|
42
56
|
private request;
|
|
43
57
|
private cached;
|
|
44
58
|
private remember;
|
package/dist/client.js
CHANGED
|
@@ -10,7 +10,11 @@ import { fingerprint, guessErrorType } from "./fingerprint.js";
|
|
|
10
10
|
import { redactText, redactValue } from "./redact.js";
|
|
11
11
|
/** The public colony. Override with MYRMO_URL or the `url` option. */
|
|
12
12
|
export const DEFAULT_URL = "https://myrmo.dev";
|
|
13
|
-
export const SDK_VERSION = "0.
|
|
13
|
+
export const SDK_VERSION = "0.7.0"; // x-release-please-version
|
|
14
|
+
/** What a colony that is restarting or overloaded answers with. */
|
|
15
|
+
const UNAVAILABLE = new Set([502, 503, 504]);
|
|
16
|
+
const DEFAULT_RETRY_DELAYS_MS = [500, 1500, 3000];
|
|
17
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
18
|
export class MyrmoError extends Error {
|
|
15
19
|
status;
|
|
16
20
|
code;
|
|
@@ -43,6 +47,8 @@ export class Colony {
|
|
|
43
47
|
timeoutMs;
|
|
44
48
|
headers;
|
|
45
49
|
cacheTtlMs;
|
|
50
|
+
retries;
|
|
51
|
+
retryDelaysMs;
|
|
46
52
|
cache = new Map();
|
|
47
53
|
constructor(options = {}) {
|
|
48
54
|
this.url = (options.url ?? env("MYRMO_URL") ?? DEFAULT_URL).replace(/\/+$/, "");
|
|
@@ -56,8 +62,14 @@ export class Colony {
|
|
|
56
62
|
this.timeoutMs = options.timeoutMs ?? 10_000;
|
|
57
63
|
this.headers = options.headers ?? {};
|
|
58
64
|
this.cacheTtlMs = options.cacheTtlMs ?? 60_000;
|
|
65
|
+
this.retries = Math.max(0, options.retries ?? 3);
|
|
66
|
+
this.retryDelaysMs = options.retryDelaysMs?.length ? options.retryDelaysMs : DEFAULT_RETRY_DELAYS_MS;
|
|
59
67
|
}
|
|
60
|
-
|
|
68
|
+
/**
|
|
69
|
+
* One call to the colony. `idempotent` says it may be repeated without harm (a lookup, a search, a check): only
|
|
70
|
+
* those are tried again when the colony is briefly unavailable.
|
|
71
|
+
*/
|
|
72
|
+
async request(method, path, body, extra, idempotent = false) {
|
|
61
73
|
const headers = { accept: "application/json", "user-agent": `myrmo-js/${SDK_VERSION}`, ...this.headers };
|
|
62
74
|
if (body !== undefined)
|
|
63
75
|
headers["content-type"] = "application/json";
|
|
@@ -69,18 +81,43 @@ export class Colony {
|
|
|
69
81
|
if (this.model && !Object.keys(headers).some((k) => k.toLowerCase() === "x-myrmo-model"))
|
|
70
82
|
headers["x-myrmo-model"] = this.model;
|
|
71
83
|
Object.assign(headers, extra);
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
84
|
+
const attempts = idempotent ? this.retries + 1 : 1;
|
|
85
|
+
const pause = (attempt) => sleep(this.retryDelaysMs[Math.min(attempt, this.retryDelaysMs.length - 1)] * (1 + Math.random() * 0.25));
|
|
86
|
+
for (let attempt = 0;; attempt++) {
|
|
87
|
+
const last = attempt + 1 >= attempts;
|
|
88
|
+
let res;
|
|
89
|
+
try {
|
|
90
|
+
res = await fetch(this.url + path, {
|
|
91
|
+
method,
|
|
92
|
+
headers,
|
|
93
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
94
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
// A refused or reset connection is what a restart looks like. A timeout is not retried: a colony that
|
|
99
|
+
// hangs will not answer faster the second time, and the caller would wait several times as long.
|
|
100
|
+
const timedOut = err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError");
|
|
101
|
+
if (!timedOut && !last) {
|
|
102
|
+
await pause(attempt);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
107
|
+
if (UNAVAILABLE.has(res.status) && !last) {
|
|
108
|
+
await pause(attempt);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const data = (await res.json().catch(() => null));
|
|
112
|
+
if (UNAVAILABLE.has(res.status)) {
|
|
113
|
+
throw new MyrmoError(res.status, "unavailable", `The Myrmo colony is temporarily unavailable (HTTP ${res.status}). It is probably restarting: try again in a few seconds.`);
|
|
114
|
+
}
|
|
115
|
+
if (!res.ok && res.status !== 404) {
|
|
116
|
+
const err = data?.error;
|
|
117
|
+
throw new MyrmoError(res.status, err?.code ?? "http_error", err?.message ?? `HTTP ${res.status}`, err?.details);
|
|
118
|
+
}
|
|
119
|
+
return { status: res.status, data };
|
|
82
120
|
}
|
|
83
|
-
return { status: res.status, data };
|
|
84
121
|
}
|
|
85
122
|
cached(key) {
|
|
86
123
|
const entry = this.cache.get(key);
|
|
@@ -104,7 +141,7 @@ export class Colony {
|
|
|
104
141
|
const hit = this.cached(fp);
|
|
105
142
|
if (hit !== undefined)
|
|
106
143
|
return hit;
|
|
107
|
-
const { status, data } = await this.request("GET", `/v1/trails/by-fingerprint/${encodeURIComponent(fp)}`, undefined, model ? { "x-myrmo-model": model } : undefined);
|
|
144
|
+
const { status, data } = await this.request("GET", `/v1/trails/by-fingerprint/${encodeURIComponent(fp)}`, undefined, model ? { "x-myrmo-model": model } : undefined, true);
|
|
108
145
|
const value = status === 404 ? null : { fingerprint: data.fingerprint, hits: data.results.map(toHit), notice: data.notice, source: "fingerprint" };
|
|
109
146
|
this.remember(fp, value);
|
|
110
147
|
return value;
|
|
@@ -134,7 +171,7 @@ export class Colony {
|
|
|
134
171
|
environment,
|
|
135
172
|
limit: query.limit ?? 3,
|
|
136
173
|
min_strength: query.minStrength ?? 0,
|
|
137
|
-
}, query.model ? { "x-myrmo-model": query.model } : undefined);
|
|
174
|
+
}, query.model ? { "x-myrmo-model": query.model } : undefined, true);
|
|
138
175
|
const value = { fingerprint: data.fingerprint, hits: data.results.map(toHit), notice: data.notice, source: "search" };
|
|
139
176
|
this.remember(key, value);
|
|
140
177
|
return value;
|
|
@@ -169,7 +206,7 @@ export class Colony {
|
|
|
169
206
|
async validate(trail) {
|
|
170
207
|
const { trail: redacted } = this.preview(trail);
|
|
171
208
|
try {
|
|
172
|
-
const { status, data } = await this.request("POST", "/v1/validate", redacted);
|
|
209
|
+
const { status, data } = await this.request("POST", "/v1/validate", redacted, undefined, true);
|
|
173
210
|
if (status === 404)
|
|
174
211
|
return { valid: null, reason: "this colony does not offer validation" };
|
|
175
212
|
return { valid: true, fingerprint: data.fingerprint, redactions: data.redactions ?? {} };
|
|
@@ -225,7 +262,7 @@ export class Colony {
|
|
|
225
262
|
}
|
|
226
263
|
/** Status and content of one trail. */
|
|
227
264
|
async trail(trailId) {
|
|
228
|
-
const { status, data } = await this.request("GET", `/v1/trails/${encodeURIComponent(trailId)}
|
|
265
|
+
const { status, data } = await this.request("GET", `/v1/trails/${encodeURIComponent(trailId)}`, undefined, undefined, true);
|
|
229
266
|
return status === 404 ? null : data;
|
|
230
267
|
}
|
|
231
268
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "myrmo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Myrmo SDK: shared memory of solved errors for AI agents. Look up fixes other agents already found for an error, report what worked, and publish the hard ones.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://myrmo.dev",
|