plugin-ai-api 1.0.8 → 1.0.9
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/externalVersion.js +1 -1
- package/dist/server/collections/ai-api-usage-records.js +63 -0
- package/dist/server/middleware/role-permission.js +15 -5
- package/dist/server/plugin.js +8 -1
- package/dist/server/routes/auth.js +37 -7
- package/dist/server/routes/router.js +22 -1
- package/dist/server/usage.js +80 -0
- package/package.json +1 -1
- package/src/server/collections/ai-api-usage-records.ts +33 -0
- package/src/server/middleware/role-permission.ts +79 -66
- package/src/server/plugin.ts +99 -89
- package/src/server/routes/auth.ts +142 -111
- package/src/server/routes/router.ts +304 -283
- package/src/server/usage.ts +52 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Context } from '@nocobase/actions';
|
|
2
|
+
|
|
3
|
+
type Usage = { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number };
|
|
4
|
+
|
|
5
|
+
export async function startUsageRecord(
|
|
6
|
+
ctx: Context,
|
|
7
|
+
requestId: string,
|
|
8
|
+
endpoint: string,
|
|
9
|
+
model: string,
|
|
10
|
+
streaming: boolean,
|
|
11
|
+
) {
|
|
12
|
+
const body = (ctx.request.body || {}) as Record<string, unknown>;
|
|
13
|
+
const messages = Array.isArray(body.messages) ? body.messages : undefined;
|
|
14
|
+
const oauth = ctx.state.oauthPrincipal as { clientId?: string; subject?: string; scopes?: string[] } | undefined;
|
|
15
|
+
const record = await ctx.db.getRepository('aiApiUsageRecords').create({
|
|
16
|
+
values: {
|
|
17
|
+
requestId,
|
|
18
|
+
userId: String(ctx.state.currentUser?.id),
|
|
19
|
+
roleName: ctx.state.currentRole || ctx.state.currentRoles?.[0] || 'unknown',
|
|
20
|
+
authType: ctx.state.aiApiAuthType || (oauth ? 'oidc' : 'session'),
|
|
21
|
+
oauthClientId: oauth?.clientId,
|
|
22
|
+
oauthSubject: oauth?.subject,
|
|
23
|
+
oauthScopes: oauth?.scopes,
|
|
24
|
+
endpoint,
|
|
25
|
+
mode: ctx.get('X-AI-Mode') || undefined,
|
|
26
|
+
model: model === '-' ? undefined : model,
|
|
27
|
+
status: 'pending',
|
|
28
|
+
streaming,
|
|
29
|
+
startedAt: new Date(),
|
|
30
|
+
requestMetadata: { messageCount: messages?.length, requestedMaxTokens: body.max_tokens },
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
return record.id;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function finishUsageRecord(ctx: Context, id: unknown, startedAt: number, status: 'succeeded' | 'failed') {
|
|
37
|
+
const response = (ctx.body || {}) as { usage?: Usage; id?: string; error?: { code?: string } };
|
|
38
|
+
const usage = response.usage;
|
|
39
|
+
const values = {
|
|
40
|
+
status,
|
|
41
|
+
httpStatus: ctx.status,
|
|
42
|
+
errorCode: response.error?.code,
|
|
43
|
+
inputTokens: usage?.prompt_tokens,
|
|
44
|
+
outputTokens: usage?.completion_tokens,
|
|
45
|
+
totalTokens: usage?.total_tokens,
|
|
46
|
+
providerRequestId: response.id,
|
|
47
|
+
completedAt: new Date(),
|
|
48
|
+
durationMs: Date.now() - startedAt,
|
|
49
|
+
responseMetadata: { usageSource: usage ? 'response' : 'unavailable' },
|
|
50
|
+
};
|
|
51
|
+
await ctx.db.getRepository('aiApiUsageRecords').update({ filterByTk: id, values });
|
|
52
|
+
}
|