plugin-ai-api 1.0.7 → 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/client/index.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +9 -9
- 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/agent-completions.js +53 -53
- package/dist/server/routes/auth.js +37 -7
- package/dist/server/routes/router.js +22 -1
- package/dist/server/usage.js +80 -0
- package/dist/server/utils/ai-employee-runtime.js +59 -0
- package/package.json +1 -1
- package/src/server/__tests__/agent-completions.test.ts +26 -0
- 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/agent-completions.ts +450 -428
- package/src/server/routes/auth.ts +142 -111
- package/src/server/routes/router.ts +304 -283
- package/src/server/usage.ts +52 -0
- package/src/server/utils/ai-employee-runtime.ts +71 -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
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Context } from '@nocobase/actions';
|
|
2
|
+
|
|
3
|
+
interface AIEmployeeModelOptions {
|
|
4
|
+
llmService: string;
|
|
5
|
+
model: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface AIEmployeeConstructorOptions {
|
|
9
|
+
ctx: Context;
|
|
10
|
+
employee: unknown;
|
|
11
|
+
sessionId: string;
|
|
12
|
+
webSearch: boolean;
|
|
13
|
+
model: AIEmployeeModelOptions;
|
|
14
|
+
legacy: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface AIEmployeeRuntime {
|
|
18
|
+
stream(options: { userMessages: unknown[] }): Promise<void>;
|
|
19
|
+
invoke(options: { userMessages: unknown[] }): Promise<unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface AgentRuntimeContext {
|
|
23
|
+
ctx: Context;
|
|
24
|
+
source: 'api';
|
|
25
|
+
employee: unknown;
|
|
26
|
+
sessionId: string;
|
|
27
|
+
userId?: number;
|
|
28
|
+
messages: Array<{ role: string; content: unknown }>;
|
|
29
|
+
metadata: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface AgentRuntimeLifecycle {
|
|
33
|
+
runBeforeHooks(context: AgentRuntimeContext): Promise<void>;
|
|
34
|
+
runAfterHooks(
|
|
35
|
+
context: AgentRuntimeContext,
|
|
36
|
+
result: { succeeded: boolean; value?: unknown; error?: Error },
|
|
37
|
+
): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getAgentRuntimeLifecycle(ctx: Context): AgentRuntimeLifecycle | undefined {
|
|
41
|
+
return (ctx.app as typeof ctx.app & { agentRuntimeLifecycle?: AgentRuntimeLifecycle }).agentRuntimeLifecycle;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type AIEmployeeConstructor = new (options: AIEmployeeConstructorOptions) => AIEmployeeRuntime;
|
|
45
|
+
|
|
46
|
+
let cachedConstructor: AIEmployeeConstructor | null = null;
|
|
47
|
+
|
|
48
|
+
export async function loadAIEmployeeConstructor(): Promise<AIEmployeeConstructor> {
|
|
49
|
+
if (cachedConstructor) return cachedConstructor;
|
|
50
|
+
|
|
51
|
+
const modulePath = '@nocobase/plugin-ai/dist/server/ai-employees/ai-employee.js';
|
|
52
|
+
const module = (await import(/* webpackIgnore: true */ modulePath)) as {
|
|
53
|
+
AIEmployee?: AIEmployeeConstructor;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (typeof module.AIEmployee !== 'function') {
|
|
57
|
+
throw new Error('AIEmployee class is not exported by the installed plugin-ai runtime.');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
cachedConstructor = module.AIEmployee;
|
|
61
|
+
return cachedConstructor;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function createAIEmployeeOptions(
|
|
65
|
+
ctx: Context,
|
|
66
|
+
employee: unknown,
|
|
67
|
+
sessionId: string,
|
|
68
|
+
model: AIEmployeeModelOptions,
|
|
69
|
+
): AIEmployeeConstructorOptions {
|
|
70
|
+
return { ctx, employee, sessionId, webSearch: false, model, legacy: false };
|
|
71
|
+
}
|