-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentCredentials.ts
More file actions
224 lines (213 loc) · 6.54 KB
/
Copy pathagentCredentials.ts
File metadata and controls
224 lines (213 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { v } from "convex/values";
import {
internalMutation,
internalQuery,
mutation,
query,
} from "./_generated/server";
/**
* Per-user credentials for external ACP agents (Codex CLI, Claude Code,
* Cursor). A user may hold MULTIPLE credentials per agent (e.g. a work and
* a personal Claude account); each harness references one by id. Values are
* AES-256-GCM ciphertext produced by the FastAPI backend — Convex and the
* browser never see plaintext.
*
* Write path: browser → FastAPI (Clerk JWT) → AES-256-GCM encrypt →
* `create` (deploy key). Ciphertext is only ever returned to FastAPI via
* the internal queries.
*/
const KIND = v.union(
v.literal("auth_json"),
v.literal("api_key"),
v.literal("oauth_token"),
);
/** All credentials for the current user (frontend — metadata, no secrets). */
export const listMine = query({
handler: async (ctx) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) return [];
const rows = await ctx.db
.query("agentCredentials")
.withIndex("by_user", (q) => q.eq("userId", identity.subject))
.collect();
return rows
.map((row) => ({
_id: row._id,
agent: row.agent,
kind: row.kind,
label: row.label,
createdAt: row.createdAt,
lastUsedAt: row.lastUsedAt,
}))
.sort((a, b) => b.createdAt - a.createdAt);
},
});
/** Insert a new credential (FastAPI via deploy key). Returns the new id. */
export const create = internalMutation({
args: {
userId: v.string(),
agent: v.string(),
kind: KIND,
ciphertext: v.string(),
label: v.optional(v.string()),
},
handler: async (ctx, args) => {
return await ctx.db.insert("agentCredentials", {
...args,
createdAt: Date.now(),
});
},
});
/** Replace an existing credential's secret/label (FastAPI via deploy key). */
export const updateSecret = internalMutation({
args: {
credentialId: v.id("agentCredentials"),
userId: v.string(),
// `kind` is validated against the REQUEST's agent upstream, so a
// rotation aimed at the wrong row would otherwise write a kind that
// is invalid for the row's actual agent (corrupting it). Optional
// only for deploy-window compatibility — FastAPI always sends it.
agent: v.optional(v.string()),
kind: KIND,
ciphertext: v.string(),
label: v.optional(v.string()),
},
handler: async (ctx, args) => {
const row = await ctx.db.get(args.credentialId);
if (!row || row.userId !== args.userId) {
throw new Error("Credential not found");
}
if (args.agent !== undefined && row.agent !== args.agent) {
throw new Error(
`Credential belongs to '${row.agent}', not '${args.agent}'`,
);
}
await ctx.db.patch(args.credentialId, {
kind: args.kind,
ciphertext: args.ciphertext,
label: args.label ?? row.label,
createdAt: Date.now(),
});
return args.credentialId;
},
});
/** Fetch one credential by id, ciphertext included (FastAPI only). */
export const getById = internalQuery({
args: { credentialId: v.id("agentCredentials"), userId: v.string() },
handler: async (ctx, args) => {
const row = await ctx.db.get(args.credentialId);
if (!row || row.userId !== args.userId) return null;
return {
agent: row.agent,
kind: row.kind,
ciphertext: row.ciphertext,
label: row.label,
};
},
});
/** Most-recently-created credential for an agent (FastAPI fallback when a
* harness has no explicit credential link). */
export const getForAgent = internalQuery({
args: { userId: v.string(), agent: v.string() },
handler: async (ctx, args) => {
const rows = await ctx.db
.query("agentCredentials")
.withIndex("by_user_agent", (q) =>
q.eq("userId", args.userId).eq("agent", args.agent),
)
.collect();
if (rows.length === 0) return null;
const row = rows.sort(
(a, b) =>
b.createdAt - a.createdAt ||
b._creationTime - a._creationTime ||
(b._id > a._id ? 1 : -1),
)[0];
return {
credentialId: row._id,
agent: row.agent,
kind: row.kind,
ciphertext: row.ciphertext,
label: row.label,
};
},
});
/** Credentials for one user (FastAPI use — metadata, no secrets). */
export const listForUser = internalQuery({
args: { userId: v.string() },
handler: async (ctx, args) => {
const rows = await ctx.db
.query("agentCredentials")
.withIndex("by_user", (q) => q.eq("userId", args.userId))
.collect();
return rows.map((row) => ({
credentialId: row._id,
agent: row.agent,
kind: row.kind,
label: row.label,
createdAt: row.createdAt,
}));
},
});
/** Delete a credential the user owns, and unlink any harnesses using it. */
export const remove = mutation({
args: { credentialId: v.id("agentCredentials") },
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) throw new Error("Unauthenticated");
const row = await ctx.db.get(args.credentialId);
if (!row || row.userId !== identity.subject) {
throw new Error("Credential not found");
}
const harnesses = await ctx.db
.query("harnesses")
.withIndex("by_user", (q) => q.eq("userId", identity.subject))
.collect();
for (const harness of harnesses) {
if (harness.agentCredentialId === args.credentialId) {
await ctx.db.patch(harness._id, { agentCredentialId: undefined });
}
}
// Cascade-delete this credential's usage ledger so it doesn't dangle as
// orphaned-but-summed rows (agentUsage totals are summed from the ledger).
const usage = await ctx.db
.query("agentUsageLedger")
.withIndex("by_credential", (q) =>
q.eq("agentCredentialId", args.credentialId),
)
.collect();
for (const row of usage) {
await ctx.db.delete(row._id);
}
await ctx.db.delete(args.credentialId);
return { removed: true };
},
});
export const touch = internalMutation({
args: { credentialId: v.id("agentCredentials") },
handler: async (ctx, args) => {
const row = await ctx.db.get(args.credentialId);
if (row) await ctx.db.patch(args.credentialId, { lastUsedAt: Date.now() });
},
});
/**
* Store the latest Anthropic rate-limit snapshot for a credential (FastAPI via
* deploy key). Decoupled from the usage ledger so it lands even on a silent
* hard-limit turn that records no cost/tokens — that's exactly when the user
* most needs to see it. Owner re-checked.
*/
export const recordRateLimit = internalMutation({
args: {
credentialId: v.id("agentCredentials"),
userId: v.string(),
rateLimit: v.any(),
},
handler: async (ctx, args) => {
const row = await ctx.db.get(args.credentialId);
if (!row || row.userId !== args.userId) return;
await ctx.db.patch(args.credentialId, {
lastRateLimit: args.rateLimit,
lastRateLimitAt: Date.now(),
});
},
});