@supa-media/tex-contract 0.0.0-stage → 0.2.7
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/LICENSE +21 -0
- package/README.md +11 -2
- package/package.json +16 -4
- package/texContract/aboutLines.cjs +117 -0
- package/texContract/aboutLines.d.cts +14 -0
- package/texContract/aboutNoteLimits.cjs +41 -0
- package/texContract/aboutNoteLimits.d.cts +5 -0
- package/texContract/actionManual.cjs +104 -0
- package/texContract/activityHint.cjs +40 -0
- package/texContract/activityHint.d.cts +4 -0
- package/texContract/asks.cjs +16 -0
- package/texContract/asks.d.cts +6 -0
- package/texContract/asksToFile.cjs +20 -0
- package/texContract/asksToFile.d.cts +2 -0
- package/texContract/attention.cjs +138 -0
- package/texContract/attention.d.cts +20 -0
- package/texContract/browserHandback.cjs +13 -0
- package/texContract/browserHandback.d.cts +3 -0
- package/texContract/browserOwner.cjs +31 -0
- package/texContract/browserOwner.d.cts +2 -0
- package/texContract/bytes.cjs +12 -0
- package/texContract/chatHistoryPath.cjs +38 -0
- package/texContract/chatHistoryPath.d.cts +5 -0
- package/texContract/chatLedger.cjs +91 -0
- package/texContract/chatLedger.d.cts +21 -0
- package/texContract/computerTool.cjs +102 -0
- package/texContract/computerTool.d.cts +16 -0
- package/texContract/consentYes.cjs +22 -0
- package/texContract/consentYes.d.cts +5 -0
- package/texContract/contextGateway.cjs +52 -0
- package/texContract/contextGateway.d.cts +19 -0
- package/texContract/deskEligible.cjs +20 -0
- package/texContract/deskEligible.d.cts +3 -0
- package/texContract/downloads.cjs +129 -0
- package/texContract/downloads.d.cts +15 -0
- package/texContract/egressLedger.cjs +114 -0
- package/texContract/egressLedger.d.cts +14 -0
- package/texContract/emailRead.cjs +38 -0
- package/texContract/emailRead.d.cts +5 -0
- package/texContract/eventWords.cjs +35 -0
- package/texContract/eventWords.d.cts +4 -0
- package/texContract/fileParts.cjs +43 -0
- package/texContract/fileParts.d.cts +6 -0
- package/texContract/fileTables.cjs +243 -0
- package/texContract/fileWords.cjs +169 -0
- package/texContract/folded.cjs +16 -0
- package/texContract/folded.d.cts +3 -0
- package/texContract/googleAiAccess.cjs +12 -0
- package/texContract/googleAiAccess.d.cts +2 -0
- package/texContract/graphWires.cjs +18 -0
- package/texContract/graphWires.d.cts +5 -0
- package/texContract/groupSession.cjs +28 -0
- package/texContract/groupSession.d.cts +7 -0
- package/texContract/guestNames.cjs +136 -0
- package/texContract/guestNames.d.cts +35 -0
- package/texContract/heard.cjs +19 -0
- package/texContract/heard.d.cts +4 -0
- package/texContract/heldOffer.cjs +57 -0
- package/texContract/heldOffer.d.cts +8 -0
- package/texContract/helperCallers.cjs +29 -0
- package/texContract/helperCallers.d.cts +4 -0
- package/texContract/identity.cjs +179 -0
- package/texContract/identity.d.cts +8 -0
- package/texContract/jobLogEntries.cjs +93 -0
- package/texContract/jobLogEntries.d.cts +12 -0
- package/texContract/judgedActions.cjs +20 -0
- package/texContract/judgedActions.d.cts +4 -0
- package/texContract/labSandboxGate.cjs +63 -0
- package/texContract/labSandboxGate.d.cts +9 -0
- package/texContract/ledgerReadChars.cjs +4 -0
- package/texContract/ledgerReadChars.d.cts +3 -0
- package/texContract/mailCheck.cjs +90 -0
- package/texContract/mailCheck.d.cts +12 -0
- package/texContract/mailEntries.cjs +64 -0
- package/texContract/mailEntries.d.cts +13 -0
- package/texContract/mailHealth.cjs +60 -0
- package/texContract/mailHealth.d.cts +10 -0
- package/texContract/notePath.cjs +99 -0
- package/texContract/notePath.d.cts +4 -0
- package/texContract/noteRunFacts.cjs +136 -0
- package/texContract/noteRunFacts.d.cts +32 -0
- package/texContract/objectEtag.cjs +84 -0
- package/texContract/objectEtag.d.cts +4 -0
- package/texContract/objectPlace.cjs +32 -0
- package/texContract/objectPlace.d.cts +5 -0
- package/texContract/pageJobs.cjs +226 -0
- package/texContract/pageJobs.d.cts +13 -0
- package/texContract/pdfLinks.cjs +135 -0
- package/texContract/pdfLinks.d.cts +4 -0
- package/texContract/personHub.cjs +38 -0
- package/texContract/personHub.d.cts +5 -0
- package/texContract/personNameText.cjs +18 -0
- package/texContract/personNameText.d.cts +3 -0
- package/texContract/pinnedContext.cjs +12 -0
- package/texContract/pinnedContext.d.cts +3 -0
- package/texContract/quietHoursWindow.cjs +36 -0
- package/texContract/quietHoursWindow.d.cts +3 -0
- package/texContract/scopeClamp.cjs +117 -0
- package/texContract/scopeClamp.d.cts +5 -0
- package/texContract/scopes.cjs +29 -0
- package/texContract/scopes.d.cts +7 -0
- package/texContract/searchSettings.cjs +54 -0
- package/texContract/searchSettings.d.cts +6 -0
- package/texContract/settingHabits.cjs +87 -0
- package/texContract/settingHabits.d.cts +20 -0
- package/texContract/settingsNotes.cjs +217 -0
- package/texContract/settingsNotes.d.cts +26 -0
- package/texContract/shownEmail.cjs +181 -0
- package/texContract/texClients.cjs +53 -0
- package/texContract/texClients.d.cts +12 -0
- package/texContract/texGraph.cjs +593 -0
- package/texContract/texGraph.d.cts +55 -0
- package/texContract/texKeys.cjs +100 -0
- package/texContract/texKeys.d.cts +13 -0
- package/texContract/texOwnSources.cjs +9 -0
- package/texContract/texOwnSources.d.cts +3 -0
- package/texContract/texStoreNames.cjs +26 -0
- package/texContract/texStoreNames.d.cts +12 -0
- package/texContract/texStoreWire.cjs +306 -0
- package/texContract/texStoreWire.d.cts +32 -0
- package/texContract/threadsFold.cjs +75 -0
- package/texContract/threadsFold.d.cts +22 -0
- package/texContract/toolActions.cjs +253 -0
- package/texContract/toolCatalog.cjs +49 -0
- package/texContract/toolCatalog.generated.json +90 -0
- package/texContract/toolContract.cjs +52 -0
- package/texContract/toolContract.d.cts +12 -0
- package/texContract/toolShapes.cjs +14 -0
- package/texContract/transcription.cjs +101 -0
- package/texContract/transcription.d.cts +3 -0
- package/texContract/treeJobs.cjs +31 -0
- package/texContract/treeJobs.d.cts +3 -0
- package/texContract/turnSession.cjs +90 -0
- package/texContract/turnSession.d.cts +19 -0
- package/texContract/turnWords.cjs +90 -0
- package/texContract/turnWords.d.cts +9 -0
- package/texContract/uploadImages.cjs +131 -0
- package/texContract/uploadImages.d.cts +13 -0
- package/texContract/v2Names.cjs +41 -0
- package/texContract/v2SearchFetch.cjs +316 -0
- package/texContract/v2Translate.cjs +123 -0
- package/texContract/vaultTools.cjs +112 -0
- package/texContract/vaultTools.d.cts +15 -0
- package/texContract/yesWords.cjs +91 -0
- package/texShared/argumentCoercion.cjs +93 -0
- package/texShared/cellComments.cjs +122 -0
- package/texShared/cellComments.d.cts +21 -0
- package/texShared/comments.cjs +586 -0
- package/texShared/comments.d.cts +115 -0
- package/texShared/contentTypes.cjs +85 -0
- package/texShared/fileKinds.cjs +123 -0
- package/texShared/flags.cjs +124 -0
- package/texShared/flags.d.cts +21 -0
- package/texShared/folderJobInfo.cjs +301 -0
- package/texShared/folderJobInfo.d.cts +40 -0
- package/texShared/folderRoles.cjs +512 -0
- package/texShared/folderRoles.d.cts +73 -0
- package/texShared/githubOidc.ts +98 -0
- package/texShared/groupRunQuestions.cjs +98 -0
- package/texShared/groupRunQuestions.d.cts +4 -0
- package/texShared/inboxFolders.cjs +49 -0
- package/texShared/inboxFolders.d.cts +5 -0
- package/texShared/ink/drawing.cjs +474 -0
- package/texShared/ink/texFigure.cjs +252 -0
- package/texShared/ink/texFigure.d.cts +36 -0
- package/texShared/ink/texPoses.cjs +180 -0
- package/texShared/ink/texPoses.d.cts +20 -0
- package/texShared/jobPartEnd.cjs +133 -0
- package/texShared/jobPartEnd.d.cts +13 -0
- package/texShared/jobResume.cjs +31 -0
- package/texShared/jobResume.d.cts +5 -0
- package/texShared/jobSplit.cjs +90 -0
- package/texShared/jobSplit.d.cts +6 -0
- package/texShared/jobStart.cjs +56 -0
- package/texShared/jobStart.d.cts +9 -0
- package/texShared/mail.cjs +400 -0
- package/texShared/mail.d.cts +56 -0
- package/texShared/messageAnchors.cjs +38 -0
- package/texShared/models/aiKeys.cjs +137 -0
- package/texShared/models/aiKeys.d.cts +42 -0
- package/texShared/models/aiKeys.json +56 -0
- package/texShared/models/creditRest.cjs +269 -0
- package/texShared/models/promptBudget.cjs +295 -0
- package/texShared/models/promptSections.cjs +29 -0
- package/texShared/noteAppLink.cjs +23 -0
- package/texShared/notePreview.cjs +219 -0
- package/texShared/notePreview.d.cts +21 -0
- package/texShared/pages/blocks.cjs +215 -0
- package/texShared/pages/cover.cjs +164 -0
- package/texShared/pages/css.cjs +181 -0
- package/texShared/pages/cssBlocks.cjs +264 -0
- package/texShared/pages/draw.cjs +212 -0
- package/texShared/pages/drawRich.cjs +232 -0
- package/texShared/pages/html.cjs +168 -0
- package/texShared/pages/inline.cjs +251 -0
- package/texShared/pages/layout.cjs +76 -0
- package/texShared/pages/parse.cjs +418 -0
- package/texShared/pages/render.cjs +200 -0
- package/texShared/pages/skill.cjs +147 -0
- package/texShared/pages/skill.d.cts +5 -0
- package/texShared/pages/templates.cjs +336 -0
- package/texShared/r2Store.cjs +163 -0
- package/texShared/remoteImage.cjs +177 -0
- package/texShared/routines.cjs +656 -0
- package/texShared/routines.d.cts +86 -0
- package/texShared/servedListing.cjs +250 -0
- package/texShared/servedListing.d.cts +17 -0
- package/texShared/sheetCsv.cjs +566 -0
- package/texShared/sheetCsv.d.cts +91 -0
- package/texShared/storageLayout.cjs +88 -0
- package/texShared/storeKeys.cjs +144 -0
- package/texShared/toolArguments.cjs +323 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export function browserOwner(env: { GATEWAY_SECRET?: unknown } | null | undefined, userId: unknown): Promise<string | null>;
|
|
2
|
+
export function groupBrowserOwner(env: { GATEWAY_SECRET?: unknown } | null | undefined, group: { key?: unknown; members?: unknown } | null | undefined): Promise<string | null>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Base64 without a dependency, chunked so a large image cannot blow the stack. Shared by the gateway (`notes/embeds.js`) and Tex. */
|
|
3
|
+
function base64FromBytes(bytes) {
|
|
4
|
+
let binary = "";
|
|
5
|
+
const CHUNK = 0x8000;
|
|
6
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
7
|
+
binary += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK));
|
|
8
|
+
}
|
|
9
|
+
return btoa(binary);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = { base64FromBytes };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A chat's history file: `tex/history.ledger.json` for a person, `tex/groups/<words>/history.ledger.json` for a group
|
|
3
|
+
* chat (`agent/historyFile.js`, which refuses writes to it by this same predicate).
|
|
4
|
+
*
|
|
5
|
+
* The one predicate for both, because two callers judge the same path for opposite reasons and must not drift: the
|
|
6
|
+
* write side refuses it, and the egress ledger marks a read of it untrusted (`privacy/egress.js`) — every word in a
|
|
7
|
+
* chat is somebody else's, as `read_texts` already says, and `fetch` routes this file to `read_note`, so the mark has
|
|
8
|
+
* to follow the path rather than the tool's name.
|
|
9
|
+
*
|
|
10
|
+
* Normalized here rather than by `normalizePath`, which leaves a `@context/` prefix on: an id naming another context's
|
|
11
|
+
* history file is still a chat's history.
|
|
12
|
+
*/
|
|
13
|
+
// The conversations file beside it is a view of the same chat (`texStore/historyView.js`), judged the same way.
|
|
14
|
+
const CHAT_HISTORY_PATH = /(?:^|\/)tex\/(?:groups\/[^/]+\/)?(?:history|conversations)\.ledger\.json$/i;
|
|
15
|
+
|
|
16
|
+
function isChatHistoryPath(path) {
|
|
17
|
+
if (typeof path !== "string") return false;
|
|
18
|
+
const bare = path
|
|
19
|
+
.trim()
|
|
20
|
+
.replace(/^(?:\.\/|\/)+/, "")
|
|
21
|
+
.replace(/^@[^/]*\/+/, "")
|
|
22
|
+
.replace(/\/{2,}/g, "/");
|
|
23
|
+
return CHAT_HISTORY_PATH.test(bare);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A person's history file (`agent/historyFile.js`, `texStore/historyView.js` `HISTORY_FILE`). */
|
|
27
|
+
const HISTORY_PATH = "tex/history.ledger.json";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The words a tool call naming the history file is refused with, or null for any other path (`tools/dispatch.js`):
|
|
31
|
+
* read only, whatever asks.
|
|
32
|
+
*/
|
|
33
|
+
function historyWriteRefusal(path) {
|
|
34
|
+
if (!isChatHistoryPath(path)) return null;
|
|
35
|
+
return "That file is this chat's history, kept by Tex as it happens; it can be read but not written, moved or removed. Nothing was changed.";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { isChatHistoryPath, HISTORY_PATH, historyWriteRefusal };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a chat's ledger is and the writer that appends to it, which both Tex's turns (`agent/ledger.js`, which
|
|
3
|
+
* re-exports these) and the gateway's own routes (`machines/jobRoutes.js`, a computer part's steps) write through.
|
|
4
|
+
* The rows are kept by Context's Tex store (`apps/mcp/src/texStore/ledger.js`); what an entry says is Tex's
|
|
5
|
+
* (`agent/ledger.js`). Pure: the ledger itself is handed in.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { TEXTS_GROUP_CLIENT_ID } = require("./groupSession.cjs");
|
|
9
|
+
const { ONE_TO_ONE_CHAT } = require("./texStoreNames.cjs");
|
|
10
|
+
|
|
11
|
+
const GROUP_KEY = /^[0-9a-f]{64}$/;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The ledgers in one owner's Tex object (`{person: workspaceId}` or `{chat: groupKey}`, as `ledgerPlace` names it),
|
|
15
|
+
* from a store opener by owner name (`texStoreWire.cjs` `gatewayTexStoreWith`), or null when it names none or the
|
|
16
|
+
* opener has no object: `{append(chat, entries), read(chat, options), chats()}`.
|
|
17
|
+
*/
|
|
18
|
+
function ledgerIn(storeOf, owner) {
|
|
19
|
+
const name = owner?.person ? `person:${owner.person}` : owner?.chat ? `chat:${owner.chat}` : null;
|
|
20
|
+
if (name === null) return null;
|
|
21
|
+
const store = storeOf(name);
|
|
22
|
+
return store?.ledger ?? null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A writer for one chat that never throws and keeps the order it was given:
|
|
27
|
+
* `write(entries)` queues them, `flush()` waits for every write so far. A
|
|
28
|
+
* write that failed is logged by count and kind, never its words.
|
|
29
|
+
*/
|
|
30
|
+
function ledgerWriter(ledger, chat) {
|
|
31
|
+
if (ledger === null || typeof chat !== "string" || chat === "") return null;
|
|
32
|
+
let chain = Promise.resolve();
|
|
33
|
+
const write = (entries) => {
|
|
34
|
+
const list = (Array.isArray(entries) ? entries : [entries]).filter(Boolean);
|
|
35
|
+
if (list.length === 0) return chain;
|
|
36
|
+
chain = chain.then(async () => {
|
|
37
|
+
for (let at = 0; at < list.length; at += 100) {
|
|
38
|
+
try {
|
|
39
|
+
await ledger.append(chat, list.slice(at, at + 100));
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.log(JSON.stringify({ event: "ledger_write_failed", count: list.length, kinds: [...new Set(list.map((entry) => entry.kind))], reason: String(error?.name ?? "error").slice(0, 40) }));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return chain;
|
|
46
|
+
};
|
|
47
|
+
return { write, flush: () => chain };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whose ledger a session's turn writes and under which chat: a group's when
|
|
52
|
+
* the grant names one (`chat:<groupKey>`, chat = the group key), the person's
|
|
53
|
+
* own one-to-one chat on their personal workspace (`person:<workspaceId>`,
|
|
54
|
+
* chat `texts`), otherwise none.
|
|
55
|
+
*/
|
|
56
|
+
function ledgerPlace(session) {
|
|
57
|
+
if (typeof session?.texChatId === "string" && GROUP_KEY.test(session.texChatId)) return { owner: { chat: session.texChatId }, chat: session.texChatId };
|
|
58
|
+
// A group grant that names no chat keeps none: never the person's own one-to-one ledger, which a group's words must not reach.
|
|
59
|
+
if (session?.actorClientId === TEXTS_GROUP_CLIENT_ID) return null;
|
|
60
|
+
if (session?.workspaceKind === "personal" && typeof session.workspaceId === "string" && session.workspaceId !== "") return { owner: { person: session.workspaceId }, chat: ONE_TO_ONE_CHAT };
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The writer for the ledger of the chat a session's turn is in, through the
|
|
66
|
+
* turn's own store (`store/texRouting.js` `texLedger`), or null when the
|
|
67
|
+
* session keeps none (the app, a shared workspace's own connection).
|
|
68
|
+
*/
|
|
69
|
+
function storeLedger(store, session) {
|
|
70
|
+
const place = ledgerPlace(session);
|
|
71
|
+
if (place === null || typeof store?.texLedger !== "function") return null;
|
|
72
|
+
return ledgerWriter(store.texLedger(place.owner.chat ?? null), place.chat);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A writer that marks every entry with the background job whose turn wrote it
|
|
77
|
+
* (`jobId`, `jobTitle`), so the chat's one record says which work was a job's
|
|
78
|
+
* (the owner, 2026-11-02: sub-agent work "in the chat's history, tagged").
|
|
79
|
+
* `job` entries keep their own `job` field; this never replaces it.
|
|
80
|
+
*/
|
|
81
|
+
function jobTagged(writer, job) {
|
|
82
|
+
if (writer === null || !job || typeof job.id !== "string") return writer;
|
|
83
|
+
const tag = { jobId: job.id, ...(typeof job.title === "string" && job.title ? { jobTitle: job.title } : {}) };
|
|
84
|
+
return {
|
|
85
|
+
...writer,
|
|
86
|
+
write: (entries) => writer.write((Array.isArray(entries) ? entries : [entries]).filter(Boolean).map((entry) => ({ ...entry, ...tag }))),
|
|
87
|
+
flush: () => writer.flush(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = { ONE_TO_ONE_CHAT, ledgerIn, ledgerWriter, ledgerPlace, storeLedger, jobTagged };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Types for `chatLedger.cjs`; see that file. */
|
|
2
|
+
|
|
3
|
+
export interface ChatLedger {
|
|
4
|
+
append(chat: string, entries: unknown[]): Promise<unknown>;
|
|
5
|
+
read(chat: string, options?: Record<string, unknown>): Promise<any>;
|
|
6
|
+
chats(): Promise<any>;
|
|
7
|
+
}
|
|
8
|
+
export interface LedgerWriter {
|
|
9
|
+
write(entries: unknown): Promise<void>;
|
|
10
|
+
flush(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface LedgerOwner {
|
|
13
|
+
person?: string;
|
|
14
|
+
chat?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const ONE_TO_ONE_CHAT: string;
|
|
17
|
+
export declare function ledgerIn(storeOf: (name: string) => any, owner: LedgerOwner | null | undefined): ChatLedger | null;
|
|
18
|
+
export declare function ledgerWriter(ledger: ChatLedger | null, chat: string): LedgerWriter | null;
|
|
19
|
+
export declare function ledgerPlace(session: any): { owner: LedgerOwner; chat: string } | null;
|
|
20
|
+
export declare function storeLedger(store: any, session: any): LedgerWriter | null;
|
|
21
|
+
export declare function jobTagged(writer: LedgerWriter | null, job: { id?: unknown; title?: unknown } | null | undefined): LedgerWriter | null;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tex's tools for a person's own computers, as Tex and the gateway both name them
|
|
3
|
+
* (docs/decisions/texting-assistant/machines.md, computer-setup.md).
|
|
4
|
+
*
|
|
5
|
+
* The work is the gateway's (`apps/mcp/src/machines/texActions.js`, `forJobs.js`, `forTurn.js`, `groupAsk.js`),
|
|
6
|
+
* reached by Tex over MCP like any tool (`packages/tex/gateway/agent/machineDoor.js`):
|
|
7
|
+
*
|
|
8
|
+
* - `tex_computers`: the tool the model calls, `send` action `computers`;
|
|
9
|
+
* - `tex_machines`: the computers a chat turn may name (`list`), how a job picks one (`pick`), and the turn's
|
|
10
|
+
* words about them (`words`). Not offered to any model;
|
|
11
|
+
* - `tex_machine_thumb`: a computer owner's tapback on the chat's ask to run a job there. Not offered to any model.
|
|
12
|
+
*
|
|
13
|
+
* Only Tex's own texting clients may call any of them (`TEX_MACHINE_TOOLS`, `apps/mcp/src/machines/texTools.js`).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const { TEXTS_CLIENT_ID, TEXTS_GROUP_CLIENT_ID } = require("./texClients.cjs");
|
|
17
|
+
|
|
18
|
+
const COMPUTER_TOOL = "tex_computers";
|
|
19
|
+
const MACHINE_TOOL = "tex_machines";
|
|
20
|
+
const MACHINE_THUMB_TOOL = "tex_machine_thumb";
|
|
21
|
+
/** Every tool here: answered by the gateway for Tex's texting clients only, and never listed. */
|
|
22
|
+
const TEX_MACHINE_TOOLS = Object.freeze(new Set([COMPUTER_TOOL, MACHINE_TOOL, MACHINE_THUMB_TOOL]));
|
|
23
|
+
|
|
24
|
+
/** The argument Tex lays its own facts under (the person's words, the judge's answer); never the model's. */
|
|
25
|
+
const TEX_FACTS = "_tex";
|
|
26
|
+
|
|
27
|
+
const COMPUTER_DOES = Object.freeze(["list", "setup_prompt", "pair", "lend", "unlend", "rename", "limits", "more_time", "install", "sign_in", "sign_in_code", "disconnect"]);
|
|
28
|
+
const SETUP_SYSTEMS = Object.freeze(["mac", "linux", "headless"]);
|
|
29
|
+
const CODING_TOOLS = Object.freeze({ claude: "Claude Code", codex: "Codex" });
|
|
30
|
+
const CHAT_KEY = /^[0-9a-f]{64}$/;
|
|
31
|
+
|
|
32
|
+
const COMPUTER_TOOL_DEFINITION = Object.freeze({
|
|
33
|
+
name: COMPUTER_TOOL,
|
|
34
|
+
description:
|
|
35
|
+
"Set up and run the person's own computers yourself, instead of sending them to buttons: never tell them to do in the app what this does. " +
|
|
36
|
+
"do: list (their computers and how each is set), setup_prompt (the prompt for Claude Code or Codex on a computer that isn't paired yet; pass system mac, linux or headless), " +
|
|
37
|
+
"pair (approve the code its runner shows, passed exactly as they texted it; their own texts only), lend / unlend (lend it to this group chat or take it back, " +
|
|
38
|
+
"who: everyone, me, or @handles, ask_first true or false; a group only), rename (name), limits (slots, day_hours, week_hours, others_day_hours), " +
|
|
39
|
+
"more_time (hours, today only), install (tool claude or codex), sign_in (tool; texts them the link; their own texts only), sign_in_code (code they texted back), disconnect. " +
|
|
40
|
+
"computer names one of theirs; leave it out when they have one. What only they can do: paste the prompt into their agent (or run it), sign in to their own account on the page " +
|
|
41
|
+
"the link opens, and text you the code. Say just that, and do the rest.",
|
|
42
|
+
inputSchema: {
|
|
43
|
+
type: "object",
|
|
44
|
+
properties: {
|
|
45
|
+
do: { type: "string", enum: COMPUTER_DOES },
|
|
46
|
+
computer: { type: "string" },
|
|
47
|
+
system: { type: "string", enum: SETUP_SYSTEMS },
|
|
48
|
+
code: { type: "string" },
|
|
49
|
+
who: { type: "string" },
|
|
50
|
+
ask_first: { type: "boolean" },
|
|
51
|
+
name: { type: "string" },
|
|
52
|
+
tool: { type: "string", enum: Object.keys(CODING_TOOLS) },
|
|
53
|
+
hours: { type: "number" },
|
|
54
|
+
slots: { type: "integer" },
|
|
55
|
+
day_hours: { type: "number" },
|
|
56
|
+
week_hours: { type: "number" },
|
|
57
|
+
others_day_hours: { type: "number" },
|
|
58
|
+
},
|
|
59
|
+
required: ["do"],
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/** The `send` action the tool is shown as (`sixTools.js`). */
|
|
64
|
+
const COMPUTER_SEND_ACTIONS = Object.freeze({
|
|
65
|
+
computers: { tool: COMPUTER_TOOL, summary: "Set up and run their own computers yourself: list, setup prompt, pair by texted code, lend to this group, rename, limits, more time, install, sign in, disconnect." },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether a texted turn is offered `tex_computers`: a person's own texts, or a group chat's turn, and never a
|
|
70
|
+
* guest's tag or a grant without its person.
|
|
71
|
+
*/
|
|
72
|
+
function computerToolOffered(session, group) {
|
|
73
|
+
const own = session?.actorClientId === TEXTS_CLIENT_ID && group === null;
|
|
74
|
+
const groupKey = session?.actorClientId === TEXTS_GROUP_CLIENT_ID && CHAT_KEY.test(session.texChatId ?? "") ? session.texChatId : null;
|
|
75
|
+
if (!own && groupKey === null) return false;
|
|
76
|
+
return !(group?.guestTagger === true || typeof session.actorUserId !== "string" || session.actorUserId === "");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The agents `delegate` reaches on the web that a paired computer runs itself. */
|
|
80
|
+
const COMPUTER_AGENTS = Object.freeze(new Set(["claude-code", "codex"]));
|
|
81
|
+
|
|
82
|
+
/** The computers that can take code: Claude Code or Codex on them. */
|
|
83
|
+
function codeComputers(machines) {
|
|
84
|
+
return (Array.isArray(machines) ? machines : []).filter((machine) => (machine?.tools ?? []).some((tool) => tool === "claude" || tool === "codex"));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
COMPUTER_TOOL,
|
|
89
|
+
MACHINE_TOOL,
|
|
90
|
+
MACHINE_THUMB_TOOL,
|
|
91
|
+
TEX_MACHINE_TOOLS,
|
|
92
|
+
TEX_FACTS,
|
|
93
|
+
COMPUTER_DOES,
|
|
94
|
+
SETUP_SYSTEMS,
|
|
95
|
+
CODING_TOOLS,
|
|
96
|
+
CHAT_KEY,
|
|
97
|
+
COMPUTER_TOOL_DEFINITION,
|
|
98
|
+
COMPUTER_SEND_ACTIONS,
|
|
99
|
+
computerToolOffered,
|
|
100
|
+
COMPUTER_AGENTS,
|
|
101
|
+
codeComputers,
|
|
102
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Types for `computerTool.cjs`; see that file. */
|
|
2
|
+
|
|
3
|
+
export declare const COMPUTER_TOOL: "tex_computers";
|
|
4
|
+
export declare const MACHINE_TOOL: "tex_machines";
|
|
5
|
+
export declare const MACHINE_THUMB_TOOL: "tex_machine_thumb";
|
|
6
|
+
export declare const TEX_MACHINE_TOOLS: ReadonlySet<string>;
|
|
7
|
+
export declare const TEX_FACTS: "_tex";
|
|
8
|
+
export declare const COMPUTER_DOES: readonly string[];
|
|
9
|
+
export declare const SETUP_SYSTEMS: readonly string[];
|
|
10
|
+
export declare const CODING_TOOLS: Readonly<Record<string, string>>;
|
|
11
|
+
export declare const CHAT_KEY: RegExp;
|
|
12
|
+
export declare const COMPUTER_TOOL_DEFINITION: { name: string; description: string; inputSchema: Record<string, unknown> };
|
|
13
|
+
export declare const COMPUTER_SEND_ACTIONS: Readonly<Record<string, { tool: string; summary: string }>>;
|
|
14
|
+
export declare function computerToolOffered(session: unknown, group: unknown): boolean;
|
|
15
|
+
export declare const COMPUTER_AGENTS: ReadonlySet<string>;
|
|
16
|
+
export declare function codeComputers<T>(machines: T[] | null | undefined): T[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The consent check's words test (`consentContext.js`): a reply that names the thing, or, with a judge, answers Tex's
|
|
3
|
+
* own offer. The gateway's texting actions (`machines/texActions.js`) run it on the same words.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** The longest reply that may answer Tex's offer instead of naming the thing itself ("ok yeah go ahead and share it"). */
|
|
7
|
+
const MAX_YES = 160;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The code word check before a consent judge: the person's own words name the thing (`pattern`), or (Dev2,
|
|
11
|
+
* 2026-11-22) they are a short reply to Tex's last text and that text names it, so "yes", "go ahead" or "do it" can
|
|
12
|
+
* answer Tex's own offer. Only with a judge (`judged`): the judge, shown the same offer as `offered`, still decides
|
|
13
|
+
* whether the reply agrees to it.
|
|
14
|
+
*/
|
|
15
|
+
function namesOrAnswers(pattern, words, offered = "", judged = false) {
|
|
16
|
+
const said = typeof words === "string" ? words.trim() : "";
|
|
17
|
+
if (pattern.test(said)) return true;
|
|
18
|
+
if (!judged || said === "" || said.length > MAX_YES) return false;
|
|
19
|
+
return typeof offered === "string" && offered !== "" && pattern.test(offered);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { MAX_YES, namesOrAnswers };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WHAT TEX STILL TAKES FROM THE GATEWAY IT RUNS IN, HANDED OVER AT START-UP RATHER THAN IMPORTED.
|
|
3
|
+
*
|
|
4
|
+
* Tex is becoming its own service, an MCP client of Context like any other
|
|
5
|
+
* (docs/decisions/texting-assistant/tex-service.md). Until it moves out, the gateway answers a few of its needs
|
|
6
|
+
* in-process: an MCP `tools/call` as one person, a call of an action only Tex's own code makes, marking a turn's egress ledger for a read, the privacy engine's
|
|
7
|
+
* answers about a key, the egress judge, the control-plane client, and a session's tool list (and the plugin switches it
|
|
8
|
+
* reads) when Tex runs in-process, without the `CONTEXT_MCP` binding. The gateway connects them here once, when its dispatcher loads
|
|
9
|
+
* (`apps/mcp/src/tools/session.js`), and Tex reaches them only through `packages/tex/gateway/contextMcp.js`, so Tex's
|
|
10
|
+
* code imports no gateway module and each of these is one named seam to swap for MCP when it moves.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here decides anything: each function is the gateway's own, unchanged, so every gate and privacy rule still
|
|
13
|
+
* runs where it ran.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The names the gateway must hand over, every one a function. */
|
|
17
|
+
const CONTEXT_GATEWAY_PARTS = Object.freeze(["callTool", "callTexAction", "recordRead", "controlPlane", "isPlumbing", "readsAsChat", "canSee", "storeRules", "teamCanSee", "judgeWords", "listTools", "disabledTools"]);
|
|
18
|
+
|
|
19
|
+
let connected = null;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Connect the gateway's parts. Once per isolate: connecting again with the very same functions is a no-op, and with
|
|
23
|
+
* any other is refused, so nothing loaded later can swap the dispatcher Tex calls through.
|
|
24
|
+
*/
|
|
25
|
+
function connectContextGateway(parts) {
|
|
26
|
+
const given = {};
|
|
27
|
+
for (const name of CONTEXT_GATEWAY_PARTS) {
|
|
28
|
+
if (typeof parts?.[name] !== "function") throw new TypeError(`connectContextGateway: ${name} must be a function`);
|
|
29
|
+
given[name] = parts[name];
|
|
30
|
+
}
|
|
31
|
+
if (connected !== null) {
|
|
32
|
+
if (CONTEXT_GATEWAY_PARTS.every((name) => connected[name] === given[name])) return;
|
|
33
|
+
throw new Error("connectContextGateway: the gateway is already connected");
|
|
34
|
+
}
|
|
35
|
+
connected = Object.freeze(given);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The gateway's parts, or a loud failure when no gateway has connected (Tex code run without one). */
|
|
39
|
+
function contextGateway() {
|
|
40
|
+
if (connected === null) throw new Error("contextGateway: no gateway has connected (apps/mcp/src/tools/session.js connects it)");
|
|
41
|
+
return connected;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether a gateway connected in this isolate: true wherever Tex runs in-process in the gateway, false in Tex's own
|
|
46
|
+
* Worker, which never loads the dispatcher. Read only by a seam that, in Tex's Worker, leaves its check to the door.
|
|
47
|
+
*/
|
|
48
|
+
function gatewayConnected() {
|
|
49
|
+
return connected !== null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = { CONTEXT_GATEWAY_PARTS, connectContextGateway, contextGateway, gatewayConnected };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Types for `contextGateway.cjs`; see that file. */
|
|
2
|
+
|
|
3
|
+
export interface ContextGatewayParts {
|
|
4
|
+
callTool: (params: unknown, store: unknown, session: unknown, options?: unknown) => Promise<any>;
|
|
5
|
+
callTexAction: (params: unknown, store: unknown, session: unknown) => Promise<any>;
|
|
6
|
+
recordRead: (ledger: unknown, call: { name: string; args?: unknown; result: unknown; writing?: boolean }) => void;
|
|
7
|
+
controlPlane: (env: unknown) => any;
|
|
8
|
+
isPlumbing: (key: string) => boolean;
|
|
9
|
+
readsAsChat: (names: unknown) => boolean;
|
|
10
|
+
canSee: (key: string, scope: string, rules: unknown, overrides: unknown) => boolean;
|
|
11
|
+
storeRules: (store: unknown) => Promise<{ rules: unknown; overrides: unknown } | { error: string }>;
|
|
12
|
+
teamCanSee: (store: unknown, path: string) => Promise<boolean>;
|
|
13
|
+
judgeWords: (input: unknown, config: unknown, options?: { model?: string; effort?: string; fetchImpl?: unknown }) => Promise<{ allow: boolean; reason: string }>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare const CONTEXT_GATEWAY_PARTS: readonly (keyof ContextGatewayParts)[];
|
|
17
|
+
export declare function connectContextGateway(parts: ContextGatewayParts): void;
|
|
18
|
+
export declare function contextGateway(): ContextGatewayParts;
|
|
19
|
+
export declare function gatewayConnected(): boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WHETHER A CONNECTION SEES EVERY NOTE OF ITS PERSONAL WORKSPACE: the
|
|
3
|
+
* person's own connection to their personal workspace, as its owner, with
|
|
4
|
+
* the private tier.
|
|
5
|
+
*
|
|
6
|
+
* Tex reads its desk's notes by key only on such a connection
|
|
7
|
+
* (`packages/tex/gateway/agent/texDesk.js`, which re-exports this), and
|
|
8
|
+
* Context reads the person's history file straight from their ledger on it
|
|
9
|
+
* (`apps/mcp/src/tools/notes/historyRead.js`): that connection sees every
|
|
10
|
+
* note there, so neither shows anything `read_note` would not. One rule, so
|
|
11
|
+
* both sides make it the same way. Reads only the store's `contexts`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Whether this connection may read the desk's notes by key: its own personal workspace, as owner, private tier. */
|
|
15
|
+
function deskEligible(store) {
|
|
16
|
+
const current = (Array.isArray(store?.contexts) ? store.contexts : []).find((entry) => entry?.current === true);
|
|
17
|
+
return current?.kind === "personal" && current.role === "owner" && current.tier === "private";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { deskEligible };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared by Tex, which keeps a file (`agent/downloads.js`), and the gateway, which attaches one to an email
|
|
3
|
+
* (`tools/emailAttach.js`).
|
|
4
|
+
*
|
|
5
|
+
* FILES TEX DOWNLOADED, KEPT FOR A WHILE (the owner, 2026-10-22: "it should be
|
|
6
|
+
* able to download a pdf and email it"). Tex found a Google Voice invoice in
|
|
7
|
+
* the Admin console and told the owner to download it and attach it himself.
|
|
8
|
+
* Now the browser's `download` step (`browse.js`, `infra/site-shots/src/download.ts`)
|
|
9
|
+
* hands the file back, and it is kept here, in the person's own bucket, under
|
|
10
|
+
* an id the model passes on:
|
|
11
|
+
*
|
|
12
|
+
* - `send_email` / `save_email_draft` `attach: ["download:<id>"]` (`tools/email.js`);
|
|
13
|
+
* - a browse `upload` step with `file: "download:<id>"` (`uploads.js`).
|
|
14
|
+
*
|
|
15
|
+
* Where: `.context/agent/downloads/<id>.json`, a hidden folder of the
|
|
16
|
+
* workspace's own bucket, so a later text, or a background job, can still
|
|
17
|
+
* use it (the owner, 2026-10-22: "store it in a hidden folder in the bucket
|
|
18
|
+
* just so that it persists across Tex sessions"). How long: a file up to
|
|
19
|
+
* `LONG_KEEP_BYTES` for `LONG_KEEP_MS`, a bigger one (up to the 10 MB a file
|
|
20
|
+
* is read at) for `SHORT_KEEP_MS`, enough for an email waiting on a yes. The
|
|
21
|
+
* id carries when it was kept, so an expired file is refused without reading
|
|
22
|
+
* it and swept on the next download.
|
|
23
|
+
*
|
|
24
|
+
* The id is random: knowing one names nothing outside this bucket, and the
|
|
25
|
+
* bucket is the tenancy boundary, so another person's id finds nothing here.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const DOWNLOADS_PREFIX = ".context/agent/downloads/";
|
|
29
|
+
const LONG_KEEP_BYTES = 5 * 1024 * 1024;
|
|
30
|
+
const LONG_KEEP_MS = 30 * 24 * 60 * 60 * 1000;
|
|
31
|
+
const SHORT_KEEP_MS = 24 * 60 * 60 * 1000;
|
|
32
|
+
const MAX_DOWNLOAD_BYTES = 10 * 1024 * 1024;
|
|
33
|
+
|
|
34
|
+
/** `download:<ms>-<s|l>-<hex>`: when it was kept, for how long, and 16 random bytes. */
|
|
35
|
+
const ID = /^download:(\d{13})-([sl])-([a-f0-9]{32})$/;
|
|
36
|
+
|
|
37
|
+
const TYPE = /^[a-z]+\/[a-z0-9.+-]{1,100}$/;
|
|
38
|
+
|
|
39
|
+
/** A file name a person may be shown: its last part, no control characters or slashes. */
|
|
40
|
+
function downloadName(raw, fallback = "download") {
|
|
41
|
+
const leaf = String(raw ?? "").split(/[\\/]/).pop() ?? "";
|
|
42
|
+
const clean = leaf.replace(/[\u0000-\u001f\u007f\u202a-\u202e\u2066-\u2069]/g, "").trim().slice(0, 200);
|
|
43
|
+
return clean || fallback;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Whether a string is a download id at all. */
|
|
47
|
+
const isDownloadId = (value) => typeof value === "string" && ID.test(value.trim());
|
|
48
|
+
|
|
49
|
+
function keyOf(id) {
|
|
50
|
+
const m = ID.exec(id);
|
|
51
|
+
return m ? `${DOWNLOADS_PREFIX}${m[1]}-${m[2]}-${m[3]}.json` : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function expired(id, now) {
|
|
55
|
+
const m = ID.exec(id);
|
|
56
|
+
if (!m) return true;
|
|
57
|
+
return now - Number(m[1]) > (m[2] === "l" ? LONG_KEEP_MS : SHORT_KEEP_MS);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function sizeOf(data) {
|
|
61
|
+
return Math.floor((data.length * 3) / 4) - (data.endsWith("==") ? 2 : data.endsWith("=") ? 1 : 0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Keep a file the browser handed back, `{ name, type, data }` with `data` as
|
|
66
|
+
* base64, and answer `{ id, name, type, size, keptFor }` or `{ error }`.
|
|
67
|
+
*/
|
|
68
|
+
async function keepDownload(store, file, { now = Date.now() } = {}) {
|
|
69
|
+
if (typeof store?.put !== "function") return { error: "files can't be kept here; tell the person, and link the page instead" };
|
|
70
|
+
const data = typeof file?.data === "string" ? file.data : "";
|
|
71
|
+
if (!data || !/^[A-Za-z0-9+/]+=*$/.test(data)) return { error: "the browser gave back no file; open the page again and retry the download step" };
|
|
72
|
+
const size = sizeOf(data);
|
|
73
|
+
if (size > MAX_DOWNLOAD_BYTES) return { error: "that file is over the 10 MB a file can be; link it instead of attaching it" };
|
|
74
|
+
const name = downloadName(file.name);
|
|
75
|
+
const type = typeof file.type === "string" && TYPE.test(file.type) ? file.type : "application/octet-stream";
|
|
76
|
+
const long = size <= LONG_KEEP_BYTES;
|
|
77
|
+
const random = [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
78
|
+
const id = `download:${now}-${long ? "l" : "s"}-${random}`;
|
|
79
|
+
await sweepDownloads(store, now).catch(() => undefined);
|
|
80
|
+
await store.put(keyOf(id), JSON.stringify({ version: 1, name, type, size, keptAt: new Date(now).toISOString(), data }));
|
|
81
|
+
return { id, name, type, size, keptFor: long ? "30 days" : "24 hours" };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A kept file by its id: `{ file: { name, type, size, data } }`, or `{ error }`
|
|
86
|
+
* in words the model is shown.
|
|
87
|
+
*/
|
|
88
|
+
async function readDownload(store, rawId, { now = Date.now() } = {}) {
|
|
89
|
+
const id = String(rawId ?? "").trim();
|
|
90
|
+
if (!ID.test(id)) return { error: "a downloaded file is named by the download:… id the download step gave; pass that id" };
|
|
91
|
+
if (expired(id, now)) return { error: `${id} is no longer kept; download it again` };
|
|
92
|
+
let parsed = null;
|
|
93
|
+
try {
|
|
94
|
+
const object = await store.get(keyOf(id));
|
|
95
|
+
parsed = object ? JSON.parse(await object.text()) : null;
|
|
96
|
+
} catch {
|
|
97
|
+
parsed = null;
|
|
98
|
+
}
|
|
99
|
+
if (typeof parsed?.data !== "string") return { error: `${id} is not kept here; download it again` };
|
|
100
|
+
return {
|
|
101
|
+
file: {
|
|
102
|
+
name: downloadName(parsed.name),
|
|
103
|
+
type: typeof parsed.type === "string" && TYPE.test(parsed.type) ? parsed.type : "application/octet-stream",
|
|
104
|
+
size: sizeOf(parsed.data),
|
|
105
|
+
data: parsed.data,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Delete every kept file past its time. Best-effort: a missed sweep only leaves a file a little longer. */
|
|
111
|
+
async function sweepDownloads(store, now = Date.now()) {
|
|
112
|
+
if (typeof store?.list !== "function" || typeof store?.delete !== "function") return 0;
|
|
113
|
+
let swept = 0;
|
|
114
|
+
let cursor;
|
|
115
|
+
for (let page = 0; page < 5; page += 1) {
|
|
116
|
+
const listed = await store.list({ prefix: DOWNLOADS_PREFIX, ...(cursor ? { cursor } : {}) });
|
|
117
|
+
for (const object of listed?.objects ?? []) {
|
|
118
|
+
const leaf = String(object.key ?? "").slice(DOWNLOADS_PREFIX.length).replace(/\.json$/, "");
|
|
119
|
+
if (!expired(`download:${leaf}`, now)) continue;
|
|
120
|
+
await store.delete(object.key).catch(() => undefined);
|
|
121
|
+
swept += 1;
|
|
122
|
+
}
|
|
123
|
+
if (!listed?.truncated || !listed.cursor) break;
|
|
124
|
+
cursor = listed.cursor;
|
|
125
|
+
}
|
|
126
|
+
return swept;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = { DOWNLOADS_PREFIX, LONG_KEEP_BYTES, LONG_KEEP_MS, SHORT_KEEP_MS, MAX_DOWNLOAD_BYTES, downloadName, isDownloadId, keepDownload, readDownload, sweepDownloads };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Types for `downloads.cjs`; see that file. */
|
|
2
|
+
|
|
3
|
+
export type KeptFile = { name: string; type: string; size: number; data: string };
|
|
4
|
+
export type Kept = { id: string; name: string; type: string; size: number; keptFor: string };
|
|
5
|
+
|
|
6
|
+
export declare const DOWNLOADS_PREFIX: string;
|
|
7
|
+
export declare const LONG_KEEP_BYTES: number;
|
|
8
|
+
export declare const LONG_KEEP_MS: number;
|
|
9
|
+
export declare const SHORT_KEEP_MS: number;
|
|
10
|
+
export declare const MAX_DOWNLOAD_BYTES: number;
|
|
11
|
+
export declare function downloadName(raw: unknown, fallback?: string): string;
|
|
12
|
+
export declare function isDownloadId(value: unknown): boolean;
|
|
13
|
+
export declare function keepDownload(store: unknown, file: unknown, options?: { now?: number }): Promise<Kept | { error: string }>;
|
|
14
|
+
export declare function readDownload(store: unknown, rawId: unknown, options?: { now?: number }): Promise<{ file: KeptFile } | { error: string }>;
|
|
15
|
+
export declare function sweepDownloads(store: unknown, now?: number): Promise<number>;
|