@opengeni/db 6.2.0-canary.36855269964001 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/canonical-human-identities.js +3 -3
- package/dist/capability-reconciliation.js +2 -2
- package/dist/{chunk-LNXRHVDB.js → chunk-3I4SGUQX.js} +18 -344
- package/dist/chunk-3I4SGUQX.js.map +1 -0
- package/dist/{chunk-XJQJJONR.js → chunk-457JAWAA.js} +2 -2
- package/dist/{chunk-4LHUMCYO.js → chunk-5NIBMHXQ.js} +4 -4
- package/dist/{chunk-DQPK3EYT.js → chunk-6ZTMU7BF.js} +2 -2
- package/dist/{chunk-AF5VZI4C.js → chunk-73ZUIXCL.js} +2 -2
- package/dist/{chunk-6MLUR3MS.js → chunk-DBX35PJN.js} +5 -5
- package/dist/{chunk-PYVCIEXX.js → chunk-FMEZG5WH.js} +4 -4
- package/dist/{chunk-3ZX6MRYR.js → chunk-I2VSFY4F.js} +2 -2
- package/dist/{chunk-2DA6RMU6.js → chunk-IWTPR5EV.js} +2 -2
- package/dist/{chunk-WAOVZEDU.js → chunk-RTVDWDBS.js} +2 -2
- package/dist/{chunk-NZVEJIPF.js → chunk-T2SHSMIB.js} +8 -118
- package/dist/chunk-T2SHSMIB.js.map +1 -0
- package/dist/{chunk-3EKE6MUD.js → chunk-UBNUEKCJ.js} +3 -3
- package/dist/{chunk-JPH5OE3M.js → chunk-VHPOPKEZ.js} +5 -231
- package/dist/chunk-VHPOPKEZ.js.map +1 -0
- package/dist/{chunk-ZCDPF2ZJ.js → chunk-VOL2VGEL.js} +4 -55
- package/dist/chunk-VOL2VGEL.js.map +1 -0
- package/dist/{chunk-T3KUH6UP.js → chunk-WKLDMSSX.js} +2 -2
- package/dist/claude-subscription-usage.d.ts +0 -2
- package/dist/connect-attempts.js +3 -3
- package/dist/editable-artifact-durable-export.js +2 -2
- package/dist/editable-artifacts.d.ts +0 -7
- package/dist/editable-artifacts.js +3 -5
- package/dist/index.d.ts +3 -30
- package/dist/index.js +5120 -5584
- package/dist/index.js.map +1 -1
- package/dist/integration-oauth-pending-states.d.ts +1 -7
- package/dist/knowledge-entries-schema.d.ts +0 -41
- package/dist/knowledge-indexing.d.ts +1 -25
- package/dist/managed-auth-session-sets.js +3 -3
- package/dist/mcp-operations.js +2 -2
- package/dist/organization-integration-policy.js +3 -3
- package/dist/provision-roles.js +1 -1
- package/dist/retained-provider-commands.js +6 -6
- package/dist/runtime-posture.d.ts +4 -9
- package/dist/schema.d.ts +1 -1538
- package/dist/schema.js +1 -19
- package/dist/session-background-commands.js +4 -4
- package/dist/session-command-output.js +5 -5
- package/dist/session-event-slices.js +3 -3
- package/dist/session-mcp-credential-rotation.js +5 -5
- package/dist/session-queue-commands.d.ts +0 -4
- package/dist/session-tenancy.js +3 -3
- package/dist/turn-initiator.d.ts +0 -5
- package/dist/video-generation.js +3 -4
- package/dist/workspace-artifacts.d.ts +0 -2
- package/dist/workspace-tool-defaults.js +4 -4
- package/package.json +7 -7
- package/src/claude-subscription-usage.ts +8 -21
- package/src/editable-artifacts.ts +1 -69
- package/src/index.ts +30 -246
- package/src/integration-oauth-pending-states.ts +8 -60
- package/src/knowledge-entries-schema.ts +0 -6
- package/src/knowledge-indexing.ts +0 -9
- package/src/provision-roles.ts +0 -29
- package/src/runtime-posture.ts +4 -130
- package/src/schema.ts +4 -232
- package/src/session-queue-commands.ts +0 -6
- package/src/turn-initiator.ts +0 -24
- package/src/video-generation.ts +0 -20
- package/src/workspace-artifacts.ts +0 -26
- package/dist/chunk-JPH5OE3M.js.map +0 -1
- package/dist/chunk-LNXRHVDB.js.map +0 -1
- package/dist/chunk-NZVEJIPF.js.map +0 -1
- package/dist/chunk-ZCDPF2ZJ.js.map +0 -1
- package/dist/claude-subscription-tokens.d.ts +0 -43
- package/dist/credit-debit-attribution.d.ts +0 -29
- package/dist/session-final-reply.d.ts +0 -8
- package/dist/usage-allowances.d.ts +0 -126
- package/drizzle/0550_organization_oauth_pending_states.sql +0 -15
- package/drizzle/0552_usage_allowances.sql +0 -1179
- package/drizzle/0553_non_model_debit_attribution.sql +0 -419
- package/drizzle/0554_video_allowance_refunds.sql +0 -149
- package/drizzle/0555_member_collaborator_permissions.sql +0 -165
- package/drizzle/0556_member_collaborator_permissions_backfill_index.sql +0 -40
- package/drizzle/0557_member_collaborator_permissions_backfill.sql +0 -69
- package/drizzle/0558_external_membership_removal_owner_rls.sql +0 -85
- package/src/claude-subscription-tokens.ts +0 -178
- package/src/credit-debit-attribution.ts +0 -81
- package/src/session-final-reply.ts +0 -79
- package/src/usage-allowances.ts +0 -391
- /package/dist/{chunk-XJQJJONR.js.map → chunk-457JAWAA.js.map} +0 -0
- /package/dist/{chunk-4LHUMCYO.js.map → chunk-5NIBMHXQ.js.map} +0 -0
- /package/dist/{chunk-DQPK3EYT.js.map → chunk-6ZTMU7BF.js.map} +0 -0
- /package/dist/{chunk-AF5VZI4C.js.map → chunk-73ZUIXCL.js.map} +0 -0
- /package/dist/{chunk-6MLUR3MS.js.map → chunk-DBX35PJN.js.map} +0 -0
- /package/dist/{chunk-PYVCIEXX.js.map → chunk-FMEZG5WH.js.map} +0 -0
- /package/dist/{chunk-3ZX6MRYR.js.map → chunk-I2VSFY4F.js.map} +0 -0
- /package/dist/{chunk-2DA6RMU6.js.map → chunk-IWTPR5EV.js.map} +0 -0
- /package/dist/{chunk-WAOVZEDU.js.map → chunk-RTVDWDBS.js.map} +0 -0
- /package/dist/{chunk-3EKE6MUD.js.map → chunk-UBNUEKCJ.js.map} +0 -0
- /package/dist/{chunk-T3KUH6UP.js.map → chunk-WKLDMSSX.js.map} +0 -0
|
@@ -11,9 +11,9 @@ import {
|
|
|
11
11
|
getCanonicalHumanIdentityProjection,
|
|
12
12
|
synchronizeCanonicalHumanLoginBindings,
|
|
13
13
|
validateCanonicalHumanSession
|
|
14
|
-
} from "./chunk-
|
|
15
|
-
import "./chunk-
|
|
16
|
-
import "./chunk-
|
|
14
|
+
} from "./chunk-73ZUIXCL.js";
|
|
15
|
+
import "./chunk-RTVDWDBS.js";
|
|
16
|
+
import "./chunk-VHPOPKEZ.js";
|
|
17
17
|
import "./chunk-S2TZ3F4L.js";
|
|
18
18
|
import "./chunk-PZ5AY32C.js";
|
|
19
19
|
export {
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
external_exports
|
|
3
|
-
} from "./chunk-4UJCXBR2.js";
|
|
4
1
|
import {
|
|
5
2
|
rawRows,
|
|
6
|
-
setSubjectRlsContext,
|
|
7
3
|
withRlsContext,
|
|
8
4
|
withWorkspaceRls
|
|
9
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-RTVDWDBS.js";
|
|
10
6
|
import {
|
|
11
7
|
connections,
|
|
12
8
|
creditLedgerEntries,
|
|
@@ -18,7 +14,7 @@ import {
|
|
|
18
14
|
workspaceVideoGenerationPolicies,
|
|
19
15
|
workspaceVideoGenerationQuotas,
|
|
20
16
|
workspaces
|
|
21
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-VHPOPKEZ.js";
|
|
22
18
|
|
|
23
19
|
// src/video-generation.ts
|
|
24
20
|
import {
|
|
@@ -30,295 +26,7 @@ import {
|
|
|
30
26
|
VideoGenerationOperationSummary,
|
|
31
27
|
retainedGeneratedVideoReferenceFromFile
|
|
32
28
|
} from "@opengeni/contracts";
|
|
33
|
-
import { and, eq, inArray, sql
|
|
34
|
-
|
|
35
|
-
// src/usage-allowances.ts
|
|
36
|
-
import { sql as sql2 } from "drizzle-orm";
|
|
37
|
-
|
|
38
|
-
// src/credit-debit-attribution.ts
|
|
39
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
40
|
-
import { sql } from "drizzle-orm";
|
|
41
|
-
var creditDebitAttributionContext = new AsyncLocalStorage();
|
|
42
|
-
function currentCreditDebitAttribution() {
|
|
43
|
-
return creditDebitAttributionContext.getStore() ?? { kind: "unknown" };
|
|
44
|
-
}
|
|
45
|
-
function withCreditDebitAttribution(attribution, fn) {
|
|
46
|
-
return creditDebitAttributionContext.run(
|
|
47
|
-
Object.freeze(CreditDebitAttribution.parse(attribution)),
|
|
48
|
-
fn
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
async function creditDebitAttributionForTurn(db, input) {
|
|
52
|
-
return withRlsContext(db, input, async (tx) => {
|
|
53
|
-
const [turn] = await rawRows(
|
|
54
|
-
tx,
|
|
55
|
-
sql`SELECT initiating_human_subject_id AS "initiatingHumanSubjectId"
|
|
56
|
-
FROM session_turns WHERE account_id=${input.accountId}::uuid
|
|
57
|
-
AND workspace_id=${input.workspaceId}::uuid AND id=${input.turnId}::uuid`
|
|
58
|
-
);
|
|
59
|
-
if (!turn) throw new Error("Paid credit debit initiating turn is unavailable");
|
|
60
|
-
return CreditDebitAttribution.parse({
|
|
61
|
-
kind: "turn",
|
|
62
|
-
turnId: input.turnId,
|
|
63
|
-
initiatingHumanSubjectId: turn.initiatingHumanSubjectId
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
var CreditDebitAttribution = external_exports.discriminatedUnion("kind", [
|
|
68
|
-
external_exports.object({
|
|
69
|
-
kind: external_exports.literal("turn"),
|
|
70
|
-
turnId: external_exports.uuid(),
|
|
71
|
-
initiatingHumanSubjectId: external_exports.string().min(1).max(1024).nullable()
|
|
72
|
-
}),
|
|
73
|
-
external_exports.object({
|
|
74
|
-
kind: external_exports.literal("human"),
|
|
75
|
-
initiatingHumanSubjectId: external_exports.string().min(1).max(1024)
|
|
76
|
-
}),
|
|
77
|
-
external_exports.object({ kind: external_exports.literal("service") }),
|
|
78
|
-
external_exports.object({ kind: external_exports.literal("unknown") })
|
|
79
|
-
]);
|
|
80
|
-
function creditDebitAttributionMetadata(attribution) {
|
|
81
|
-
switch (attribution.kind) {
|
|
82
|
-
case "turn":
|
|
83
|
-
return {
|
|
84
|
-
turnId: attribution.turnId,
|
|
85
|
-
...attribution.initiatingHumanSubjectId ? { initiatingHumanSubjectId: attribution.initiatingHumanSubjectId } : {}
|
|
86
|
-
};
|
|
87
|
-
case "human":
|
|
88
|
-
return { initiatingHumanSubjectId: attribution.initiatingHumanSubjectId };
|
|
89
|
-
case "service":
|
|
90
|
-
return {};
|
|
91
|
-
case "unknown":
|
|
92
|
-
throw new Error("Paid credit debit has no frozen initiating attribution");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// src/usage-allowances.ts
|
|
97
|
-
var UsageAllowanceVersionConflictError = class extends Error {
|
|
98
|
-
constructor() {
|
|
99
|
-
super("Usage allowance version conflict");
|
|
100
|
-
this.name = "UsageAllowanceVersionConflictError";
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
function amount(value, name, positive = false) {
|
|
104
|
-
if (!Number.isSafeInteger(value) || value < (positive ? 1 : 0)) {
|
|
105
|
-
throw new Error(
|
|
106
|
-
`${name} must be ${positive ? "a positive" : "a non-negative"} safe integer in USD micros`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function validateMemberAllowanceRule(rule) {
|
|
111
|
-
if (rule === null) return;
|
|
112
|
-
if ("credits" in rule) {
|
|
113
|
-
if (Object.keys(rule).length !== 1)
|
|
114
|
-
throw new Error("Member rule must have only credits or share");
|
|
115
|
-
amount(rule.credits, "credits");
|
|
116
|
-
} else if ("share" in rule) {
|
|
117
|
-
if (Object.keys(rule).length !== 1 || !Number.isFinite(rule.share) || rule.share < 0) {
|
|
118
|
-
throw new Error("share must be a finite non-negative number");
|
|
119
|
-
}
|
|
120
|
-
} else {
|
|
121
|
-
throw new Error("Member rule must have credits or share");
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function validateWorkspaceAllowanceConfig(config) {
|
|
125
|
-
amount(config.includedCredits, "includedCredits");
|
|
126
|
-
if (config.period !== "monthly" && config.period !== "none")
|
|
127
|
-
throw new Error("Invalid allowance period");
|
|
128
|
-
if (config.anchorDay !== void 0 && (!Number.isInteger(config.anchorDay) || config.anchorDay < 1 || config.anchorDay > 31)) {
|
|
129
|
-
throw new Error("anchorDay must be an integer from 1 to 31");
|
|
130
|
-
}
|
|
131
|
-
if (config.memberDefault !== void 0 && config.memberDefault !== "none" && config.memberDefault !== "equal_share") {
|
|
132
|
-
validateMemberAllowanceRule(config.memberDefault);
|
|
133
|
-
}
|
|
134
|
-
for (const list of [config.thresholds?.workspace, config.thresholds?.member]) {
|
|
135
|
-
if (list !== void 0 && (list.length > 16 || list.some((threshold) => !Number.isFinite(threshold) || threshold <= 0 || threshold > 1))) {
|
|
136
|
-
throw new Error(
|
|
137
|
-
"thresholds must contain at most 16 fractions greater than zero and at most one"
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async function command(db, input) {
|
|
143
|
-
if (["set", "clear", "member", "grant"].includes(String(input.action)) && currentCreditDebitAttribution().kind === "turn") {
|
|
144
|
-
throw new Error("Agent attempts cannot change usage allowances");
|
|
145
|
-
}
|
|
146
|
-
try {
|
|
147
|
-
const mutation = ["set", "clear", "member", "grant"].includes(String(input.action));
|
|
148
|
-
return await withRlsContext(
|
|
149
|
-
db,
|
|
150
|
-
input,
|
|
151
|
-
async (scoped) => {
|
|
152
|
-
if (mutation) {
|
|
153
|
-
await scoped.execute(sql2`select pg_advisory_xact_lock(
|
|
154
|
-
hashtextextended(${`organization-membership:${input.accountId}`},0))`);
|
|
155
|
-
await scoped.execute(sql2`select pg_advisory_xact_lock_shared(
|
|
156
|
-
hashtextextended(${`session-tenancy:${input.workspaceId}`},0))`);
|
|
157
|
-
}
|
|
158
|
-
if (typeof input.actorSubjectId === "string")
|
|
159
|
-
await setSubjectRlsContext(scoped, input.actorSubjectId);
|
|
160
|
-
const [row] = await rawRows(
|
|
161
|
-
scoped,
|
|
162
|
-
sql2`select usage_allowance_command(${JSON.stringify(input)}::jsonb) as result`
|
|
163
|
-
);
|
|
164
|
-
if (!row) throw new Error("Usage allowance command returned no result");
|
|
165
|
-
return row.result;
|
|
166
|
-
},
|
|
167
|
-
void 0,
|
|
168
|
-
mutation ? "none" : "shared"
|
|
169
|
-
);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
const cause = error;
|
|
172
|
-
if ((cause.code ?? cause.cause?.code) === "40001")
|
|
173
|
-
throw new UsageAllowanceVersionConflictError();
|
|
174
|
-
throw error;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function actor(input) {
|
|
178
|
-
return { actorSubjectId: input.actorSubjectId, actorType: input.actorType ?? "subject" };
|
|
179
|
-
}
|
|
180
|
-
function version(value) {
|
|
181
|
-
amount(value, "expectedVersion");
|
|
182
|
-
}
|
|
183
|
-
async function getWorkspaceAllowance(db, input) {
|
|
184
|
-
return await command(db, { ...input, action: "get" });
|
|
185
|
-
}
|
|
186
|
-
async function getWorkspaceAllowanceState(db, input) {
|
|
187
|
-
return await command(db, { ...input, action: "state" });
|
|
188
|
-
}
|
|
189
|
-
async function setWorkspaceAllowance(db, input) {
|
|
190
|
-
const { accountId, workspaceId, expectedVersion, actorSubjectId, actorType } = input;
|
|
191
|
-
const config = {
|
|
192
|
-
includedCredits: input.includedCredits,
|
|
193
|
-
period: input.period,
|
|
194
|
-
...input.anchorDay === void 0 ? {} : { anchorDay: input.anchorDay },
|
|
195
|
-
...input.memberDefault === void 0 ? {} : { memberDefault: input.memberDefault },
|
|
196
|
-
...input.thresholds === void 0 ? {} : { thresholds: input.thresholds }
|
|
197
|
-
};
|
|
198
|
-
version(expectedVersion);
|
|
199
|
-
validateWorkspaceAllowanceConfig(config);
|
|
200
|
-
return await command(db, {
|
|
201
|
-
accountId,
|
|
202
|
-
workspaceId,
|
|
203
|
-
expectedVersion,
|
|
204
|
-
...actor({ actorSubjectId, actorType }),
|
|
205
|
-
action: "set",
|
|
206
|
-
config
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
async function clearWorkspaceAllowance(db, input) {
|
|
210
|
-
version(input.expectedVersion);
|
|
211
|
-
if (input.expectedVersion === 0) throw new UsageAllowanceVersionConflictError();
|
|
212
|
-
if (input.operationId !== void 0 && (!input.operationId.trim() || new TextEncoder().encode(input.operationId).length > 256))
|
|
213
|
-
throw new Error("operationId must be non-empty and at most 256 bytes");
|
|
214
|
-
return await command(db, { ...input, ...actor(input), action: "clear" });
|
|
215
|
-
}
|
|
216
|
-
async function grantWorkspaceCredits(db, input) {
|
|
217
|
-
amount(input.credits, "credits", true);
|
|
218
|
-
if (!input.operationId.trim() || new TextEncoder().encode(input.operationId).length > 256) {
|
|
219
|
-
throw new Error("operationId must be non-empty and at most 256 bytes");
|
|
220
|
-
}
|
|
221
|
-
if (input.expiresAt != null && !Number.isFinite(Date.parse(input.expiresAt)))
|
|
222
|
-
throw new Error("Invalid grant expiresAt");
|
|
223
|
-
return await command(db, {
|
|
224
|
-
...input,
|
|
225
|
-
...actor(input),
|
|
226
|
-
expiresAt: input.expiresAt ?? null,
|
|
227
|
-
action: "grant"
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
async function setMemberAllowance(db, input) {
|
|
231
|
-
version(input.expectedVersion);
|
|
232
|
-
validateMemberAllowanceRule(input.rule);
|
|
233
|
-
return await command(db, { ...input, ...actor(input), action: "member" });
|
|
234
|
-
}
|
|
235
|
-
function allowanceUsage(limit, used, resetsAt, thresholds = [0.8, 1]) {
|
|
236
|
-
const fraction = limit === null ? null : limit === 0 ? 1 : used / limit;
|
|
237
|
-
const remaining = limit === null ? null : Math.max(0, limit - used);
|
|
238
|
-
return {
|
|
239
|
-
limit,
|
|
240
|
-
used,
|
|
241
|
-
remaining,
|
|
242
|
-
fraction,
|
|
243
|
-
status: remaining === 0 ? "exhausted" : fraction !== null && thresholds.some((t) => fraction >= t) ? "warning" : "ok",
|
|
244
|
-
resetsAt
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
async function getWorkspaceUsage(db, input) {
|
|
248
|
-
if (input.period !== void 0 && input.period !== "current" && !/^\d{4}-(0[1-9]|1[0-2])$/.test(input.period)) {
|
|
249
|
-
throw new Error("period must be current or YYYY-MM");
|
|
250
|
-
}
|
|
251
|
-
const pageSize = Math.min(500, Math.max(1, Math.trunc(input.limit ?? 100)));
|
|
252
|
-
const raw = await command(db, { ...input, limit: pageSize, action: "usage" });
|
|
253
|
-
return projectUsage(raw, pageSize);
|
|
254
|
-
}
|
|
255
|
-
function projectUsage(raw, pageSize) {
|
|
256
|
-
const config = raw.config;
|
|
257
|
-
const pool = config ? config.includedCredits + Number(raw.grantsUsed) + Number(raw.grantsRemaining) : null;
|
|
258
|
-
const memberPool = config ? config.includedCredits + Number(raw.grantsRemaining) : null;
|
|
259
|
-
const resetsAt = config ? raw.period.end : null;
|
|
260
|
-
const members = raw.members.slice(0, pageSize).map((member) => {
|
|
261
|
-
let rule = member.rule;
|
|
262
|
-
if (rule === null) {
|
|
263
|
-
const fallback = config?.memberDefault ?? "none";
|
|
264
|
-
rule = fallback === "none" ? null : fallback === "equal_share" ? { share: raw.memberCount > 0 ? 1 / Number(raw.memberCount) : 0 } : fallback;
|
|
265
|
-
}
|
|
266
|
-
const limit = rule === null ? null : "credits" in rule ? rule.credits : memberPool === null ? null : Math.min(Number.MAX_SAFE_INTEGER, Math.floor(memberPool * rule.share));
|
|
267
|
-
return {
|
|
268
|
-
...member,
|
|
269
|
-
rule: member.rule,
|
|
270
|
-
...allowanceUsage(limit, Number(member.used), resetsAt, config?.thresholds?.member)
|
|
271
|
-
};
|
|
272
|
-
});
|
|
273
|
-
return {
|
|
274
|
-
period: config ? raw.period : { start: null, end: null },
|
|
275
|
-
workspace: {
|
|
276
|
-
...allowanceUsage(pool, Number(raw.used), resetsAt, config?.thresholds?.workspace),
|
|
277
|
-
includedCredits: config?.includedCredits ?? 0,
|
|
278
|
-
grantsRemaining: Number(raw.grantsRemaining)
|
|
279
|
-
},
|
|
280
|
-
members,
|
|
281
|
-
nextCursor: raw.members.length > pageSize ? members.at(-1).subjectId : null
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
async function checkWorkspaceAllowance(db, input) {
|
|
285
|
-
const raw = await command(db, {
|
|
286
|
-
accountId: input.accountId,
|
|
287
|
-
workspaceId: input.workspaceId,
|
|
288
|
-
...input.subjectId ? { subjectId: input.subjectId } : {},
|
|
289
|
-
limit: 1,
|
|
290
|
-
action: "check"
|
|
291
|
-
});
|
|
292
|
-
const usage = projectUsage(raw, 1);
|
|
293
|
-
if (usage.workspace.status === "exhausted") {
|
|
294
|
-
return {
|
|
295
|
-
code: "allowance_exhausted",
|
|
296
|
-
scope: "workspace",
|
|
297
|
-
resetsAt: usage.workspace.resetsAt,
|
|
298
|
-
message: "The workspace usage allowance is exhausted."
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
const member = input.subjectId ? usage.members.find((row) => row.subjectId === input.subjectId) : null;
|
|
302
|
-
if (member?.status === "exhausted") {
|
|
303
|
-
return {
|
|
304
|
-
code: "allowance_exhausted",
|
|
305
|
-
scope: "member",
|
|
306
|
-
subjectId: member.subjectId,
|
|
307
|
-
resetsAt: member.resetsAt,
|
|
308
|
-
message: "The member usage allowance is exhausted."
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
return null;
|
|
312
|
-
}
|
|
313
|
-
async function maintainWorkspaceAllowances(db, input = {}) {
|
|
314
|
-
const [row] = await rawRows(
|
|
315
|
-
db,
|
|
316
|
-
sql2`select maintain_usage_allowances(${input.limit ?? 20}::integer,${input.memberLimit ?? 100}::integer) as processed`
|
|
317
|
-
);
|
|
318
|
-
return Number(row?.processed ?? 0);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// src/video-generation.ts
|
|
29
|
+
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
322
30
|
var ACTIVE_VIDEO_GENERATION_STATUSES = [
|
|
323
31
|
"preparing",
|
|
324
32
|
"prepared",
|
|
@@ -400,7 +108,7 @@ async function admitVideoGenerationOperation(db, input) {
|
|
|
400
108
|
{ accountId: input.accountId, workspaceId: input.workspaceId },
|
|
401
109
|
async (scopedDb) => await scopedDb.transaction(async (tx) => {
|
|
402
110
|
await tx.execute(
|
|
403
|
-
|
|
111
|
+
sql`select pg_advisory_xact_lock(hashtextextended(${`video-generation:${input.admissionKey}`}, 0))`
|
|
404
112
|
);
|
|
405
113
|
const [existing] = await tx.select().from(videoGenerationOperations).where(
|
|
406
114
|
and(
|
|
@@ -487,7 +195,7 @@ async function admitVideoGenerationOperation(db, input) {
|
|
|
487
195
|
);
|
|
488
196
|
}
|
|
489
197
|
await tx.update(workspaceVideoGenerationQuotas).set({
|
|
490
|
-
reservedBytes:
|
|
198
|
+
reservedBytes: sql`${workspaceVideoGenerationQuotas.reservedBytes} + ${reservedBytes}`,
|
|
491
199
|
updatedAt: /* @__PURE__ */ new Date()
|
|
492
200
|
}).where(eq(workspaceVideoGenerationQuotas.workspaceId, input.workspaceId));
|
|
493
201
|
return { operation, created: true };
|
|
@@ -615,7 +323,7 @@ async function cancelUnacceptedVideoGenerationsForToolCallsInTransaction(tx, inp
|
|
|
615
323
|
inArray(videoGenerationOperations.toolCallId, toolCallIds),
|
|
616
324
|
eq(videoGenerationOperations.admissionOutputState, "pending"),
|
|
617
325
|
inArray(videoGenerationOperations.status, ["preparing", "prepared"]),
|
|
618
|
-
|
|
326
|
+
sql`${videoGenerationOperations.providerRequestSentAt} is null`
|
|
619
327
|
)
|
|
620
328
|
).orderBy(videoGenerationOperations.id).for("update");
|
|
621
329
|
const cancelled = [];
|
|
@@ -648,7 +356,7 @@ async function cancelUnacceptedVideoGenerationsForToolCallsInTransaction(tx, inp
|
|
|
648
356
|
async function claimVideoGenerationOperations(db, input) {
|
|
649
357
|
const rows = await rawRows(
|
|
650
358
|
db,
|
|
651
|
-
|
|
359
|
+
sql`select * from opengeni_private.claim_video_generation_operations(
|
|
652
360
|
${input.owner}, ${input.leaseSeconds}, ${input.limit}
|
|
653
361
|
)`
|
|
654
362
|
);
|
|
@@ -839,7 +547,7 @@ async function settleVideoGenerationReady(db, input) {
|
|
|
839
547
|
throw new Error("Video artifact and File identities must be distinct");
|
|
840
548
|
}
|
|
841
549
|
await tx.execute(
|
|
842
|
-
|
|
550
|
+
sql`SELECT set_config('opengeni.private_file_owner',${operation.privateFileOwnerSubjectId ?? ""},true)`
|
|
843
551
|
);
|
|
844
552
|
const now = /* @__PURE__ */ new Date();
|
|
845
553
|
await tx.insert(files).values({
|
|
@@ -883,8 +591,8 @@ async function settleVideoGenerationReady(db, input) {
|
|
|
883
591
|
}).returning();
|
|
884
592
|
if (!artifact) throw new Error("Generated video artifact was not persisted");
|
|
885
593
|
await tx.update(workspaceVideoGenerationQuotas).set({
|
|
886
|
-
reservedBytes:
|
|
887
|
-
readyBytes:
|
|
594
|
+
reservedBytes: sql`${workspaceVideoGenerationQuotas.reservedBytes} - ${operation.reservedBytes}`,
|
|
595
|
+
readyBytes: sql`${workspaceVideoGenerationQuotas.readyBytes} + ${input.sizeBytes}`,
|
|
888
596
|
updatedAt: now
|
|
889
597
|
}).where(eq(workspaceVideoGenerationQuotas.workspaceId, input.workspaceId));
|
|
890
598
|
const [updated] = await tx.update(videoGenerationOperations).set({
|
|
@@ -933,7 +641,7 @@ async function listSessionGeneratedVideoArtifacts(db, workspaceId, sessionId) {
|
|
|
933
641
|
and(
|
|
934
642
|
eq(generatedVideoArtifacts.workspaceId, workspaceId),
|
|
935
643
|
eq(generatedVideoArtifacts.sessionId, sessionId),
|
|
936
|
-
|
|
644
|
+
sql`${generatedVideoArtifacts.deletedAt} is null`
|
|
937
645
|
)
|
|
938
646
|
).orderBy(generatedVideoArtifacts.createdAt, generatedVideoArtifacts.id)
|
|
939
647
|
);
|
|
@@ -1076,7 +784,7 @@ async function getVideoGenerationOperationSummary(db, workspaceId, operationId)
|
|
|
1076
784
|
workspaceId,
|
|
1077
785
|
(tx) => rawRows(
|
|
1078
786
|
tx,
|
|
1079
|
-
|
|
787
|
+
sql`SELECT nullif(current_setting('opengeni.private_file_owner',true),'') AS owner`
|
|
1080
788
|
)
|
|
1081
789
|
);
|
|
1082
790
|
if (context?.owner !== operation.privateFileOwnerSubjectId) return null;
|
|
@@ -1119,9 +827,9 @@ async function mutateOperation(db, input, mutate) {
|
|
|
1119
827
|
async function releaseReservedQuota(tx, operation, ready) {
|
|
1120
828
|
if (operation.quotaState !== "reserved") return;
|
|
1121
829
|
await tx.update(workspaceVideoGenerationQuotas).set({
|
|
1122
|
-
reservedBytes:
|
|
830
|
+
reservedBytes: sql`${workspaceVideoGenerationQuotas.reservedBytes} - ${operation.reservedBytes}`,
|
|
1123
831
|
...ready ? {
|
|
1124
|
-
readyBytes:
|
|
832
|
+
readyBytes: sql`${workspaceVideoGenerationQuotas.readyBytes} + ${operation.reservedBytes}`
|
|
1125
833
|
} : {},
|
|
1126
834
|
updatedAt: /* @__PURE__ */ new Date()
|
|
1127
835
|
}).where(eq(workspaceVideoGenerationQuotas.workspaceId, operation.workspaceId));
|
|
@@ -1130,23 +838,9 @@ async function debitVideoGenerationCredits(tx, operation) {
|
|
|
1130
838
|
if (operation.pricedCostMicros <= 0 || operation.fundingSource !== "opengeni_credits") {
|
|
1131
839
|
throw new Error("Managed video credit debit has an invalid funding binding");
|
|
1132
840
|
}
|
|
1133
|
-
|
|
1134
|
-
throw new VideoGenerationCreditError("Paid video initiating turn is unavailable");
|
|
1135
|
-
}
|
|
1136
|
-
await tx.execute(sql3`select pg_advisory_xact_lock(hashtext(${operation.accountId}))`);
|
|
1137
|
-
const attribution = await creditDebitAttributionForTurn(tx, {
|
|
1138
|
-
accountId: operation.accountId,
|
|
1139
|
-
workspaceId: operation.workspaceId,
|
|
1140
|
-
turnId: operation.turnId
|
|
1141
|
-
});
|
|
1142
|
-
const refusal = await checkWorkspaceAllowance(tx, {
|
|
1143
|
-
accountId: operation.accountId,
|
|
1144
|
-
workspaceId: operation.workspaceId,
|
|
1145
|
-
subjectId: attribution.kind === "turn" ? attribution.initiatingHumanSubjectId : null
|
|
1146
|
-
});
|
|
1147
|
-
if (refusal) throw Object.assign(new VideoGenerationCreditError(refusal.message), refusal);
|
|
841
|
+
await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${operation.accountId}))`);
|
|
1148
842
|
const [balanceRow] = await tx.select({
|
|
1149
|
-
balanceMicros:
|
|
843
|
+
balanceMicros: sql`coalesce(sum(${creditLedgerEntries.amountMicros}), 0)`
|
|
1150
844
|
}).from(creditLedgerEntries).where(eq(creditLedgerEntries.accountId, operation.accountId));
|
|
1151
845
|
const balanceMicros = Number(balanceRow?.balanceMicros ?? 0);
|
|
1152
846
|
if (!Number.isSafeInteger(balanceMicros) || balanceMicros < operation.pricedCostMicros) {
|
|
@@ -1162,7 +856,6 @@ async function debitVideoGenerationCredits(tx, operation) {
|
|
|
1162
856
|
sourceId: operation.id,
|
|
1163
857
|
idempotencyKey: `credit:video_generation_debit:${operation.id}`,
|
|
1164
858
|
metadata: {
|
|
1165
|
-
turnId: operation.turnId,
|
|
1166
859
|
modelId: operation.modelId,
|
|
1167
860
|
sourceMode: operation.sourceMode,
|
|
1168
861
|
pricedCostMicros: operation.pricedCostMicros
|
|
@@ -1192,7 +885,7 @@ async function refundVideoGenerationCredits(tx, operation, now) {
|
|
|
1192
885
|
if (operation.fundingSource !== "opengeni_credits" || operation.pricedCostMicros <= 0) {
|
|
1193
886
|
throw new Error("Video generation credit refund has an invalid funding binding");
|
|
1194
887
|
}
|
|
1195
|
-
await tx.execute(
|
|
888
|
+
await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${operation.accountId}))`);
|
|
1196
889
|
const inserted = await tx.insert(creditLedgerEntries).values({
|
|
1197
890
|
accountId: operation.accountId,
|
|
1198
891
|
workspaceId: operation.workspaceId,
|
|
@@ -1202,7 +895,6 @@ async function refundVideoGenerationCredits(tx, operation, now) {
|
|
|
1202
895
|
sourceId: operation.id,
|
|
1203
896
|
idempotencyKey: `credit:video_generation_refund:${operation.id}`,
|
|
1204
897
|
metadata: {
|
|
1205
|
-
turnId: operation.turnId,
|
|
1206
898
|
modelId: operation.modelId,
|
|
1207
899
|
sourceMode: operation.sourceMode,
|
|
1208
900
|
pricedCostMicros: operation.pricedCostMicros
|
|
@@ -1283,24 +975,6 @@ function boundPrivateError(value) {
|
|
|
1283
975
|
}
|
|
1284
976
|
|
|
1285
977
|
export {
|
|
1286
|
-
currentCreditDebitAttribution,
|
|
1287
|
-
withCreditDebitAttribution,
|
|
1288
|
-
creditDebitAttributionForTurn,
|
|
1289
|
-
CreditDebitAttribution,
|
|
1290
|
-
creditDebitAttributionMetadata,
|
|
1291
|
-
UsageAllowanceVersionConflictError,
|
|
1292
|
-
validateMemberAllowanceRule,
|
|
1293
|
-
validateWorkspaceAllowanceConfig,
|
|
1294
|
-
getWorkspaceAllowance,
|
|
1295
|
-
getWorkspaceAllowanceState,
|
|
1296
|
-
setWorkspaceAllowance,
|
|
1297
|
-
clearWorkspaceAllowance,
|
|
1298
|
-
grantWorkspaceCredits,
|
|
1299
|
-
setMemberAllowance,
|
|
1300
|
-
allowanceUsage,
|
|
1301
|
-
getWorkspaceUsage,
|
|
1302
|
-
checkWorkspaceAllowance,
|
|
1303
|
-
maintainWorkspaceAllowances,
|
|
1304
978
|
ACTIVE_VIDEO_GENERATION_STATUSES,
|
|
1305
979
|
VideoGenerationConflictError,
|
|
1306
980
|
VideoGenerationCapacityError,
|
|
@@ -1332,4 +1006,4 @@ export {
|
|
|
1332
1006
|
mediaGenerationResultForStoredOperation,
|
|
1333
1007
|
getVideoGenerationOperationSummary
|
|
1334
1008
|
};
|
|
1335
|
-
//# sourceMappingURL=chunk-
|
|
1009
|
+
//# sourceMappingURL=chunk-3I4SGUQX.js.map
|