@polycode-projects/the-mechanical-code-talker 6.0.0 → 6.0.1
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 +27 -0
- package/bin/tmct.mjs +61 -0
- package/package.json +1 -1
- package/src/adapters/memory/core.mjs +522 -146
- package/src/adapters/memory/corpus-bands.mjs +155 -0
- package/src/adapters/memory/dynamo-circuit-breaker.mjs +266 -0
- package/src/adapters/memory/systemic-failure.mjs +28 -0
- package/src/domain/ask.mjs +341 -75
- package/src/domain/cli-verbs.mjs +17 -0
- package/src/domain/news-feed.mjs +42 -4
- package/src/domain/retrieval-marker.mjs +120 -0
- package/src/domain/retrieval-modes.mjs +13 -0
- package/src/services/chat-session.mjs +13 -2
- package/src/services/chat.mjs +73 -16
- package/src/services/corpus-loader.mjs +222 -0
- package/src/services/subgraph-retrieval.mjs +4 -33
- package/src/surfaces/web/http-row-backend.mjs +150 -0
- package/src/surfaces/web/memory-ask-browser.bundle.js +120 -117
|
@@ -16,7 +16,10 @@ import {
|
|
|
16
16
|
buildRetrievalPlan, expandedTerms, ancestryTerms,
|
|
17
17
|
} from "../domain/retrieval-plan.mjs";
|
|
18
18
|
import { loadLexicon } from "../domain/grammar/lexicon.mjs";
|
|
19
|
-
import {
|
|
19
|
+
import { isSystemicFailure } from "../adapters/memory/systemic-failure.mjs";
|
|
20
|
+
import { SUPPLEMENTED_MODE, SEED_SESSION_MODE } from "../domain/retrieval-modes.mjs";
|
|
21
|
+
|
|
22
|
+
export { isSystemicFailure, SUPPLEMENTED_MODE, SEED_SESSION_MODE };
|
|
20
23
|
|
|
21
24
|
/** The retrieval budgets, set from what scripts/corpus-bands/calibrate-retrieval.mjs
|
|
22
25
|
* measured over the committed WordNet and ConceptNet corpora. Changing one is
|
|
@@ -64,38 +67,6 @@ export const RETRIEVAL_BUDGETS = Object.freeze({
|
|
|
64
67
|
backoffBaseMs: 25,
|
|
65
68
|
});
|
|
66
69
|
|
|
67
|
-
/** Retrieval ran and the answer stands on the corpus supplement as well as the
|
|
68
|
-
* seed and the session. */
|
|
69
|
-
export const SUPPLEMENTED_MODE = "supplemented";
|
|
70
|
-
|
|
71
|
-
/** No corpus read happened, because no bands are configured or the breaker is
|
|
72
|
-
* open. The
|
|
73
|
-
* answer stands on the bundled seed and the session alone. */
|
|
74
|
-
export const SEED_SESSION_MODE = "seed-session";
|
|
75
|
-
|
|
76
|
-
const THROTTLE_ERROR_NAMES = new Set([
|
|
77
|
-
"ProvisionedThroughputExceededException",
|
|
78
|
-
"RequestLimitExceeded",
|
|
79
|
-
"ThrottlingException",
|
|
80
|
-
"TooManyRequestsException",
|
|
81
|
-
"InternalServerError",
|
|
82
|
-
"ServiceUnavailable",
|
|
83
|
-
"TimeoutError",
|
|
84
|
-
"AbortError",
|
|
85
|
-
]);
|
|
86
|
-
|
|
87
|
-
/** Does this failure mean the store itself is in trouble? Only these count
|
|
88
|
-
* toward a breaker: throttles, 5xx and timeouts. An empty result is an answer,
|
|
89
|
-
* and a rejected input is a bug in the caller, so neither is systemic. */
|
|
90
|
-
export function isSystemicFailure(error) {
|
|
91
|
-
if (!error) return false;
|
|
92
|
-
if (error.code === BACKEND_UNAVAILABLE_CODE) return true;
|
|
93
|
-
if (error.retryable === true || error.$retryable?.throttling === true) return true;
|
|
94
|
-
if (THROTTLE_ERROR_NAMES.has(error.name)) return true;
|
|
95
|
-
const status = error.status ?? error.$metadata?.httpStatusCode;
|
|
96
|
-
return status === 429 || (typeof status === "number" && status >= 500);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
70
|
/** Is fuzzy variant expansion on for this turn? Most specific setting wins:
|
|
100
71
|
* the request, then the environment, then the config file, then the default,
|
|
101
72
|
* which is on. */
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// http-row-backend.mjs — the §3.1 row-backend contract over the row
|
|
2
|
+
// service's HTTP surface (server/row-service/handler.mjs's endpoint table).
|
|
3
|
+
// This is the client half of the seam the DynamoDB backend fills on the
|
|
4
|
+
// storage side. A page or a Node caller constructs one per session and gets
|
|
5
|
+
// the same contract every other backend gives. Only the network can fail.
|
|
6
|
+
//
|
|
7
|
+
// The module is fetch-only and imports nothing node-specific, so it runs
|
|
8
|
+
// unmodified in a browser bundle and in a node test. `fetchImpl` lets a test
|
|
9
|
+
// or an older runtime hand in its own implementation.
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
ROW_BACKEND_KIND, ROW_BACKEND_CONTRACT_VERSION,
|
|
13
|
+
BackendRejected, BackendUnavailable,
|
|
14
|
+
} from "../../adapters/memory/row-backend.mjs";
|
|
15
|
+
|
|
16
|
+
const SESSION_HEADER = "x-tmct-session";
|
|
17
|
+
|
|
18
|
+
function trimTrailingSlash(value) {
|
|
19
|
+
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Reads whatever text the response carries, for the error's own message
|
|
23
|
+
* only. The class decision uses the status code alone (the service's own
|
|
24
|
+
* §3.8 mapping), so a consumer never has to parse a body to know whether a
|
|
25
|
+
* failure is worth retrying. */
|
|
26
|
+
async function responseFailureMessage(response, fallback) {
|
|
27
|
+
try {
|
|
28
|
+
const text = await response.text();
|
|
29
|
+
if (!text) return fallback;
|
|
30
|
+
const parsed = JSON.parse(text);
|
|
31
|
+
return parsed?.error?.message || fallback;
|
|
32
|
+
} catch {
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function throwForStatus(response, fallbackMessage) {
|
|
38
|
+
const status = response.status;
|
|
39
|
+
const message = await responseFailureMessage(response, fallbackMessage);
|
|
40
|
+
if (status === 400 || status === 413) throw new BackendRejected(message, { status });
|
|
41
|
+
// 429 (mutation rate), 507 (table cap), and any 5xx (503 is the handler's
|
|
42
|
+
// own mapping for a backend-level BackendUnavailable; 500 is an
|
|
43
|
+
// unexpected handler fault) all mean the store refused or failed to
|
|
44
|
+
// serve. They share one retryable bucket, decided by status, never by
|
|
45
|
+
// matching on message text.
|
|
46
|
+
throw new BackendUnavailable(message, { status });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function runFetch(fetchImpl, url, init, networkFailureMessage) {
|
|
50
|
+
try {
|
|
51
|
+
return await fetchImpl(url, init);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new BackendUnavailable(networkFailureMessage, { cause: error });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** One retry, on a 5xx only, for the one call the spec singles out.
|
|
58
|
+
* `deleteAll`'s purge is query-then-mark and idempotent, so a retried
|
|
59
|
+
* DELETE just re-marks whatever the first attempt already reached; it
|
|
60
|
+
* never double-applies. Every other mutation gets one attempt only:
|
|
61
|
+
* retrying a PUT or a keyed DELETE past a 5xx is a caller decision, not
|
|
62
|
+
* this backend's. */
|
|
63
|
+
async function fetchWithOneRetryOn5xx(fetchImpl, url, init, networkFailureMessage) {
|
|
64
|
+
const first = await runFetch(fetchImpl, url, init, networkFailureMessage);
|
|
65
|
+
if (first.status < 500) return first;
|
|
66
|
+
return runFetch(fetchImpl, url, init, networkFailureMessage);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** `createHttpRowBackend({ apiBase, sessionKey, fetchImpl })`: a §3.1 row
|
|
70
|
+
* backend over the row service at `apiBase` (same-origin, no trailing
|
|
71
|
+
* slash needed), scoped to `sessionKey` (a v4 UUID the caller minted).
|
|
72
|
+
* `fetchImpl` defaults to the ambient `fetch`, so a page never has to pass
|
|
73
|
+
* one. A test points it at a running `local.mjs` double instead. */
|
|
74
|
+
export function createHttpRowBackend({ apiBase, sessionKey, fetchImpl = fetch } = {}) {
|
|
75
|
+
if (typeof apiBase !== "string" || !apiBase) throw new TypeError("createHttpRowBackend needs an apiBase URL");
|
|
76
|
+
if (typeof sessionKey !== "string" || !sessionKey) throw new TypeError("createHttpRowBackend needs a sessionKey");
|
|
77
|
+
if (typeof fetchImpl !== "function") throw new TypeError("createHttpRowBackend needs a fetchImpl(url, init) function, or an ambient fetch");
|
|
78
|
+
|
|
79
|
+
const base = trimTrailingSlash(apiBase);
|
|
80
|
+
let closed = false;
|
|
81
|
+
|
|
82
|
+
const assertOpen = () => {
|
|
83
|
+
if (closed) throw new BackendUnavailable("this row backend was closed and can no longer be used");
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const jsonHeaders = { "content-type": "application/json" };
|
|
87
|
+
const sessionReadHeaders = { [SESSION_HEADER]: sessionKey };
|
|
88
|
+
|
|
89
|
+
async function getJson(path, { headers = {} } = {}) {
|
|
90
|
+
const response = await runFetch(fetchImpl, `${base}${path}`, { headers }, "could not reach the row service");
|
|
91
|
+
if (response.status === 404) return { notFound: true };
|
|
92
|
+
if (!response.ok) await throwForStatus(response, `GET ${path} failed`);
|
|
93
|
+
return { value: await response.json() };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function mutate(path, { method, body }, { retryOn5xx = false } = {}) {
|
|
97
|
+
const init = { method, headers: jsonHeaders, body: JSON.stringify(body) };
|
|
98
|
+
const response = retryOn5xx
|
|
99
|
+
? await fetchWithOneRetryOn5xx(fetchImpl, `${base}${path}`, init, "could not reach the row service")
|
|
100
|
+
: await runFetch(fetchImpl, `${base}${path}`, init, "could not reach the row service");
|
|
101
|
+
if (!response.ok) await throwForStatus(response, `${method} ${path} failed`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
kind: ROW_BACKEND_KIND,
|
|
106
|
+
contractVersion: ROW_BACKEND_CONTRACT_VERSION,
|
|
107
|
+
|
|
108
|
+
async readRows() {
|
|
109
|
+
assertOpen();
|
|
110
|
+
const result = await getJson("/api/rows", { headers: sessionReadHeaders });
|
|
111
|
+
return result.notFound ? [] : (result.value.rows || []);
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
async readRowsByTerm(term) {
|
|
115
|
+
assertOpen();
|
|
116
|
+
const result = await getJson(`/api/rows?term=${encodeURIComponent(term)}`, { headers: sessionReadHeaders });
|
|
117
|
+
return result.notFound ? [] : (result.value.rows || []);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
async putRows(rows) {
|
|
121
|
+
assertOpen();
|
|
122
|
+
await mutate(`/api/sessions/${sessionKey}/rows`, { method: "PUT", body: { puts: rows } });
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
async deleteRows(rowKeys) {
|
|
126
|
+
assertOpen();
|
|
127
|
+
await mutate(`/api/sessions/${sessionKey}/rows`, { method: "DELETE", body: { rowKeys } });
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
async deleteAll() {
|
|
131
|
+
assertOpen();
|
|
132
|
+
await mutate(`/api/sessions/${sessionKey}/rows`, { method: "DELETE", body: { all: true } }, { retryOn5xx: true });
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
async readMeta(key) {
|
|
136
|
+
assertOpen();
|
|
137
|
+
const result = await getJson(`/api/meta/${encodeURIComponent(key)}`, { headers: sessionReadHeaders });
|
|
138
|
+
return result.notFound ? null : result.value.value;
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
async putMeta(key, value) {
|
|
142
|
+
assertOpen();
|
|
143
|
+
await mutate(`/api/sessions/${sessionKey}/meta/${encodeURIComponent(key)}`, { method: "PUT", body: { value } });
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
async close() {
|
|
147
|
+
closed = true;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|