@nyte-ai/host 0.0.0-stage → 0.0.18
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 +6 -2
- package/THIRD-PARTY-NOTICES.md +800 -0
- package/dist/chunks/canvas-preview-0cmbkp2j.js +65 -0
- package/dist/chunks/canvas-preview-0nq75h9k.js +7 -0
- package/dist/chunks/canvas-preview-0nrnbt76.js +497 -0
- package/dist/chunks/canvas-preview-1sq7z9jq.js +17 -0
- package/dist/chunks/canvas-preview-1ta3avah.js +56 -0
- package/dist/chunks/canvas-preview-2zdyh9ny.js +26 -0
- package/dist/chunks/canvas-preview-31w7xm8z.js +34 -0
- package/dist/chunks/canvas-preview-4b6sayhg.js +295 -0
- package/dist/chunks/canvas-preview-4e0pxa32.js +63 -0
- package/dist/chunks/canvas-preview-518dh8by.js +169 -0
- package/dist/chunks/canvas-preview-5hxat7hh.js +239 -0
- package/dist/chunks/canvas-preview-5mex725c.js +14 -0
- package/dist/chunks/canvas-preview-5z63am63.js +295 -0
- package/dist/chunks/canvas-preview-65xqjw7f.js +20 -0
- package/dist/chunks/canvas-preview-69fkk9yn.js +95 -0
- package/dist/chunks/canvas-preview-75db60xy.js +457 -0
- package/dist/chunks/canvas-preview-76fwvd6e.js +272 -0
- package/dist/chunks/canvas-preview-8xgt67ka.js +598 -0
- package/dist/chunks/canvas-preview-98aj5p34.js +176 -0
- package/dist/chunks/canvas-preview-9aqczacs.js +73 -0
- package/dist/chunks/canvas-preview-a3yw0j70.js +72 -0
- package/dist/chunks/canvas-preview-azg56359.js +68 -0
- package/dist/chunks/canvas-preview-d9wwhbtz.js +240 -0
- package/dist/chunks/canvas-preview-ehf3vp31.js +411 -0
- package/dist/chunks/canvas-preview-evh92ywd.js +369 -0
- package/dist/chunks/canvas-preview-ewgspmsd.js +81 -0
- package/dist/chunks/canvas-preview-g3qj3y3k.js +156 -0
- package/dist/chunks/canvas-preview-h8hshnff.js +63 -0
- package/dist/chunks/canvas-preview-j1tnf51w.js +36 -0
- package/dist/chunks/canvas-preview-kdqgrstc.js +194 -0
- package/dist/chunks/canvas-preview-kn7kvbfj.js +12 -0
- package/dist/chunks/canvas-preview-m02v4yy6.js +386 -0
- package/dist/chunks/canvas-preview-mgcx2b2p.js +271 -0
- package/dist/chunks/canvas-preview-rp4x6y1h.js +144 -0
- package/dist/chunks/canvas-preview-rp6fz0w5.js +130 -0
- package/dist/chunks/canvas-preview-s94ebwf5.js +112 -0
- package/dist/chunks/canvas-preview-senpjwep.js +518 -0
- package/dist/chunks/canvas-preview-skzwvw4d.js +23 -0
- package/dist/chunks/canvas-preview-srgfpdmf.js +60 -0
- package/dist/chunks/canvas-preview-tjhabynd.js +252 -0
- package/dist/chunks/canvas-preview-vaatfznm.js +189 -0
- package/dist/chunks/canvas-preview-vax9ne4e.js +341 -0
- package/dist/chunks/canvas-preview-wg80rbc2.js +46 -0
- package/dist/chunks/canvas-preview-xn10d4f8.js +608 -0
- package/dist/chunks/canvas-preview-yjz0f7n4.js +50 -0
- package/dist/chunks/canvas-preview-ymgz0937.js +587 -0
- package/dist/chunks/canvas-preview-yrfz2ak6.js +134 -0
- package/dist/chunks/canvas-preview-zaaj99wd.js +244 -0
- package/dist/chunks/canvas-preview-zn2d0k9d.js +133 -0
- package/dist/src/canvas-preview.d.ts +29 -0
- package/dist/src/canvas-preview.js +267 -0
- package/dist/src/credentials.d.ts +44 -0
- package/dist/src/credentials.js +10 -0
- package/dist/src/deploy/cloudflare/worker.d.ts +50 -0
- package/dist/src/deploy/cloudflare/worker.js +196 -0
- package/dist/src/deploy/vercel/nitro.config.d.ts +2 -0
- package/dist/src/deploy/vercel/nitro.config.js +30 -0
- package/dist/src/deploy/vercel/server/routes/[...].d.ts +2 -0
- package/dist/src/deploy/vercel/server/routes/[...].js +18 -0
- package/dist/src/deploy/vercel/server/utils/host.d.ts +13 -0
- package/dist/src/deploy/vercel/server/utils/host.js +7 -0
- package/dist/src/deploy/vercel/server/utils/repair.d.ts +1 -0
- package/dist/src/deploy/vercel/server/utils/repair.js +6 -0
- package/dist/src/deploy/vercel/server/utils/step.d.ts +10 -0
- package/dist/src/deploy/vercel/server/utils/step.js +6 -0
- package/dist/src/deploy/vercel/server/workflows/drive.d.ts +14 -0
- package/dist/src/deploy/vercel/server/workflows/drive.js +13 -0
- package/dist/src/durable-object.d.ts +14 -0
- package/dist/src/durable-object.js +231 -0
- package/dist/src/hash.d.ts +1 -0
- package/dist/src/hash.js +6 -0
- package/dist/src/identity-key.d.ts +11 -0
- package/dist/src/identity-key.js +9 -0
- package/dist/src/identity.d.ts +12 -0
- package/dist/src/identity.js +6 -0
- package/dist/src/index.d.ts +21 -0
- package/dist/src/index.js +120 -0
- package/dist/src/key-file.d.ts +1 -0
- package/dist/src/key-file.js +7 -0
- package/dist/src/listen-fetch.d.ts +8 -0
- package/dist/src/listen-fetch.js +9 -0
- package/dist/src/listen.d.ts +4 -0
- package/dist/src/listen.js +10 -0
- package/dist/src/local/command.d.ts +15 -0
- package/dist/src/local/command.js +8 -0
- package/dist/src/local/feed.d.ts +7 -0
- package/dist/src/local/feed.js +6 -0
- package/dist/src/local/files.d.ts +18 -0
- package/dist/src/local/files.js +13 -0
- package/dist/src/local/git-watch.d.ts +15 -0
- package/dist/src/local/git-watch.js +7 -0
- package/dist/src/local/git.d.ts +5 -0
- package/dist/src/local/git.js +13 -0
- package/dist/src/local/github.d.ts +30 -0
- package/dist/src/local/github.js +9 -0
- package/dist/src/local/identity.d.ts +3 -0
- package/dist/src/local/identity.js +7 -0
- package/dist/src/local/jobs.d.ts +7 -0
- package/dist/src/local/jobs.js +6 -0
- package/dist/src/local/legacy-trust.d.ts +10 -0
- package/dist/src/local/legacy-trust.js +6 -0
- package/dist/src/local/patch.d.ts +16 -0
- package/dist/src/local/patch.js +17 -0
- package/dist/src/local/provider.d.ts +11 -0
- package/dist/src/local/provider.js +20 -0
- package/dist/src/local/snapshots.d.ts +7 -0
- package/dist/src/local/snapshots.js +11 -0
- package/dist/src/local/text.d.ts +1 -0
- package/dist/src/local/text.js +6 -0
- package/dist/src/local/tree.d.ts +50 -0
- package/dist/src/local/tree.js +16 -0
- package/dist/src/local/workspace-migration.d.ts +2 -0
- package/dist/src/local/workspace-migration.js +7 -0
- package/dist/src/local/worktrees.d.ts +46 -0
- package/dist/src/local/worktrees.js +14 -0
- package/dist/src/mcp/config.d.ts +75 -0
- package/dist/src/mcp/config.js +12 -0
- package/dist/src/mcp/connect.d.ts +28 -0
- package/dist/src/mcp/connect.js +9 -0
- package/dist/src/mcp/hub.d.ts +19 -0
- package/dist/src/mcp/hub.js +11 -0
- package/dist/src/mcp/oauth.d.ts +46 -0
- package/dist/src/mcp/oauth.js +12 -0
- package/dist/src/mcp/tools.d.ts +29 -0
- package/dist/src/mcp/tools.js +12 -0
- package/dist/src/open.d.ts +42 -0
- package/dist/src/open.js +44 -0
- package/dist/src/plugins/loader.d.ts +21 -0
- package/dist/src/plugins/loader.js +11 -0
- package/dist/src/postgres.d.ts +19 -0
- package/dist/src/postgres.js +8 -0
- package/dist/src/providers.d.ts +16 -0
- package/dist/src/providers.js +112 -0
- package/dist/src/secrets.d.ts +12 -0
- package/dist/src/secrets.js +8 -0
- package/dist/src/serve/binding.d.ts +39 -0
- package/dist/src/serve/binding.js +6 -0
- package/dist/src/serve/handler.d.ts +15 -0
- package/dist/src/serve/handler.js +8 -0
- package/dist/src/serve/stream-server.d.ts +6 -0
- package/dist/src/serve/stream-server.js +6 -0
- package/dist/src/session-index.d.ts +11 -0
- package/dist/src/session-index.js +11 -0
- package/dist/src/shell-env.d.ts +14 -0
- package/dist/src/shell-env.js +108 -0
- package/dist/src/sqlite-workers.d.ts +6 -0
- package/dist/src/sqlite-workers.js +6 -0
- package/dist/src/sqlite.d.ts +7 -0
- package/dist/src/sqlite.js +10 -0
- package/dist/src/store-owner.d.ts +15 -0
- package/dist/src/store-owner.js +9 -0
- package/dist/src/store-rows.d.ts +13 -0
- package/dist/src/store-rows.js +26 -0
- package/dist/src/store-usage.d.ts +25 -0
- package/dist/src/store-usage.js +16 -0
- package/dist/src/usage-scan.d.ts +24 -0
- package/dist/src/usage-scan.js +65 -0
- package/dist/src/usage-service.d.ts +22 -0
- package/dist/src/usage-service.js +12 -0
- package/dist/src/usage-summary.d.ts +9 -0
- package/dist/src/usage-summary.js +16 -0
- package/dist/src/usage-types.d.ts +86 -0
- package/dist/src/usage-types.js +0 -0
- package/dist/src/usage-worker.d.ts +1 -0
- package/dist/src/usage-worker.js +25 -0
- package/dist/src/usage.d.ts +72 -0
- package/dist/src/usage.js +13 -0
- package/package.json +132 -3
- package/src/canvas-preview.ts +402 -0
- package/src/credentials.ts +294 -0
- package/src/deploy/cloudflare/README.md +3 -0
- package/src/deploy/cloudflare/worker.ts +306 -0
- package/src/deploy/cloudflare/wrangler.jsonc +12 -0
- package/src/deploy/vercel/README.md +3 -0
- package/src/deploy/vercel/nitro.config.ts +29 -0
- package/src/deploy/vercel/server/routes/[...].ts +10 -0
- package/src/deploy/vercel/server/utils/host.ts +104 -0
- package/src/deploy/vercel/server/utils/repair.ts +35 -0
- package/src/deploy/vercel/server/utils/step.ts +25 -0
- package/src/deploy/vercel/server/workflows/drive.ts +46 -0
- package/src/deploy/vercel/tsconfig.json +9 -0
- package/src/deploy/vercel/types/bun.d.ts +2 -0
- package/src/durable-object.ts +386 -0
- package/src/hash.ts +5 -0
- package/src/identity-key.ts +20 -0
- package/src/identity.ts +66 -0
- package/src/index.ts +21 -0
- package/src/key-file.ts +53 -0
- package/src/listen-fetch.ts +409 -0
- package/src/listen.ts +228 -0
- package/src/local/command.ts +109 -0
- package/src/local/feed.ts +70 -0
- package/src/local/files.ts +812 -0
- package/src/local/git-watch.ts +189 -0
- package/src/local/git.ts +914 -0
- package/src/local/github.ts +692 -0
- package/src/local/identity.ts +21 -0
- package/src/local/jobs.ts +587 -0
- package/src/local/legacy-trust.ts +105 -0
- package/src/local/patch.ts +334 -0
- package/src/local/provider.ts +184 -0
- package/src/local/snapshots.ts +776 -0
- package/src/local/text.ts +9 -0
- package/src/local/tree.ts +398 -0
- package/src/local/workspace-migration.ts +95 -0
- package/src/local/worktrees.ts +239 -0
- package/src/mcp/config.ts +330 -0
- package/src/mcp/connect.ts +105 -0
- package/src/mcp/hub.ts +836 -0
- package/src/mcp/oauth.ts +436 -0
- package/src/mcp/tools.ts +333 -0
- package/src/open.ts +469 -0
- package/src/plugins/loader.ts +186 -0
- package/src/postgres.ts +492 -0
- package/src/providers.ts +135 -0
- package/src/secrets.ts +31 -0
- package/src/serve/binding.ts +628 -0
- package/src/serve/handler.ts +362 -0
- package/src/serve/stream-server.ts +176 -0
- package/src/session-index.ts +99 -0
- package/src/shell-env.ts +153 -0
- package/src/sqlite-workers.ts +283 -0
- package/src/sqlite.ts +528 -0
- package/src/store-owner.ts +98 -0
- package/src/store-rows.ts +110 -0
- package/src/store-usage.ts +344 -0
- package/src/usage-scan.ts +81 -0
- package/src/usage-service.ts +73 -0
- package/src/usage-summary.ts +79 -0
- package/src/usage-types.ts +87 -0
- package/src/usage-worker.ts +24 -0
- package/src/usage.ts +701 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cacheSavings2,
|
|
3
|
+
usageTokens2
|
|
4
|
+
} from "./canvas-preview-srgfpdmf.js";
|
|
5
|
+
import {
|
|
6
|
+
decodeEvent2,
|
|
7
|
+
parseNumber2,
|
|
8
|
+
parseText2
|
|
9
|
+
} from "./canvas-preview-69fkk9yn.js";
|
|
10
|
+
|
|
11
|
+
// src/store-usage.ts
|
|
12
|
+
import { existsSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { DatabaseSync } from "node:sqlite";
|
|
15
|
+
import {
|
|
16
|
+
decodeObject,
|
|
17
|
+
decodeSessionRecord,
|
|
18
|
+
decodeWorkspaceRecord,
|
|
19
|
+
parseSessionRef
|
|
20
|
+
} from "@nyte-ai/core/store";
|
|
21
|
+
var EMPTY_TOTALS = {
|
|
22
|
+
input: 0,
|
|
23
|
+
output: 0,
|
|
24
|
+
cacheRead: 0,
|
|
25
|
+
cacheWrite: 0,
|
|
26
|
+
reasoning: 0,
|
|
27
|
+
tokens: 0,
|
|
28
|
+
cost: 0,
|
|
29
|
+
cacheSavings: 0
|
|
30
|
+
};
|
|
31
|
+
function add(totals, usage) {
|
|
32
|
+
return {
|
|
33
|
+
input: totals.input + usage.input,
|
|
34
|
+
output: totals.output + usage.output,
|
|
35
|
+
cacheRead: totals.cacheRead + usage.cacheRead,
|
|
36
|
+
cacheWrite: totals.cacheWrite + usage.cacheWrite,
|
|
37
|
+
reasoning: totals.reasoning + (usage.reasoning ?? 0),
|
|
38
|
+
tokens: totals.tokens + usageTokens2(usage),
|
|
39
|
+
cost: totals.cost + usage.cost.total,
|
|
40
|
+
cacheSavings: totals.cacheSavings + cacheSavings2(usage)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
var USAGE_EVENTS2 = "SELECT session, seq, at, event FROM log WHERE json_extract(event, '$.kind') = 'request' OR (json_extract(event, '$.kind') = 'run' AND json_extract(event, '$.change') = 'started') ORDER BY session, seq";
|
|
44
|
+
function localDay2(at) {
|
|
45
|
+
const date = new Date(at);
|
|
46
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
47
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
48
|
+
return `${String(date.getFullYear())}-${month}-${day}`;
|
|
49
|
+
}
|
|
50
|
+
function shiftDay2(day, delta) {
|
|
51
|
+
const date = new Date(`${day}T00:00:00`);
|
|
52
|
+
date.setDate(date.getDate() + delta);
|
|
53
|
+
return localDay2(date.getTime());
|
|
54
|
+
}
|
|
55
|
+
function priorWindow(sinceDay, untilDay) {
|
|
56
|
+
if (sinceDay === null)
|
|
57
|
+
return;
|
|
58
|
+
const priorUntil = shiftDay2(sinceDay, -1);
|
|
59
|
+
const span = (Date.parse(`${untilDay}T00:00:00Z`) - Date.parse(`${sinceDay}T00:00:00Z`)) / 86400000 + 1;
|
|
60
|
+
return { sinceDay: shiftDay2(priorUntil, -(span - 1)), untilDay: priorUntil };
|
|
61
|
+
}
|
|
62
|
+
function projectUsageReport2(stores, window, readAt) {
|
|
63
|
+
const requestedSince = window.sinceDay;
|
|
64
|
+
const untilDay = window.untilDay;
|
|
65
|
+
const prior = priorWindow(requestedSince, untilDay);
|
|
66
|
+
const cells = new Map;
|
|
67
|
+
const sessions = new Map;
|
|
68
|
+
const sources = [];
|
|
69
|
+
let priorCost = 0;
|
|
70
|
+
let priorTokens = 0;
|
|
71
|
+
let sawPrior = false;
|
|
72
|
+
let earliestDay;
|
|
73
|
+
for (const store of stores) {
|
|
74
|
+
sources.push({
|
|
75
|
+
workspacePath: store.workspacePath,
|
|
76
|
+
status: store.failure === null ? "ok" : "failed",
|
|
77
|
+
sessions: store.sessions.length,
|
|
78
|
+
message: store.failure?.message ?? null
|
|
79
|
+
});
|
|
80
|
+
for (const session of store.sessions) {
|
|
81
|
+
let lastActivityAt = 0;
|
|
82
|
+
let spentInWindow = false;
|
|
83
|
+
for (const spend of session.records) {
|
|
84
|
+
const { usage } = spend;
|
|
85
|
+
const amounts = [
|
|
86
|
+
usage.total,
|
|
87
|
+
usage.input,
|
|
88
|
+
usage.output,
|
|
89
|
+
usage.cacheRead,
|
|
90
|
+
usage.cacheWrite,
|
|
91
|
+
usage.cost.total
|
|
92
|
+
];
|
|
93
|
+
if (!amounts.some((amount) => amount > 0))
|
|
94
|
+
continue;
|
|
95
|
+
const day = localDay2(spend.at);
|
|
96
|
+
if (earliestDay === undefined || day < earliestDay)
|
|
97
|
+
earliestDay = day;
|
|
98
|
+
if (prior !== undefined && day >= prior.sinceDay && day <= prior.untilDay) {
|
|
99
|
+
sawPrior = true;
|
|
100
|
+
priorCost += spend.usage.cost.total;
|
|
101
|
+
priorTokens += usageTokens2(spend.usage);
|
|
102
|
+
}
|
|
103
|
+
if (day > untilDay)
|
|
104
|
+
continue;
|
|
105
|
+
if (requestedSince !== null && day < requestedSince)
|
|
106
|
+
continue;
|
|
107
|
+
const key = JSON.stringify([day, store.workspacePath, session.sessionId, spend.subject]);
|
|
108
|
+
const cell = cells.get(key);
|
|
109
|
+
if (cell === undefined) {
|
|
110
|
+
cells.set(key, {
|
|
111
|
+
entry: {
|
|
112
|
+
day,
|
|
113
|
+
workspacePath: store.workspacePath,
|
|
114
|
+
sessionId: session.sessionId,
|
|
115
|
+
subject: spend.subject
|
|
116
|
+
},
|
|
117
|
+
totals: add(EMPTY_TOTALS, spend.usage)
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
cell.totals = add(cell.totals, spend.usage);
|
|
121
|
+
}
|
|
122
|
+
spentInWindow = true;
|
|
123
|
+
lastActivityAt = Math.max(lastActivityAt, spend.at);
|
|
124
|
+
}
|
|
125
|
+
if (spentInWindow) {
|
|
126
|
+
sessions.set(session.sessionId, {
|
|
127
|
+
sessionId: session.sessionId,
|
|
128
|
+
name: session.name,
|
|
129
|
+
workspacePath: store.workspacePath,
|
|
130
|
+
lastActivityAt: session.lastActivityAt ?? lastActivityAt
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
readAt,
|
|
137
|
+
sinceDay: requestedSince ?? earliestDay ?? untilDay,
|
|
138
|
+
untilDay,
|
|
139
|
+
entries: [...cells.values()].map((cell) => ({ ...cell.entry, totals: cell.totals })).toSorted((left, right) => left.day.localeCompare(right.day)),
|
|
140
|
+
sessions: [...sessions.values()],
|
|
141
|
+
sources,
|
|
142
|
+
previous: sawPrior ? { cost: priorCost, tokens: priorTokens } : undefined,
|
|
143
|
+
earliestDay
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function readNyteUsage2(home, window) {
|
|
147
|
+
const readAt = Date.now();
|
|
148
|
+
const path = join(home, ".nyte", "nyte.db");
|
|
149
|
+
if (!existsSync(path))
|
|
150
|
+
return projectUsageReport2([], window, readAt);
|
|
151
|
+
let database;
|
|
152
|
+
try {
|
|
153
|
+
database = new DatabaseSync(path, { readOnly: true, timeout: 5000 });
|
|
154
|
+
database.exec("BEGIN");
|
|
155
|
+
const workspaces = new Map;
|
|
156
|
+
const sessions = new Map;
|
|
157
|
+
const refs = database.prepare("SELECT refs.name, objects.body FROM refs JOIN objects ON objects.id = refs.target WHERE refs.name LIKE 'workspaces/%' OR (refs.name LIKE 'sessions/%' AND instr(substr(refs.name, 10), '/') = 0)").all();
|
|
158
|
+
for (const row of refs) {
|
|
159
|
+
const name = parseText2(row.name);
|
|
160
|
+
const body = parseText2(row.body);
|
|
161
|
+
if (name === null || body === null || !name.startsWith("workspaces/"))
|
|
162
|
+
continue;
|
|
163
|
+
const workspace = decodeWorkspaceRecord(decodeObject(body));
|
|
164
|
+
if (workspace !== null)
|
|
165
|
+
workspaces.set(workspace.id, workspace.target.kind === "local" && "path" in workspace.target ? parseText2(workspace.target.path) : null);
|
|
166
|
+
}
|
|
167
|
+
for (const row of refs) {
|
|
168
|
+
const name = parseText2(row.name);
|
|
169
|
+
const body = parseText2(row.body);
|
|
170
|
+
if (name === null || body === null || name.split("/").length !== 2)
|
|
171
|
+
continue;
|
|
172
|
+
const id = parseSessionRef(name);
|
|
173
|
+
if (id === null)
|
|
174
|
+
continue;
|
|
175
|
+
const session = decodeSessionRecord(decodeObject(body));
|
|
176
|
+
if (session === null)
|
|
177
|
+
throw new Error(`Cannot read session ${id}.`);
|
|
178
|
+
sessions.set(id, {
|
|
179
|
+
workspacePath: workspaces.get(session.workspace) ?? null,
|
|
180
|
+
name: session.name ?? undefined,
|
|
181
|
+
lastActivityAt: session.lastActivityAt,
|
|
182
|
+
records: []
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
const models = new Map;
|
|
186
|
+
for (const row of database.prepare(USAGE_EVENTS2).iterate()) {
|
|
187
|
+
const session = parseSessionRef(`sessions/${parseText2(row.session) ?? ""}`);
|
|
188
|
+
if (session === null)
|
|
189
|
+
throw new Error("Cannot read usage session.");
|
|
190
|
+
const event = decodeEvent2(parseNumber2(row.seq), parseNumber2(row.at), parseText2(row.event) ?? "");
|
|
191
|
+
let heads = models.get(session);
|
|
192
|
+
if (heads === undefined) {
|
|
193
|
+
heads = new Map;
|
|
194
|
+
models.set(session, heads);
|
|
195
|
+
}
|
|
196
|
+
if (event.kind === "run" && event.change === "started")
|
|
197
|
+
heads.set(event.head, event.view.configuration.model);
|
|
198
|
+
if (event.kind !== "request")
|
|
199
|
+
continue;
|
|
200
|
+
const model = heads.get(event.head);
|
|
201
|
+
const subject = event.step === "compaction" || event.step === "summary" ? { kind: "compaction" } : event.step === "plugin" ? { kind: "tool" } : {
|
|
202
|
+
kind: "model",
|
|
203
|
+
provider: model?.provider ?? "unknown",
|
|
204
|
+
model: model?.id ?? "unknown"
|
|
205
|
+
};
|
|
206
|
+
let info = sessions.get(session);
|
|
207
|
+
if (info === undefined) {
|
|
208
|
+
info = { workspacePath: null, name: undefined, lastActivityAt: undefined, records: [] };
|
|
209
|
+
sessions.set(session, info);
|
|
210
|
+
}
|
|
211
|
+
info.records.push({ at: event.at, subject, usage: event.usage });
|
|
212
|
+
}
|
|
213
|
+
database.exec("COMMIT");
|
|
214
|
+
const stores = new Map;
|
|
215
|
+
for (const [sessionId, info] of sessions) {
|
|
216
|
+
const group = stores.get(info.workspacePath) ?? [];
|
|
217
|
+
group.push({
|
|
218
|
+
sessionId,
|
|
219
|
+
name: info.name,
|
|
220
|
+
records: info.records,
|
|
221
|
+
lastActivityAt: info.lastActivityAt
|
|
222
|
+
});
|
|
223
|
+
stores.set(info.workspacePath, group);
|
|
224
|
+
}
|
|
225
|
+
return projectUsageReport2([...stores].map(([workspacePath, sessions]) => ({ workspacePath, sessions, failure: null })), window, readAt);
|
|
226
|
+
} catch (cause) {
|
|
227
|
+
return projectUsageReport2([
|
|
228
|
+
{
|
|
229
|
+
workspacePath: null,
|
|
230
|
+
sessions: [],
|
|
231
|
+
failure: { message: cause instanceof Error ? cause.message : String(cause) }
|
|
232
|
+
}
|
|
233
|
+
], window, readAt);
|
|
234
|
+
} finally {
|
|
235
|
+
database?.close();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export { USAGE_EVENTS2, localDay2, shiftDay2, projectUsageReport2, readNyteUsage2 };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
seedSigner2
|
|
3
|
+
} from "./canvas-preview-31w7xm8z.js";
|
|
4
|
+
import {
|
|
5
|
+
readKeyFile2
|
|
6
|
+
} from "./canvas-preview-wg80rbc2.js";
|
|
7
|
+
|
|
8
|
+
// src/identity-key.ts
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
async function identitySigner2(id, home) {
|
|
11
|
+
return seedSigner2(id, await readKeyFile2(join(home, ".nyte", "identity.key"), "identity key"));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { identitySigner2 };
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// src/mcp/oauth.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
McpOAuthAuthorizationRequiredError,
|
|
5
|
+
McpOAuthProvider,
|
|
6
|
+
OAuthCallbackServer,
|
|
7
|
+
authorizeMcp,
|
|
8
|
+
parseWwwAuthenticate,
|
|
9
|
+
stepUpScope
|
|
10
|
+
} from "@earendil-works/pi-mcp/oauth";
|
|
11
|
+
import { closable } from "@nyte-ai/core";
|
|
12
|
+
import { Type } from "typebox";
|
|
13
|
+
import { Value } from "typebox/value";
|
|
14
|
+
var CALLBACK_HOST = "127.0.0.1";
|
|
15
|
+
var CALLBACK_PATH = "/callback";
|
|
16
|
+
var FALLBACK_REDIRECT_URL = `http://${CALLBACK_HOST}${CALLBACK_PATH}`;
|
|
17
|
+
var REFRESH_SKEW_MS = 30000;
|
|
18
|
+
var REQUEST_TIMEOUT_MS = 15000;
|
|
19
|
+
var SIGN_IN_TIMEOUT_MS = 5 * 60 * 1000;
|
|
20
|
+
var tokens = Type.Object({
|
|
21
|
+
access_token: Type.String(),
|
|
22
|
+
token_type: Type.String(),
|
|
23
|
+
expires_in: Type.Optional(Type.Number()),
|
|
24
|
+
scope: Type.Optional(Type.String()),
|
|
25
|
+
refresh_token: Type.Optional(Type.String()),
|
|
26
|
+
id_token: Type.Optional(Type.String())
|
|
27
|
+
});
|
|
28
|
+
var storedState = Type.Object({
|
|
29
|
+
serverUrl: Type.String(),
|
|
30
|
+
clientInformation: Type.Optional(Type.Object({
|
|
31
|
+
client_id: Type.String(),
|
|
32
|
+
client_secret: Type.Optional(Type.String()),
|
|
33
|
+
client_id_issued_at: Type.Optional(Type.Number()),
|
|
34
|
+
client_secret_expires_at: Type.Optional(Type.Number()),
|
|
35
|
+
redirect_uris: Type.Optional(Type.Array(Type.String()))
|
|
36
|
+
})),
|
|
37
|
+
tokens: Type.Optional(tokens),
|
|
38
|
+
tokensExpireAt: Type.Optional(Type.Number()),
|
|
39
|
+
codeVerifier: Type.Optional(Type.String()),
|
|
40
|
+
oauthState: Type.Optional(Type.String())
|
|
41
|
+
});
|
|
42
|
+
function oauthKey2(server, url) {
|
|
43
|
+
return `oauth:${server}:${createHash("sha256").update(url).digest("hex").slice(0, 16)}`;
|
|
44
|
+
}
|
|
45
|
+
function oauthStore2(secrets, key) {
|
|
46
|
+
return {
|
|
47
|
+
async load() {
|
|
48
|
+
const text = await secrets.get(key);
|
|
49
|
+
const parsed = text === undefined ? undefined : JSON.parse(text);
|
|
50
|
+
return Value.Check(storedState, parsed) ? parsed : undefined;
|
|
51
|
+
},
|
|
52
|
+
save: (state) => secrets.set(key, JSON.stringify({ ...state, discovery: undefined }))
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function createAuthProvider2(input) {
|
|
56
|
+
const { serverUrl, store } = input;
|
|
57
|
+
let refreshing;
|
|
58
|
+
const refresh = (stale, fetch = globalThis.fetch, challenge) => {
|
|
59
|
+
refreshing ??= (async () => {
|
|
60
|
+
const state = await store.load();
|
|
61
|
+
if (state?.tokens?.access_token !== stale)
|
|
62
|
+
return;
|
|
63
|
+
if (state?.tokens?.refresh_token === undefined)
|
|
64
|
+
throw new McpOAuthAuthorizationRequiredError;
|
|
65
|
+
const settings = input.settings();
|
|
66
|
+
const redirectUrl = fixedRedirectUrl(settings) ?? registeredRedirectUrls(state.clientInformation)[0] ?? FALLBACK_REDIRECT_URL;
|
|
67
|
+
const provider = createProvider(serverUrl, store, settings, redirectUrl, () => {
|
|
68
|
+
return;
|
|
69
|
+
});
|
|
70
|
+
const result = await authorizeMcp(provider, {
|
|
71
|
+
serverUrl,
|
|
72
|
+
resourceMetadataUrl: challenge?.resourceMetadataUrl,
|
|
73
|
+
authorizationServerMetadataUrl: settings.authServerMetadataUrl,
|
|
74
|
+
scope: challenge?.scope,
|
|
75
|
+
fetch: timedFetch(fetch)
|
|
76
|
+
});
|
|
77
|
+
if (result === "REDIRECT")
|
|
78
|
+
throw new McpOAuthAuthorizationRequiredError;
|
|
79
|
+
})().finally(() => {
|
|
80
|
+
refreshing = undefined;
|
|
81
|
+
});
|
|
82
|
+
return refreshing;
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
async token() {
|
|
86
|
+
await refreshing?.catch(() => {
|
|
87
|
+
return;
|
|
88
|
+
});
|
|
89
|
+
const state = await store.load();
|
|
90
|
+
const token = state?.tokens?.access_token;
|
|
91
|
+
const expired = state?.tokensExpireAt !== undefined && state.tokensExpireAt - REFRESH_SKEW_MS <= Date.now();
|
|
92
|
+
if (!expired || state?.tokens?.refresh_token === undefined)
|
|
93
|
+
return token;
|
|
94
|
+
await refresh(token).catch(() => {
|
|
95
|
+
return;
|
|
96
|
+
});
|
|
97
|
+
return (await store.load())?.tokens?.access_token;
|
|
98
|
+
},
|
|
99
|
+
async onUnauthorized(context) {
|
|
100
|
+
const challenge = parseWwwAuthenticate(context.response.headers.get("www-authenticate"));
|
|
101
|
+
input.onChallenge(challenge);
|
|
102
|
+
if (challenge.error === "insufficient_scope")
|
|
103
|
+
throw new McpOAuthAuthorizationRequiredError;
|
|
104
|
+
await refresh(context.token, context.fetch, challenge);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function signIn2(input) {
|
|
109
|
+
const queued = [];
|
|
110
|
+
const wakers = new Set;
|
|
111
|
+
let pasted = null;
|
|
112
|
+
const controller = new AbortController;
|
|
113
|
+
const wakeAll = () => {
|
|
114
|
+
for (const wake of wakers)
|
|
115
|
+
wake();
|
|
116
|
+
wakers.clear();
|
|
117
|
+
};
|
|
118
|
+
const deliver = (step) => {
|
|
119
|
+
queued.push(step);
|
|
120
|
+
wakeAll();
|
|
121
|
+
};
|
|
122
|
+
(async () => {
|
|
123
|
+
try {
|
|
124
|
+
await authorize({
|
|
125
|
+
...input,
|
|
126
|
+
signal: controller.signal,
|
|
127
|
+
show(url) {
|
|
128
|
+
deliver({ kind: "open", url: url.href });
|
|
129
|
+
pasted = Promise.withResolvers();
|
|
130
|
+
deliver({
|
|
131
|
+
kind: "paste",
|
|
132
|
+
message: "If the browser can't reach this machine, paste the URL it ended on",
|
|
133
|
+
secret: false
|
|
134
|
+
});
|
|
135
|
+
return pasted.promise;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
input.signedIn();
|
|
139
|
+
deliver({ kind: "done" });
|
|
140
|
+
} catch (error) {
|
|
141
|
+
deliver({
|
|
142
|
+
kind: "failed",
|
|
143
|
+
reason: controller.signal.aborted ? "Sign-in cancelled." : `Sign-in to ${input.server} failed: ${error instanceof Error ? error.message : String(error)}`
|
|
144
|
+
});
|
|
145
|
+
} finally {
|
|
146
|
+
input.ended();
|
|
147
|
+
}
|
|
148
|
+
})();
|
|
149
|
+
return {
|
|
150
|
+
steps: () => closable(() => {
|
|
151
|
+
let finished = false;
|
|
152
|
+
return {
|
|
153
|
+
async pull() {
|
|
154
|
+
if (finished)
|
|
155
|
+
return { done: true, value: undefined };
|
|
156
|
+
let step = queued.shift();
|
|
157
|
+
while (step === undefined && !controller.signal.aborted) {
|
|
158
|
+
await new Promise((resolve) => wakers.add(resolve));
|
|
159
|
+
step = queued.shift();
|
|
160
|
+
}
|
|
161
|
+
if (step === undefined)
|
|
162
|
+
return { done: true, value: undefined };
|
|
163
|
+
finished = step.kind === "done" || step.kind === "failed";
|
|
164
|
+
return { done: false, value: step };
|
|
165
|
+
},
|
|
166
|
+
close() {
|
|
167
|
+
controller.abort();
|
|
168
|
+
wakeAll();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}),
|
|
172
|
+
async answer(code) {
|
|
173
|
+
pasted?.resolve(code);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async function authorize(input) {
|
|
178
|
+
const { serverUrl, store, settings, challenge, signal } = input;
|
|
179
|
+
const stored = await store.load();
|
|
180
|
+
const stepUp = challenge?.error === "insufficient_scope";
|
|
181
|
+
const fixed = fixedRedirectUrl(settings);
|
|
182
|
+
const configured = new URL(settings.callbackUrl ?? `http://${CALLBACK_HOST}${CALLBACK_PATH}`);
|
|
183
|
+
const registered = registeredRedirectUrls(stored?.clientInformation)[0];
|
|
184
|
+
const configuredPort = configured.port === "" ? settings.callbackPort : Number(configured.port);
|
|
185
|
+
const preferred = configuredPort ?? (registered === undefined ? undefined : Number(new URL(registered).port) || undefined);
|
|
186
|
+
const callback = await listen(configured, preferred, configuredPort !== undefined);
|
|
187
|
+
const redirectUrl = fixed ?? callback.redirectUrl;
|
|
188
|
+
try {
|
|
189
|
+
if (stored !== undefined) {
|
|
190
|
+
const keep = settings.clientId !== undefined || registeredRedirectUrls(stored.clientInformation).includes(redirectUrl);
|
|
191
|
+
const next = keep ? { ...stored, oauthState: undefined } : { serverUrl: stored.serverUrl };
|
|
192
|
+
await store.save(next);
|
|
193
|
+
}
|
|
194
|
+
let authorizationUrl;
|
|
195
|
+
const provider = createProvider(serverUrl, store, settings, redirectUrl, (url) => {
|
|
196
|
+
authorizationUrl = url;
|
|
197
|
+
});
|
|
198
|
+
const flow = {
|
|
199
|
+
serverUrl,
|
|
200
|
+
fetch: timedFetch(),
|
|
201
|
+
signal,
|
|
202
|
+
resourceMetadataUrl: challenge?.resourceMetadataUrl,
|
|
203
|
+
authorizationServerMetadataUrl: settings.authServerMetadataUrl,
|
|
204
|
+
scope: mergeScopes(settings.scope, stepUp ? stepUpScope(stored?.tokens?.scope, challenge?.scope) : challenge?.scope)
|
|
205
|
+
};
|
|
206
|
+
if (await authorizeMcp(provider, { ...flow, skipRefresh: stepUp }) === "AUTHORIZED")
|
|
207
|
+
return;
|
|
208
|
+
if (authorizationUrl === undefined)
|
|
209
|
+
throw new Error("The server did not give an authorization page.");
|
|
210
|
+
const state = await provider.state();
|
|
211
|
+
const expected = new URL(authorizationUrl.searchParams.get("redirect_uri") ?? redirectUrl);
|
|
212
|
+
signal.throwIfAborted();
|
|
213
|
+
const pasted = input.show(authorizationUrl);
|
|
214
|
+
const response = await Promise.race([
|
|
215
|
+
callback.waitForCallback(state, expected.pathname),
|
|
216
|
+
pasted.then((text) => fromRedirectUrl(text, state, expected)),
|
|
217
|
+
new Promise((_resolve, reject) => signal.addEventListener("abort", () => reject(new Error("Sign-in cancelled.")), {
|
|
218
|
+
once: true
|
|
219
|
+
}))
|
|
220
|
+
]);
|
|
221
|
+
await authorizeMcp(provider, { ...flow, authorizationCode: response.code, iss: response.iss });
|
|
222
|
+
} finally {
|
|
223
|
+
await callback.close();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function fromRedirectUrl(text, state, expected) {
|
|
227
|
+
const url = URL.parse(text.trim());
|
|
228
|
+
if (url === null)
|
|
229
|
+
throw new Error("Paste the full URL from the browser's address bar.");
|
|
230
|
+
if (url.origin !== expected.origin || url.pathname !== expected.pathname)
|
|
231
|
+
throw new Error("That URL is not this sign-in's redirect.");
|
|
232
|
+
const error = url.searchParams.get("error");
|
|
233
|
+
if (error !== null)
|
|
234
|
+
throw new Error(url.searchParams.get("error_description") ?? error);
|
|
235
|
+
if (url.searchParams.get("state") !== state)
|
|
236
|
+
throw new Error("That URL belongs to a different sign-in.");
|
|
237
|
+
const code = url.searchParams.get("code");
|
|
238
|
+
if (code === null)
|
|
239
|
+
throw new Error("That URL has no authorization code.");
|
|
240
|
+
return { code, iss: url.searchParams.get("iss") ?? undefined };
|
|
241
|
+
}
|
|
242
|
+
async function listen(configured, port, required) {
|
|
243
|
+
const address = configured.hostname.replace(/^\[|\]$/g, "");
|
|
244
|
+
const options = {
|
|
245
|
+
host: address === "localhost" ? CALLBACK_HOST : address,
|
|
246
|
+
redirectHost: address,
|
|
247
|
+
path: configured.pathname,
|
|
248
|
+
timeoutMs: SIGN_IN_TIMEOUT_MS
|
|
249
|
+
};
|
|
250
|
+
try {
|
|
251
|
+
return await OAuthCallbackServer.listen({ ...options, port: port ?? 0 });
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (required || port === undefined)
|
|
254
|
+
throw error;
|
|
255
|
+
return OAuthCallbackServer.listen(options);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function fixedRedirectUrl(settings) {
|
|
259
|
+
if (settings.callbackUrl !== undefined && new URL(settings.callbackUrl).port !== "")
|
|
260
|
+
return settings.callbackUrl;
|
|
261
|
+
if (settings.callbackPort === undefined)
|
|
262
|
+
return;
|
|
263
|
+
const url = new URL(settings.callbackUrl ?? `http://${CALLBACK_HOST}${CALLBACK_PATH}`);
|
|
264
|
+
url.port = String(settings.callbackPort);
|
|
265
|
+
return url.href;
|
|
266
|
+
}
|
|
267
|
+
function createProvider(serverUrl, store, settings, redirectUrl, onRedirect) {
|
|
268
|
+
return new McpOAuthProvider({
|
|
269
|
+
serverUrl,
|
|
270
|
+
redirectUrl,
|
|
271
|
+
clientMetadata: { client_name: settings.clientName ?? "Nyte" },
|
|
272
|
+
clientId: settings.clientId,
|
|
273
|
+
clientSecret: settings.clientSecret,
|
|
274
|
+
store,
|
|
275
|
+
onRedirect
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
function registeredRedirectUrls(client) {
|
|
279
|
+
return client !== undefined && "redirect_uris" in client ? client.redirect_uris : [];
|
|
280
|
+
}
|
|
281
|
+
function mergeScopes(...scopes) {
|
|
282
|
+
const merged = [...new Set(scopes.flatMap((scope) => scope?.split(/\s+/).filter(Boolean) ?? []))];
|
|
283
|
+
return merged.length > 0 ? merged.join(" ") : undefined;
|
|
284
|
+
}
|
|
285
|
+
function timedFetch(fetch = globalThis.fetch) {
|
|
286
|
+
return (url, init) => {
|
|
287
|
+
const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
288
|
+
return fetch(url, {
|
|
289
|
+
...init,
|
|
290
|
+
signal: init?.signal ? AbortSignal.any([init.signal, timeout]) : timeout
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export { oauthKey2, oauthStore2, createAuthProvider2, signIn2 };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/deploy/vercel/server/utils/step.ts
|
|
2
|
+
function stepFrom2(report, now) {
|
|
3
|
+
switch (report.kind) {
|
|
4
|
+
case "continue":
|
|
5
|
+
return { kind: "advance" };
|
|
6
|
+
case "waiting":
|
|
7
|
+
return report.until === null ? { kind: "stop" } : untilStep(report.until, now);
|
|
8
|
+
case "retry":
|
|
9
|
+
return untilStep(report.until, now);
|
|
10
|
+
case "busy":
|
|
11
|
+
case "idle":
|
|
12
|
+
case "fenced":
|
|
13
|
+
return { kind: "stop" };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function untilStep(until, now) {
|
|
17
|
+
return until > now ? { kind: "sleep", ms: until - now } : { kind: "advance" };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { stepFrom2 };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/store-rows.ts
|
|
2
|
+
import { asId, SdkError } from "@nyte-ai/schema";
|
|
3
|
+
import { loggedEvent } from "@nyte-ai/core/store";
|
|
4
|
+
function prefixEnd2(prefix) {
|
|
5
|
+
const points = Array.from(prefix);
|
|
6
|
+
const last = points.pop()?.codePointAt(0);
|
|
7
|
+
if (last === undefined)
|
|
8
|
+
return null;
|
|
9
|
+
if (last === 1114111)
|
|
10
|
+
return prefixEnd2(points.join(""));
|
|
11
|
+
return points.join("") + String.fromCodePoint(last === 55295 ? 57344 : last + 1);
|
|
12
|
+
}
|
|
13
|
+
function mintRevision2() {
|
|
14
|
+
return asId(crypto.randomUUID());
|
|
15
|
+
}
|
|
16
|
+
function decodeEvent2(seq, at, text) {
|
|
17
|
+
return loggedEvent(seq, at, JSON.parse(text));
|
|
18
|
+
}
|
|
19
|
+
function parseNumber2(value) {
|
|
20
|
+
if (typeof value === "number")
|
|
21
|
+
return value;
|
|
22
|
+
if (typeof value === "bigint" || typeof value === "string")
|
|
23
|
+
return Number(value);
|
|
24
|
+
throw new SdkError("internal", "A stored number is malformed.");
|
|
25
|
+
}
|
|
26
|
+
function parseText2(value) {
|
|
27
|
+
if (value === null || value === undefined)
|
|
28
|
+
return null;
|
|
29
|
+
if (typeof value === "string")
|
|
30
|
+
return value;
|
|
31
|
+
throw new SdkError("internal", "A stored text column is malformed.");
|
|
32
|
+
}
|
|
33
|
+
function parseRef2(row) {
|
|
34
|
+
const name = parseText2(row.name);
|
|
35
|
+
const revision = parseText2(row.revision);
|
|
36
|
+
const target = parseText2(row.target);
|
|
37
|
+
if (name === null || revision === null)
|
|
38
|
+
throw new SdkError("internal", "A stored ref is malformed.");
|
|
39
|
+
return {
|
|
40
|
+
name,
|
|
41
|
+
target: target === null ? null : asId(target),
|
|
42
|
+
revision: asId(revision)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function parseStoredRecord2(row) {
|
|
46
|
+
const name = parseText2(row.name);
|
|
47
|
+
const target = parseText2(row.target);
|
|
48
|
+
const revision = parseText2(row.revision);
|
|
49
|
+
const body = parseText2(row.body);
|
|
50
|
+
if (name === null || target === null || revision === null || body === null)
|
|
51
|
+
throw new SdkError("internal", "A stored ref is malformed.");
|
|
52
|
+
return { name, target: asId(target), revision, body };
|
|
53
|
+
}
|
|
54
|
+
function parseStoredObject2(row) {
|
|
55
|
+
const id = parseText2(row.id);
|
|
56
|
+
const body = parseText2(row.body);
|
|
57
|
+
if (id === null || body === null)
|
|
58
|
+
throw new SdkError("internal", "A stored object is malformed.");
|
|
59
|
+
return { id: asId(id), body };
|
|
60
|
+
}
|
|
61
|
+
function loggedRow2(row) {
|
|
62
|
+
const event = parseText2(row.event);
|
|
63
|
+
if (event === null)
|
|
64
|
+
throw new SdkError("internal", "A logged event is malformed.");
|
|
65
|
+
return { seq: parseNumber2(row.seq), event };
|
|
66
|
+
}
|
|
67
|
+
async function collect2(bytes) {
|
|
68
|
+
const chunks = [];
|
|
69
|
+
for await (const chunk of bytes)
|
|
70
|
+
chunks.push(chunk);
|
|
71
|
+
const joined = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.byteLength, 0));
|
|
72
|
+
let offset = 0;
|
|
73
|
+
for (const chunk of chunks) {
|
|
74
|
+
joined.set(chunk, offset);
|
|
75
|
+
offset += chunk.byteLength;
|
|
76
|
+
}
|
|
77
|
+
return joined;
|
|
78
|
+
}
|
|
79
|
+
function memoryReader2(bytes) {
|
|
80
|
+
return {
|
|
81
|
+
size: bytes.byteLength,
|
|
82
|
+
mime: null,
|
|
83
|
+
async* chunks() {
|
|
84
|
+
yield bytes;
|
|
85
|
+
},
|
|
86
|
+
close: async () => {
|
|
87
|
+
return;
|
|
88
|
+
},
|
|
89
|
+
[Symbol.asyncDispose]: async () => {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { prefixEnd2, mintRevision2, decodeEvent2, parseNumber2, parseText2, parseRef2, parseStoredRecord2, parseStoredObject2, loggedRow2, collect2, memoryReader2 };
|