@nocobase/plugin-ai 2.2.0-alpha.6 → 2.2.0-alpha.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.
- package/dist/ai/docs/nocobase/file-manager/storage/local.md +3 -1
- package/dist/ai/docs/nocobase/multi-app/multi-app-vs-multi-portal-vs-multi-space.md +159 -0
- package/dist/ai/docs/nocobase/multi-app/multi-portal/index.md +195 -0
- package/dist/ai/docs/nocobase/security/guide.md +23 -0
- package/dist/ai/tools/formFiller.js +4 -3
- package/dist/ai/tools/subAgentWebSearch.js +20 -15
- package/dist/client/372.40eb52905e3f3049.js +10 -0
- package/dist/client/index.js +3 -3
- package/dist/client-v2/372.8cc3fde09c9bec77.js +10 -0
- package/dist/client-v2/ai-employees/tools/data-modeling/useFieldInterfaceOptions.d.ts +1 -0
- package/dist/client-v2/index.js +2 -2
- package/dist/client-v2/llm-providers/forms.d.ts +2 -0
- package/dist/client-v2/llm-providers/index.d.ts +1 -0
- package/dist/client-v2/pages/EmployeesPage.d.ts +9 -0
- package/dist/externalVersion.js +15 -15
- package/dist/locale/en-US.json +6 -0
- package/dist/locale/zh-CN.json +6 -0
- package/dist/node_modules/@langchain/mistralai/package.json +1 -1
- package/dist/node_modules/@langchain/xai/package.json +1 -1
- package/dist/node_modules/fs-extra/package.json +1 -1
- package/dist/node_modules/jsonrepair/package.json +1 -1
- package/dist/node_modules/just-bash/package.json +1 -1
- package/dist/node_modules/nodejs-snowflake/package.json +1 -1
- package/dist/node_modules/openai/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/ai-employees/ai-employee.js +2 -1
- package/dist/server/ai-employees/prompts.d.ts +2 -1
- package/dist/server/ai-employees/prompts.js +8 -2
- package/dist/server/ai-employees/tool-call-sanitizer.d.ts +1 -0
- package/dist/server/ai-employees/tool-call-sanitizer.js +2 -1
- package/dist/server/ai-employees/utils.js +11 -5
- package/dist/server/collections/ai-usage-events.d.ts +10 -0
- package/dist/server/collections/ai-usage-events.js +183 -0
- package/dist/server/llm-providers/dashscope.d.ts +2 -1
- package/dist/server/llm-providers/dashscope.js +16 -1
- package/dist/server/llm-providers/deepseek.d.ts +2 -1
- package/dist/server/llm-providers/deepseek.js +33 -2
- package/dist/server/llm-providers/kimi/provider.d.ts +2 -1
- package/dist/server/llm-providers/kimi/provider.js +22 -2
- package/dist/server/llm-providers/mistral.d.ts +36 -2
- package/dist/server/llm-providers/mistral.js +62 -2
- package/dist/server/llm-providers/openai/completions.d.ts +2 -1
- package/dist/server/llm-providers/openai/completions.js +17 -1
- package/dist/server/llm-providers/openai/responses.d.ts +2 -1
- package/dist/server/llm-providers/openai/responses.js +17 -1
- package/dist/server/llm-providers/orcarouter.d.ts +35 -0
- package/dist/server/llm-providers/orcarouter.js +155 -0
- package/dist/server/llm-providers/provider.d.ts +7 -0
- package/dist/server/llm-providers/provider.js +7 -1
- package/dist/server/manager/ai-chat-conversation.js +2 -0
- package/dist/server/manager/ai-context-datasource-manager.js +34 -7
- package/dist/server/manager/ai-manager.d.ts +2 -1
- package/dist/server/manager/ai-manager.js +4 -1
- package/dist/server/manager/ai-usage-events.d.ts +49 -0
- package/dist/server/manager/ai-usage-events.js +195 -0
- package/dist/server/plugin.js +2 -0
- package/package.json +2 -2
- package/dist/client/372.da38fe350bf841f4.js +0 -10
- package/dist/client-v2/372.d76ea1ceed2be2a4.js +0 -10
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { LLMProvider } from './provider';
|
|
10
|
+
import { LLMProviderMeta } from '../manager/ai-manager';
|
|
11
|
+
import { AIMessageChunk, BaseMessageChunk } from '@langchain/core/messages';
|
|
12
|
+
import { ReasoningChatOpenAI } from './common/reasoning';
|
|
13
|
+
import { AttachmentModel } from '@nocobase/plugin-file-manager';
|
|
14
|
+
import { Model } from '@nocobase/database';
|
|
15
|
+
export declare class OrcaRouterProvider extends LLMProvider {
|
|
16
|
+
chatModel: ReasoningChatOpenAI;
|
|
17
|
+
get baseURL(): string;
|
|
18
|
+
createModel(): ChatOrcaRouterCompletions;
|
|
19
|
+
protected isApiSupportedAttachment(attachment: AttachmentModel): boolean;
|
|
20
|
+
parseResponseMessage(message: Model): {
|
|
21
|
+
key: any;
|
|
22
|
+
createdAt: any;
|
|
23
|
+
content: any;
|
|
24
|
+
role: any;
|
|
25
|
+
};
|
|
26
|
+
parseReasoningContent(chunk: AIMessageChunk): {
|
|
27
|
+
status: string;
|
|
28
|
+
content: string;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
declare class ChatOrcaRouterCompletions extends ReasoningChatOpenAI {
|
|
32
|
+
_convertCompletionsDeltaToBaseMessageChunk(delta: any, rawResponse: any, defaultRole: any): BaseMessageChunk;
|
|
33
|
+
}
|
|
34
|
+
export declare const orcarouterProviderOptions: LLMProviderMeta;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
var orcarouter_exports = {};
|
|
38
|
+
__export(orcarouter_exports, {
|
|
39
|
+
OrcaRouterProvider: () => OrcaRouterProvider,
|
|
40
|
+
orcarouterProviderOptions: () => orcarouterProviderOptions
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(orcarouter_exports);
|
|
43
|
+
var import_provider = require("./provider");
|
|
44
|
+
var import_ai_manager = require("../manager/ai-manager");
|
|
45
|
+
var import_lodash = __toESM(require("lodash"));
|
|
46
|
+
var import_openai = require("@langchain/openai");
|
|
47
|
+
var import_messages = require("@langchain/core/messages");
|
|
48
|
+
var import_reasoning = require("./common/reasoning");
|
|
49
|
+
class OrcaRouterProvider extends import_provider.LLMProvider {
|
|
50
|
+
get baseURL() {
|
|
51
|
+
return "https://api.orcarouter.ai/v1";
|
|
52
|
+
}
|
|
53
|
+
createModel() {
|
|
54
|
+
const { apiKey, httpReferer, xTitle } = this.serviceOptions || {};
|
|
55
|
+
const { responseFormat, structuredOutput } = this.modelOptions || {};
|
|
56
|
+
const { name, schema } = structuredOutput || {};
|
|
57
|
+
const responseFormatOptions = {
|
|
58
|
+
type: responseFormat ?? "text"
|
|
59
|
+
};
|
|
60
|
+
if (responseFormat === "json_schema" && schema) {
|
|
61
|
+
responseFormatOptions["json_schema"] = { schema, name: name ?? "schema" };
|
|
62
|
+
}
|
|
63
|
+
const defaultHeaders = {};
|
|
64
|
+
if (httpReferer) {
|
|
65
|
+
defaultHeaders["HTTP-Referer"] = httpReferer;
|
|
66
|
+
}
|
|
67
|
+
if (xTitle) {
|
|
68
|
+
defaultHeaders["X-Title"] = xTitle;
|
|
69
|
+
}
|
|
70
|
+
return new ChatOrcaRouterCompletions({
|
|
71
|
+
apiKey,
|
|
72
|
+
...this.modelOptions,
|
|
73
|
+
modelKwargs: {
|
|
74
|
+
response_format: responseFormatOptions
|
|
75
|
+
},
|
|
76
|
+
configuration: {
|
|
77
|
+
baseURL: this.getResolvedBaseURL(),
|
|
78
|
+
...Object.keys(defaultHeaders).length ? { defaultHeaders } : {}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
isApiSupportedAttachment(attachment) {
|
|
83
|
+
const media = ["image/"];
|
|
84
|
+
return media.some((it) => {
|
|
85
|
+
var _a;
|
|
86
|
+
return (_a = attachment == null ? void 0 : attachment.mimetype) == null ? void 0 : _a.startsWith(it);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
parseResponseMessage(message) {
|
|
90
|
+
var _a;
|
|
91
|
+
const result = super.parseResponseMessage(message);
|
|
92
|
+
if (["user", "tool"].includes(result == null ? void 0 : result.role)) {
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
const { metadata } = (message == null ? void 0 : message.toJSON()) ?? {};
|
|
96
|
+
if (!import_lodash.default.isEmpty((_a = metadata == null ? void 0 : metadata.additional_kwargs) == null ? void 0 : _a.reasoning_content)) {
|
|
97
|
+
result.content = {
|
|
98
|
+
...result.content ?? {},
|
|
99
|
+
reasoning: {
|
|
100
|
+
status: "stop",
|
|
101
|
+
content: metadata == null ? void 0 : metadata.additional_kwargs.reasoning_content
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
parseReasoningContent(chunk) {
|
|
108
|
+
var _a;
|
|
109
|
+
if (!import_lodash.default.isEmpty((_a = chunk == null ? void 0 : chunk.additional_kwargs) == null ? void 0 : _a.reasoning_content)) {
|
|
110
|
+
return {
|
|
111
|
+
status: "streaming",
|
|
112
|
+
content: chunk.additional_kwargs.reasoning_content
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
class ChatOrcaRouterCompletions extends import_reasoning.ReasoningChatOpenAI {
|
|
119
|
+
_convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
|
|
120
|
+
const chunk = (0, import_openai.convertCompletionsDeltaToBaseMessageChunk)({
|
|
121
|
+
delta,
|
|
122
|
+
rawResponse,
|
|
123
|
+
includeRawResponse: this.__includeRawResponse,
|
|
124
|
+
defaultRole
|
|
125
|
+
});
|
|
126
|
+
if (chunk instanceof import_messages.AIMessageChunk) {
|
|
127
|
+
if (delta.reasoning_content) {
|
|
128
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return chunk;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const orcarouterProviderOptions = {
|
|
135
|
+
title: "OrcaRouter",
|
|
136
|
+
supportedModel: [import_ai_manager.SupportedModel.LLM],
|
|
137
|
+
models: {
|
|
138
|
+
[import_ai_manager.SupportedModel.LLM]: [
|
|
139
|
+
"orcarouter/auto",
|
|
140
|
+
"openai/gpt-5.5",
|
|
141
|
+
"google/gemini-3.5-flash",
|
|
142
|
+
"anthropic/claude-opus-4.8",
|
|
143
|
+
"grok/grok-4.3",
|
|
144
|
+
"deepseek/deepseek-v4-pro",
|
|
145
|
+
"minimax/minimax-m2.7",
|
|
146
|
+
"qwen/qwen3.7-max"
|
|
147
|
+
]
|
|
148
|
+
},
|
|
149
|
+
provider: OrcaRouterProvider
|
|
150
|
+
};
|
|
151
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
152
|
+
0 && (module.exports = {
|
|
153
|
+
OrcaRouterProvider,
|
|
154
|
+
orcarouterProviderOptions
|
|
155
|
+
});
|
|
@@ -27,6 +27,11 @@ export type LLMProviderInvokeOptions = {
|
|
|
27
27
|
modelRequestParams?: Record<string, any>;
|
|
28
28
|
[key: string]: any;
|
|
29
29
|
};
|
|
30
|
+
export type ReasoningMode = 'default' | 'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
|
|
31
|
+
export type ReasoningOptions = {
|
|
32
|
+
mode: ReasoningMode;
|
|
33
|
+
};
|
|
34
|
+
export type ResolvedReasoningOptions = Pick<LLMProviderInvokeOptions, 'modelKwargs' | 'modelRequestParams'>;
|
|
30
35
|
export type LLMModelRequestBuilderResult = {
|
|
31
36
|
context: AIChatContext;
|
|
32
37
|
options?: LLMProviderInvokeOptions;
|
|
@@ -45,10 +50,12 @@ export declare abstract class LLMProvider {
|
|
|
45
50
|
serviceOptions: Record<string, any>;
|
|
46
51
|
modelOptions: Record<string, any> | undefined;
|
|
47
52
|
chatModel: any;
|
|
53
|
+
protected modelReasoningOptions: ReasoningOptions | undefined;
|
|
48
54
|
abstract createModel(): BaseChatModel | any;
|
|
49
55
|
get baseURL(): string | null;
|
|
50
56
|
constructor(opts: LLMProviderOptions);
|
|
51
57
|
protected getModelRequestBuilder(_model?: string): LLMModelRequestBuilder | null;
|
|
58
|
+
protected resolveReasoningOptions(_reasoning?: ReasoningOptions): ResolvedReasoningOptions;
|
|
52
59
|
prepareChain(context: AIChatContext): any;
|
|
53
60
|
invoke(context: AIChatContext, options?: LLMProviderInvokeOptions): Promise<any>;
|
|
54
61
|
stream(context: AIChatContext, options?: any): Promise<any>;
|
|
@@ -84,6 +84,7 @@ class LLMProvider {
|
|
|
84
84
|
serviceOptions;
|
|
85
85
|
modelOptions;
|
|
86
86
|
chatModel;
|
|
87
|
+
modelReasoningOptions;
|
|
87
88
|
get baseURL() {
|
|
88
89
|
return null;
|
|
89
90
|
}
|
|
@@ -92,13 +93,18 @@ class LLMProvider {
|
|
|
92
93
|
this.app = app;
|
|
93
94
|
this.serviceOptions = resolveServiceOptions(serviceOptions, app);
|
|
94
95
|
if (modelOptions) {
|
|
95
|
-
|
|
96
|
+
const { _reasoning, ...restModelOptions } = modelOptions;
|
|
97
|
+
this.modelReasoningOptions = _reasoning;
|
|
98
|
+
this.modelOptions = restModelOptions;
|
|
96
99
|
this.chatModel = this.createModel();
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
getModelRequestBuilder(_model) {
|
|
100
103
|
return null;
|
|
101
104
|
}
|
|
105
|
+
resolveReasoningOptions(_reasoning) {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
102
108
|
prepareChain(context) {
|
|
103
109
|
var _a, _b, _c, _d;
|
|
104
110
|
let chain = this.chatModel;
|
|
@@ -40,6 +40,7 @@ __export(ai_chat_conversation_exports, {
|
|
|
40
40
|
});
|
|
41
41
|
module.exports = __toCommonJS(ai_chat_conversation_exports);
|
|
42
42
|
var import_lodash = __toESM(require("lodash"));
|
|
43
|
+
var import_ai_usage_events = require("./ai-usage-events");
|
|
43
44
|
const createAIChatConversation = (ctx, sessionId) => {
|
|
44
45
|
return new AIChatConversationImpl(ctx, sessionId);
|
|
45
46
|
};
|
|
@@ -81,6 +82,7 @@ class AIChatConversationImpl {
|
|
|
81
82
|
),
|
|
82
83
|
transaction: this.transaction
|
|
83
84
|
});
|
|
85
|
+
await (0, import_ai_usage_events.recordAIUsageEventsForMessages)(this.ctx, this.sessionId, instances, this.transaction);
|
|
84
86
|
return isArray ? instances : instances[0];
|
|
85
87
|
}
|
|
86
88
|
async removeMessages({ messageId }) {
|
|
@@ -60,6 +60,34 @@ function serializeQueryFieldValue(value) {
|
|
|
60
60
|
}
|
|
61
61
|
return value;
|
|
62
62
|
}
|
|
63
|
+
function getRecordFieldValue(record, field) {
|
|
64
|
+
return field.split(".").reduce((current, key) => {
|
|
65
|
+
if (current === null || current === void 0) {
|
|
66
|
+
return current;
|
|
67
|
+
}
|
|
68
|
+
if (typeof current.get === "function") {
|
|
69
|
+
return current.get(key);
|
|
70
|
+
}
|
|
71
|
+
return current[key];
|
|
72
|
+
}, record);
|
|
73
|
+
}
|
|
74
|
+
function getQueryFieldOptions(collection, field) {
|
|
75
|
+
const parts = field.split(".");
|
|
76
|
+
let currentCollection = collection;
|
|
77
|
+
for (const [index, part] of parts.entries()) {
|
|
78
|
+
const currentField = currentCollection == null ? void 0 : currentCollection.getField(part);
|
|
79
|
+
if (!currentField) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (index === parts.length - 1) {
|
|
83
|
+
return currentField.options;
|
|
84
|
+
}
|
|
85
|
+
if (!currentField.isRelationField()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
currentCollection = currentField.targetCollection();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
63
91
|
class AIContextDatasourceManager {
|
|
64
92
|
constructor(plugin) {
|
|
65
93
|
this.plugin = plugin;
|
|
@@ -149,17 +177,16 @@ class AIContextDatasourceManager {
|
|
|
149
177
|
}
|
|
150
178
|
});
|
|
151
179
|
}
|
|
152
|
-
const { fields, filter, sort, offset, limit } = options;
|
|
153
|
-
const result = await collection.repository.find({ fields, filter, sort, offset: offset ?? 0, limit });
|
|
180
|
+
const { fields, appends, filter, sort, offset, limit } = options;
|
|
181
|
+
const result = await collection.repository.find({ fields, appends, filter, sort, offset: offset ?? 0, limit });
|
|
154
182
|
const total = await collection.repository.count({ fields, filter });
|
|
155
183
|
const records = result.map(
|
|
156
184
|
(x) => fields.map((field) => {
|
|
157
|
-
|
|
158
|
-
const
|
|
159
|
-
const value = serializeQueryFieldValue(x[field]);
|
|
185
|
+
const { name, type } = getQueryFieldOptions(collection, field) || {};
|
|
186
|
+
const value = serializeQueryFieldValue(getRecordFieldValue(x, field));
|
|
160
187
|
return {
|
|
161
|
-
name,
|
|
162
|
-
type,
|
|
188
|
+
name: field.includes(".") ? field : name || field,
|
|
189
|
+
type: type || "unknown",
|
|
163
190
|
value
|
|
164
191
|
};
|
|
165
192
|
})
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
|
-
import { LLMProvider, LLMProviderOptions, EmbeddingProvider, EmbeddingProviderOptions } from '../llm-providers/provider';
|
|
9
|
+
import { LLMProvider, LLMProviderOptions, EmbeddingProvider, EmbeddingProviderOptions, ReasoningOptions } from '../llm-providers/provider';
|
|
10
10
|
import PluginAIServer from '../plugin';
|
|
11
11
|
import { ToolManager } from './tool-manager';
|
|
12
12
|
export type LLMProviderMeta = {
|
|
@@ -25,6 +25,7 @@ export type LLMModelOptions = {
|
|
|
25
25
|
llmService: string;
|
|
26
26
|
model: string;
|
|
27
27
|
webSearch?: boolean;
|
|
28
|
+
reasoning?: ReasoningOptions;
|
|
28
29
|
};
|
|
29
30
|
export type EnabledLLMModel = {
|
|
30
31
|
label: string;
|
|
@@ -123,7 +123,7 @@ class AIManager {
|
|
|
123
123
|
return (0, import_recommended_models.getRecommendedModels)(provider);
|
|
124
124
|
}
|
|
125
125
|
async getLLMService(options) {
|
|
126
|
-
const { llmService, model, webSearch } = options ?? {};
|
|
126
|
+
const { llmService, model, webSearch, reasoning } = options ?? {};
|
|
127
127
|
if (!llmService || !model) {
|
|
128
128
|
throw new Error("LLM service not configured");
|
|
129
129
|
}
|
|
@@ -134,6 +134,9 @@ class AIManager {
|
|
|
134
134
|
if (webSearch === true) {
|
|
135
135
|
modelOptions.builtIn = { webSearch: true };
|
|
136
136
|
}
|
|
137
|
+
if (reasoning) {
|
|
138
|
+
modelOptions._reasoning = reasoning;
|
|
139
|
+
}
|
|
137
140
|
const service = await this.plugin.db.getRepository("llmServices").findOne({
|
|
138
141
|
filter: {
|
|
139
142
|
name: llmService
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Context } from '@nocobase/actions';
|
|
10
|
+
import { Transaction } from '@nocobase/database';
|
|
11
|
+
import { AIMessage } from '../types';
|
|
12
|
+
type RecordLike = Record<string, unknown> & {
|
|
13
|
+
get?: (key: string) => unknown;
|
|
14
|
+
};
|
|
15
|
+
export type NormalizedUsageMetadata = {
|
|
16
|
+
inputTokens: number;
|
|
17
|
+
outputTokens: number;
|
|
18
|
+
totalTokens: number;
|
|
19
|
+
cachedTokens: number;
|
|
20
|
+
reasoningTokens: number;
|
|
21
|
+
};
|
|
22
|
+
export type AIUsageEventValues = {
|
|
23
|
+
occurredAt: Date;
|
|
24
|
+
sessionId: string;
|
|
25
|
+
messageId: string;
|
|
26
|
+
userId?: unknown;
|
|
27
|
+
aiEmployeeUsername?: unknown;
|
|
28
|
+
from: string;
|
|
29
|
+
category: string;
|
|
30
|
+
eventType: 'llm_message';
|
|
31
|
+
role: string;
|
|
32
|
+
provider: string;
|
|
33
|
+
llmService?: string;
|
|
34
|
+
model: string;
|
|
35
|
+
inputTokens: number;
|
|
36
|
+
outputTokens: number;
|
|
37
|
+
totalTokens: number;
|
|
38
|
+
cachedTokens: number;
|
|
39
|
+
reasoningTokens: number;
|
|
40
|
+
toolCallCount: number;
|
|
41
|
+
autoToolCallCount: number;
|
|
42
|
+
status: 'success';
|
|
43
|
+
rawUsageMetadata: Record<string, unknown>;
|
|
44
|
+
rawResponseMetadata?: Record<string, unknown>;
|
|
45
|
+
};
|
|
46
|
+
export declare function normalizeUsageMetadata(usageMetadata: Record<string, unknown>): NormalizedUsageMetadata;
|
|
47
|
+
export declare function buildAIUsageEventValues(sessionId: string, message: AIMessage, conversation: RecordLike): AIUsageEventValues | null;
|
|
48
|
+
export declare function recordAIUsageEventsForMessages(ctx: Context, sessionId: string, messages: AIMessage[], transaction?: Transaction): Promise<void>;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var ai_usage_events_exports = {};
|
|
28
|
+
__export(ai_usage_events_exports, {
|
|
29
|
+
buildAIUsageEventValues: () => buildAIUsageEventValues,
|
|
30
|
+
normalizeUsageMetadata: () => normalizeUsageMetadata,
|
|
31
|
+
recordAIUsageEventsForMessages: () => recordAIUsageEventsForMessages
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(ai_usage_events_exports);
|
|
34
|
+
const SKIPPED_MESSAGE_ROLES = /* @__PURE__ */ new Set(["user", "tool", "system"]);
|
|
35
|
+
function normalizeUsageMetadata(usageMetadata) {
|
|
36
|
+
const inputTokens = getNumericValue(usageMetadata, ["input_tokens", "prompt_tokens", "inputTokens", "promptTokens"]) ?? 0;
|
|
37
|
+
const outputTokens = getNumericValue(usageMetadata, ["output_tokens", "completion_tokens", "outputTokens", "completionTokens"]) ?? 0;
|
|
38
|
+
const totalTokens = getNumericValue(usageMetadata, ["total_tokens", "totalTokens"]) ?? inputTokens + outputTokens;
|
|
39
|
+
const cachedTokens = getNumericValue(usageMetadata, ["cached_tokens", "cachedTokens"]) ?? getNestedNumericValue(usageMetadata, ["input_token_details", "cache_read"]) ?? getNestedNumericValue(usageMetadata, ["input_token_details", "cached_tokens"]) ?? getNestedNumericValue(usageMetadata, ["prompt_tokens_details", "cached_tokens"]) ?? 0;
|
|
40
|
+
const reasoningTokens = getNumericValue(usageMetadata, ["reasoning_tokens", "reasoningTokens"]) ?? getNestedNumericValue(usageMetadata, ["output_token_details", "reasoning"]) ?? getNestedNumericValue(usageMetadata, ["completion_tokens_details", "reasoning_tokens"]) ?? 0;
|
|
41
|
+
return {
|
|
42
|
+
inputTokens,
|
|
43
|
+
outputTokens,
|
|
44
|
+
totalTokens,
|
|
45
|
+
cachedTokens,
|
|
46
|
+
reasoningTokens
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function buildAIUsageEventValues(sessionId, message, conversation) {
|
|
50
|
+
const role = readString(message, "role");
|
|
51
|
+
if (!role || SKIPPED_MESSAGE_ROLES.has(role)) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const metadata = readRecord(message, "metadata");
|
|
55
|
+
const provider = readString(metadata, "provider");
|
|
56
|
+
const model = readString(metadata, "model");
|
|
57
|
+
if (!provider || !model) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const usageMetadata = readRecord(metadata, "usage_metadata") ?? {};
|
|
61
|
+
const responseMetadata = readRecord(metadata, "response_metadata");
|
|
62
|
+
const normalizedUsage = normalizeUsageMetadata(usageMetadata);
|
|
63
|
+
const toolCalls = readArray(message, "toolCalls");
|
|
64
|
+
const autoCallTools = readArray(metadata, "autoCallTools");
|
|
65
|
+
const llmService = readString(metadata, "llmService");
|
|
66
|
+
return {
|
|
67
|
+
occurredAt: normalizeDate(readValue(message, "createdAt")),
|
|
68
|
+
sessionId,
|
|
69
|
+
messageId: String(readValue(message, "messageId") ?? ""),
|
|
70
|
+
userId: readValue(conversation, "userId"),
|
|
71
|
+
aiEmployeeUsername: readValue(conversation, "aiEmployeeUsername"),
|
|
72
|
+
from: readString(conversation, "from") ?? "main-agent",
|
|
73
|
+
category: readString(conversation, "category") ?? "chat",
|
|
74
|
+
eventType: "llm_message",
|
|
75
|
+
role,
|
|
76
|
+
provider,
|
|
77
|
+
...llmService ? { llmService } : {},
|
|
78
|
+
model,
|
|
79
|
+
...normalizedUsage,
|
|
80
|
+
toolCallCount: (toolCalls == null ? void 0 : toolCalls.length) ?? 0,
|
|
81
|
+
autoToolCallCount: (autoCallTools == null ? void 0 : autoCallTools.length) ?? 0,
|
|
82
|
+
status: "success",
|
|
83
|
+
rawUsageMetadata: usageMetadata,
|
|
84
|
+
...responseMetadata ? { rawResponseMetadata: responseMetadata } : {}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function recordAIUsageEventsForMessages(ctx, sessionId, messages, transaction) {
|
|
88
|
+
const recordableMessages = messages.filter(isRecordableLLMMessage);
|
|
89
|
+
if (recordableMessages.length === 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const conversation = await ctx.db.getRepository("aiConversations").findOne({
|
|
93
|
+
filter: {
|
|
94
|
+
sessionId
|
|
95
|
+
},
|
|
96
|
+
transaction
|
|
97
|
+
});
|
|
98
|
+
if (!conversation) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const usageEvents = recordableMessages.map((message) => buildAIUsageEventValues(sessionId, message, conversation)).filter(isUsageEventValues);
|
|
102
|
+
if (usageEvents.length === 0) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const usageEventsRepo = ctx.db.getRepository("aiUsageEvents");
|
|
106
|
+
for (const values of usageEvents) {
|
|
107
|
+
await usageEventsRepo.updateOrCreate({
|
|
108
|
+
filterKeys: ["messageId", "eventType"],
|
|
109
|
+
values,
|
|
110
|
+
transaction
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function isUsageEventValues(value) {
|
|
115
|
+
return value !== null && value.messageId !== "";
|
|
116
|
+
}
|
|
117
|
+
function isRecordableLLMMessage(message) {
|
|
118
|
+
const role = readString(message, "role");
|
|
119
|
+
if (!role || SKIPPED_MESSAGE_ROLES.has(role)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const metadata = readRecord(message, "metadata");
|
|
123
|
+
return Boolean(readString(metadata, "provider") && readString(metadata, "model"));
|
|
124
|
+
}
|
|
125
|
+
function isRecord(value) {
|
|
126
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
127
|
+
}
|
|
128
|
+
function readValue(source, key) {
|
|
129
|
+
if (!isRecord(source)) {
|
|
130
|
+
return void 0;
|
|
131
|
+
}
|
|
132
|
+
const value = source[key];
|
|
133
|
+
if (value !== void 0) {
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
if (typeof source.get === "function") {
|
|
137
|
+
return source.get(key);
|
|
138
|
+
}
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
function readString(source, key) {
|
|
142
|
+
const value = readValue(source, key);
|
|
143
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
144
|
+
}
|
|
145
|
+
function readRecord(source, key) {
|
|
146
|
+
const value = readValue(source, key);
|
|
147
|
+
return isRecord(value) ? value : void 0;
|
|
148
|
+
}
|
|
149
|
+
function readArray(source, key) {
|
|
150
|
+
const value = readValue(source, key);
|
|
151
|
+
return Array.isArray(value) ? value : void 0;
|
|
152
|
+
}
|
|
153
|
+
function getNumericValue(source, keys) {
|
|
154
|
+
for (const key of keys) {
|
|
155
|
+
const normalized = normalizeNumber(source[key]);
|
|
156
|
+
if (normalized !== void 0) {
|
|
157
|
+
return normalized;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function getNestedNumericValue(source, path) {
|
|
162
|
+
let current = source;
|
|
163
|
+
for (const key of path) {
|
|
164
|
+
if (!isRecord(current)) {
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
current = current[key];
|
|
168
|
+
}
|
|
169
|
+
return normalizeNumber(current);
|
|
170
|
+
}
|
|
171
|
+
function normalizeNumber(value) {
|
|
172
|
+
const numeric = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
|
|
173
|
+
if (!Number.isFinite(numeric)) {
|
|
174
|
+
return void 0;
|
|
175
|
+
}
|
|
176
|
+
return Math.max(0, Math.trunc(numeric));
|
|
177
|
+
}
|
|
178
|
+
function normalizeDate(value) {
|
|
179
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
183
|
+
const date = new Date(value);
|
|
184
|
+
if (Number.isFinite(date.getTime())) {
|
|
185
|
+
return date;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return /* @__PURE__ */ new Date();
|
|
189
|
+
}
|
|
190
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
191
|
+
0 && (module.exports = {
|
|
192
|
+
buildAIUsageEventValues,
|
|
193
|
+
normalizeUsageMetadata,
|
|
194
|
+
recordAIUsageEventsForMessages
|
|
195
|
+
});
|
package/dist/server/plugin.js
CHANGED
|
@@ -75,6 +75,7 @@ var import_document_loader = require("./document-loader");
|
|
|
75
75
|
var import_checkpoints = require("./ai-employees/checkpoints");
|
|
76
76
|
var import_mimo = require("./llm-providers/mimo");
|
|
77
77
|
var import_mistral = require("./llm-providers/mistral");
|
|
78
|
+
var import_orcarouter = require("./llm-providers/orcarouter");
|
|
78
79
|
var import_sub_agents = require("./ai-employees/sub-agents");
|
|
79
80
|
var import_employee = require("./workflow/nodes/employee");
|
|
80
81
|
var import_ai_knowledge_base = require("./ai-employees/ai-knowledge-base");
|
|
@@ -186,6 +187,7 @@ class PluginAIServer extends import_server.Plugin {
|
|
|
186
187
|
this.aiManager.registerLLMProvider("openai-completions", import_openai2.openaiCompletionsProviderOptions);
|
|
187
188
|
this.aiManager.registerLLMProvider("kimi", import_kimi.kimiProviderOptions);
|
|
188
189
|
this.aiManager.registerLLMProvider("xai", import_xai.xaiProviderOptions);
|
|
190
|
+
this.aiManager.registerLLMProvider("orcarouter", import_orcarouter.orcarouterProviderOptions);
|
|
189
191
|
}
|
|
190
192
|
registerTools() {
|
|
191
193
|
const toolsManager = this.ai.toolsManager;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"description": "Create AI employees with diverse skills to collaborate with humans, build systems, and handle business operations.",
|
|
7
7
|
"description.ru-RU": "Поддержка интеграции с AI-сервисами: предоставляются AI-узлы для рабочих процессов, расширяя возможности бизнес-обработки.",
|
|
8
8
|
"description.zh-CN": "创建各种技能的 AI 员工,与人类协同,搭建系统,处理业务。",
|
|
9
|
-
"version": "2.2.0-alpha.
|
|
9
|
+
"version": "2.2.0-alpha.8",
|
|
10
10
|
"main": "dist/server/index.js",
|
|
11
11
|
"homepage": "https://docs.nocobase.com/handbook/action-ai",
|
|
12
12
|
"homepage.ru-RU": "https://docs-ru.nocobase.com/handbook/action-ai",
|
|
@@ -66,5 +66,5 @@
|
|
|
66
66
|
"keywords": [
|
|
67
67
|
"AI"
|
|
68
68
|
],
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "810c81e5963966bda7ec03b6453a3a80fe60e027"
|
|
70
70
|
}
|