plugin-ai-api 1.0.7 → 1.0.8

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.
@@ -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
+ }