@_tc/template-core 0.4.0 → 0.4.1
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/.skills/tc-component-usage-skills/reference/component-api.md +27 -0
- package/.skills/tc-generator/SKILL.md +1 -1
- package/.skills/tc-generator/reference/project-template/ssr/apps/hello/hello.entry.tsx +6 -1
- package/.skills/tc-generator/reference/runtime-extensions.md +10 -0
- package/AGENT_README.md +8 -2
- package/CHANGE.md +27 -0
- package/README.md +157 -8
- package/cjs/app/ai/agent/agent-scanner.service.js +1 -1
- package/cjs/app/ai/agent/agent.module.js +1 -1
- package/cjs/app/ai/ai-config.module.js +1 -1
- package/cjs/app/ai/ai-config.service.js +1 -1
- package/cjs/app/ai/ai.controller.js +1 -1
- package/cjs/app/ai/ai.module.js +1 -1
- package/cjs/app/ai/guardrail/guardrail.module.js +1 -1
- package/cjs/app/ai/guardrail/guardrail.service.js +1 -1
- package/cjs/app/ai/index.js +1 -0
- package/cjs/app/ai/mcp/internal/read-sse-response.js +2 -0
- package/cjs/app/ai/mcp/internal/stdio-process.js +1 -0
- package/cjs/app/ai/mcp/mcp-registry.service.js +2 -2
- package/cjs/app/ai/mcp/mcp.client.js +2 -3
- package/cjs/app/ai/mcp/mcp.loader.js +1 -1
- package/cjs/app/ai/mcp/mcp.module.js +1 -1
- package/cjs/app/ai/memory/checkpoint.service.js +1 -1
- package/cjs/app/ai/memory/memory.module.js +1 -1
- package/cjs/app/ai/memory/thread.service.js +1 -1
- package/cjs/app/ai/models/chat-sse.js +4 -0
- package/cjs/app/ai/models/chat.js +1 -0
- package/cjs/app/ai/models/http.js +1 -0
- package/cjs/app/ai/models/image.js +1 -0
- package/cjs/app/ai/models/video.js +1 -0
- package/cjs/app/ai/nest-service-provider.js +1 -0
- package/cjs/app/ai/rag/rag.module.js +1 -1
- package/cjs/app/ai/rag/retriever.service.js +1 -1
- package/cjs/app/ai/retention/conversation-retention.service.js +1 -1
- package/cjs/app/ai/runtime/ai-runtime-startup.validator.js +1 -1
- package/cjs/app/ai/runtime/ai-runtime.js +1 -0
- package/cjs/app/ai/runtime/internal/context-compaction.service.js +3 -3
- package/cjs/app/ai/runtime/internal/declaration-snapshot.js +1 -0
- package/cjs/app/ai/runtime/internal/owned-runtime-resources.js +1 -0
- package/cjs/app/ai/runtime/langchain/langchain-planner.service.js +1 -1
- package/cjs/app/ai/runtime/langgraph/langgraph-orchestrator.service.js +1 -1
- package/cjs/app/ai/runtime/runtime.module.js +1 -1
- package/cjs/app/ai/skill/skill-scanner.service.js +1 -1
- package/cjs/app/ai/skill/skill.module.js +1 -1
- package/cjs/app/ai/snapshot-data.js +1 -0
- package/cjs/app/ai/store/internal/create-vector-store.js +1 -0
- package/cjs/app/ai/store/store.module.js +1 -1
- package/cjs/app/ai/tool/builtin/index.js +1 -1
- package/cjs/app/ai/tool/builtin/invoke-agent.tool.js +1 -1
- package/cjs/app/ai/tool/builtin/retrieve.tool.js +1 -1
- package/cjs/app/ai/tool/executor.service.js +1 -1
- package/cjs/app/ai/tool/tool-registry.service.js +1 -1
- package/cjs/app/ai/tool/tool.module.js +1 -1
- package/cjs/app/ai/trace/trace.service.js +1 -1
- package/cjs/app/csr/controller.js +1 -1
- package/cjs/app/csr/types.js +0 -0
- package/cjs/app/html/browser-options.js +1 -1
- package/cjs/app/html/shell.js +31 -16
- package/cjs/app/http/api-signing.js +1 -0
- package/cjs/app/http/body-parser.middleware.js +1 -1
- package/cjs/app/http/csp-nonce.js +1 -0
- package/cjs/app/http/error-response.js +1 -1
- package/cjs/app/http/exception-filter.js +1 -1
- package/cjs/app/http/request-body-parser-policy.js +1 -0
- package/cjs/app/http/security.guard.js +1 -1
- package/cjs/app/project/controller.js +1 -1
- package/cjs/app/project/service.js +1 -1
- package/cjs/app/providers/foundation.js +1 -1
- package/cjs/app/ssr/artifacts.js +1 -1
- package/cjs/app/ssr/controller.js +1 -1
- package/cjs/app/ssr/html.js +17 -17
- package/cjs/app/ssr/stream.js +1 -1
- package/cjs/app/static-assets.middleware.js +1 -1
- package/cjs/app/template-core.module.js +1 -1
- package/cjs/app/view/entry.tpl +2 -2
- package/cjs/bundler/buildSSR.js +1 -1
- package/cjs/bundler/entryCollect.js +1 -1
- package/cjs/bundler/feSharedConfig.js +1 -1
- package/cjs/bundler/utils.js +3 -3
- package/cjs/config/config.default.js +1 -1
- package/cjs/packages/common/index.js +1 -1
- package/cjs/packages/core/bootstrap.js +1 -1
- package/cjs/packages/core/startup.js +1 -1
- package/esm/app/ai/agent/agent-scanner.service.js +33 -36
- package/esm/app/ai/agent/agent.module.js +9 -7
- package/esm/app/ai/ai-config.module.js +14 -7
- package/esm/app/ai/ai-config.service.js +24 -20
- package/esm/app/ai/ai.controller.js +41 -41
- package/esm/app/ai/ai.module.js +15 -15
- package/esm/app/ai/guardrail/guardrail.module.js +11 -9
- package/esm/app/ai/guardrail/guardrail.service.js +5 -10
- package/esm/app/ai/index.js +2 -0
- package/esm/app/ai/mcp/internal/read-sse-response.js +35 -0
- package/esm/app/ai/mcp/internal/stdio-process.js +59 -0
- package/esm/app/ai/mcp/mcp-registry.service.js +10 -15
- package/esm/app/ai/mcp/mcp.client.js +77 -78
- package/esm/app/ai/mcp/mcp.loader.js +64 -42
- package/esm/app/ai/mcp/mcp.module.js +62 -22
- package/esm/app/ai/memory/checkpoint.service.js +18 -28
- package/esm/app/ai/memory/memory.module.js +15 -12
- package/esm/app/ai/memory/thread.service.js +21 -26
- package/esm/app/ai/models/chat-sse.js +44 -0
- package/esm/app/ai/models/chat.js +60 -0
- package/esm/app/ai/models/http.js +52 -0
- package/esm/app/ai/models/image.js +37 -0
- package/esm/app/ai/models/video.js +32 -0
- package/esm/app/ai/nest-service-provider.js +10 -0
- package/esm/app/ai/rag/rag.module.js +24 -15
- package/esm/app/ai/rag/retriever.service.js +37 -54
- package/esm/app/ai/retention/conversation-retention.service.js +16 -34
- package/esm/app/ai/runtime/ai-runtime-startup.validator.js +2 -19
- package/esm/app/ai/runtime/ai-runtime.js +104 -0
- package/esm/app/ai/runtime/internal/context-compaction.service.js +139 -145
- package/esm/app/ai/runtime/internal/declaration-snapshot.js +32 -0
- package/esm/app/ai/runtime/internal/owned-runtime-resources.js +21 -0
- package/esm/app/ai/runtime/langchain/langchain-planner.service.js +43 -49
- package/esm/app/ai/runtime/langgraph/langgraph-orchestrator.service.js +273 -225
- package/esm/app/ai/runtime/runtime.module.js +56 -35
- package/esm/app/ai/skill/skill-scanner.service.js +11 -16
- package/esm/app/ai/skill/skill.module.js +11 -9
- package/esm/app/ai/snapshot-data.js +24 -0
- package/esm/app/ai/store/internal/create-vector-store.js +10 -0
- package/esm/app/ai/store/store.module.js +9 -13
- package/esm/app/ai/tool/builtin/index.js +16 -4
- package/esm/app/ai/tool/builtin/invoke-agent.tool.js +30 -34
- package/esm/app/ai/tool/builtin/retrieve.tool.js +9 -13
- package/esm/app/ai/tool/executor.service.js +22 -29
- package/esm/app/ai/tool/tool-registry.service.js +6 -16
- package/esm/app/ai/tool/tool.module.js +26 -12
- package/esm/app/ai/trace/trace.service.js +15 -28
- package/esm/app/csr/controller.js +114 -63
- package/esm/app/csr/types.js +0 -0
- package/esm/app/html/browser-options.js +2 -2
- package/esm/app/html/shell.js +173 -32
- package/esm/app/http/api-signing.js +13 -0
- package/esm/app/http/body-parser.middleware.js +47 -27
- package/esm/app/http/csp-nonce.js +13 -0
- package/esm/app/http/error-response.js +8 -5
- package/esm/app/http/exception-filter.js +4 -4
- package/esm/app/http/request-body-parser-policy.js +46 -0
- package/esm/app/http/security.guard.js +23 -21
- package/esm/app/project/controller.js +15 -15
- package/esm/app/project/service.js +3 -3
- package/esm/app/providers/foundation.js +5 -5
- package/esm/app/ssr/artifacts.js +25 -21
- package/esm/app/ssr/controller.js +148 -75
- package/esm/app/ssr/html.js +49 -44
- package/esm/app/ssr/stream.js +25 -24
- package/esm/app/static-assets.middleware.js +4 -4
- package/esm/app/template-core.module.js +57 -52
- package/esm/app/view/entry.tpl +2 -2
- package/esm/bundler/buildSSR.js +1 -1
- package/esm/bundler/entryCollect.js +9 -9
- package/esm/bundler/feSharedConfig.js +42 -42
- package/esm/bundler/utils.js +78 -90
- package/esm/config/config.default.js +4 -3
- package/esm/packages/common/index.js +6 -6
- package/esm/packages/core/bootstrap.js +5 -5
- package/esm/packages/core/startup.js +69 -46
- package/fe/frontend/src/common/request.js +5 -2
- package/fe/frontend/src/hooks/useRouterParams.d.ts +4 -2
- package/fe/packages/common/ssr/hydration.d.ts +9 -0
- package/fe/packages/react/core/Label/Label.d.ts +1 -0
- package/fe/packages/react/core/Label/Label.js +3 -1
- package/fe/packages/react/core/internal/i18n/default.d.ts +13 -0
- package/fe/packages/react/core/internal/i18n/default.js +16 -1
- package/fe/packages/react/core/internal/i18n/en-US.d.ts +13 -0
- package/fe/packages/react/core/internal/i18n/en-US.js +16 -1
- package/fe/packages/react/forms/DataTable/index.d.ts +2 -0
- package/fe/packages/react/forms/DataTable/index.js +43 -29
- package/fe/packages/react/forms/Form/FormItem.js +39 -12
- package/fe/packages/react/forms/MultiSelect/MultiSelect.d.ts +26 -0
- package/fe/packages/react/forms/MultiSelect/MultiSelect.js +218 -0
- package/fe/packages/react/forms/MultiSelect/index.d.ts +1 -0
- package/fe/packages/react/forms/MultiSelect/index.js +2 -0
- package/fe/packages/react/forms/Pagination/Pagination.d.ts +6 -1
- package/fe/packages/react/forms/Pagination/Pagination.js +13 -4
- package/fe/packages/react/forms/Select/Select.d.ts +11 -0
- package/fe/packages/react/forms/Select/Select.js +20 -7
- package/fe/packages/react/forms/index.d.ts +1 -0
- package/fe/packages/react/forms/index.js +3 -1
- package/fe/packages/react/i18n/getUiReactLocale.d.ts +26 -0
- package/fe/packages/react/index.js +2 -1
- package/fe/packages/react/overlays/Drawer/Drawer.d.ts +3 -2
- package/fe/packages/react/overlays/Drawer/Drawer.js +16 -2
- package/fe/packages/react/overlays/Dropdown/Dropdown.d.ts +12 -2
- package/fe/packages/react/overlays/Dropdown/Dropdown.js +27 -14
- package/fe/packages/react/overlays/Modal/Modal.d.ts +11 -3
- package/fe/packages/react/overlays/Modal/Modal.js +26 -2
- package/fe/packages/react/overlays/Modal/ModalManager.d.ts +13 -12
- package/fe/packages/react/overlays/Modal/ModalManager.js +15 -5
- package/fe/packages/react/overlays/Modal/index.d.ts +1 -0
- package/fe/packages/react/overlays/Overlay/Overlay.d.ts +6 -2
- package/fe/packages/react/overlays/Overlay/Overlay.js +31 -14
- package/fe/packages/react/overlays/Overlay/focusTypes.d.ts +8 -0
- package/fe/packages/react/overlays/Overlay/focusTypes.js +0 -0
- package/fe/packages/react/overlays/Overlay/index.d.ts +1 -0
- package/fe/packages/react/overlays/Overlay/overlayStack.d.ts +16 -0
- package/fe/packages/react/overlays/Overlay/overlayStack.js +290 -0
- package/fe/packages/react/overlays/Popup/Popup.d.ts +5 -4
- package/fe/packages/react/overlays/Popup/Popup.js +28 -11
- package/fe/packages/react/ui/components/index.js +2 -1
- package/fe/packages/react/ui/index.js +2 -1
- package/fe/ssr/components/StreamingRender/StreamingCache.d.ts +1 -0
- package/fe/ssr/components/StreamingRender/StreamingCache.js +5 -1
- package/fe/ssr/components/StreamingRender/StreamingRender.js +1 -1
- package/fe/ssr/components/StreamingRender/StreamingScript.d.ts +1 -0
- package/fe/ssr/components/StreamingRender/StreamingScript.js +9 -1
- package/fe/ssr/createSSREntry.d.ts +9 -3
- package/fe/ssr/createSSREntry.js +43 -19
- package/fe/ssr/index.d.ts +4 -1
- package/fe/ssr/index.js +2 -1
- package/fe/ssr/router.d.ts +76 -0
- package/fe/ssr/router.js +349 -0
- package/fe/ssr/types.d.ts +1 -1
- package/models/packages/common/ssr/hydration.d.ts +9 -0
- package/models/packages/react/core/Label/Label.d.ts +1 -0
- package/models/packages/react/core/internal/i18n/default.d.ts +13 -0
- package/models/packages/react/core/internal/i18n/en-US.d.ts +13 -0
- package/models/packages/react/forms/DataTable/index.d.ts +2 -0
- package/models/packages/react/forms/MultiSelect/MultiSelect.d.ts +26 -0
- package/models/packages/react/forms/MultiSelect/index.d.ts +1 -0
- package/models/packages/react/forms/Pagination/Pagination.d.ts +6 -1
- package/models/packages/react/forms/Select/Select.d.ts +11 -0
- package/models/packages/react/forms/index.d.ts +1 -0
- package/models/packages/react/i18n/getUiReactLocale.d.ts +26 -0
- package/models/packages/react/overlays/Drawer/Drawer.d.ts +3 -2
- package/models/packages/react/overlays/Dropdown/Dropdown.d.ts +12 -2
- package/models/packages/react/overlays/Modal/Modal.d.ts +11 -3
- package/models/packages/react/overlays/Modal/ModalManager.d.ts +13 -12
- package/models/packages/react/overlays/Modal/index.d.ts +1 -0
- package/models/packages/react/overlays/Overlay/Overlay.d.ts +6 -2
- package/models/packages/react/overlays/Overlay/focusTypes.d.ts +8 -0
- package/models/packages/react/overlays/Overlay/index.d.ts +1 -0
- package/models/packages/react/overlays/Overlay/overlayStack.d.ts +16 -0
- package/models/packages/react/overlays/Popup/Popup.d.ts +5 -4
- package/package.json +26 -1
- package/types/app/ai/agent/agent-scanner.service.d.ts +4 -4
- package/types/app/ai/ai-config.service.d.ts +7 -3
- package/types/app/ai/guardrail/guardrail.service.d.ts +2 -2
- package/types/app/ai/index.d.ts +9 -0
- package/types/app/ai/mcp/internal/load-mcp-config.d.ts +2 -2
- package/types/app/ai/mcp/internal/read-sse-response.d.ts +2 -0
- package/types/app/ai/mcp/internal/stdio-process.d.ts +5 -0
- package/types/app/ai/mcp/mcp-registry.service.d.ts +5 -3
- package/types/app/ai/mcp/mcp.client.d.ts +7 -4
- package/types/app/ai/mcp/mcp.loader.d.ts +11 -11
- package/types/app/ai/mcp/mcp.module.d.ts +5 -5
- package/types/app/ai/memory/thread.service.d.ts +3 -3
- package/types/app/ai/models/chat-sse.d.ts +5 -0
- package/types/app/ai/models/chat.d.ts +22 -0
- package/types/app/ai/models/http.d.ts +20 -0
- package/types/app/ai/models/image.d.ts +69 -0
- package/types/app/ai/models/video.d.ts +99 -0
- package/types/app/ai/nest-service-provider.d.ts +6 -0
- package/types/app/ai/retention/conversation-retention.service.d.ts +3 -4
- package/types/app/ai/runtime/ai-runtime-startup.validator.d.ts +2 -3
- package/types/app/ai/runtime/ai-runtime.d.ts +46 -0
- package/types/app/ai/runtime/internal/agent-run-context.d.ts +4 -0
- package/types/app/ai/runtime/internal/context-compaction.service.d.ts +2 -2
- package/types/app/ai/runtime/internal/declaration-snapshot.d.ts +8 -0
- package/types/app/ai/runtime/internal/owned-runtime-resources.d.ts +5 -0
- package/types/app/ai/runtime/langchain/langchain-planner.service.d.ts +1 -1
- package/types/app/ai/runtime/langgraph/langgraph-orchestrator.service.d.ts +33 -16
- package/types/app/ai/skill/skill-scanner.service.d.ts +3 -4
- package/types/app/ai/snapshot-data.d.ts +1 -0
- package/types/app/ai/store/internal/create-vector-store.d.ts +4 -0
- package/types/app/ai/tool/builtin/index.d.ts +1 -1
- package/types/app/ai/tool/builtin/invoke-agent.tool.d.ts +10 -3
- package/types/app/ai/tool/builtin/retrieve.tool.d.ts +4 -3
- package/types/app/ai/tool/executor.service.d.ts +3 -3
- package/types/app/ai/tool/tool-registry.service.d.ts +2 -7
- package/types/app/ai/tool/tool.interface.d.ts +1 -1
- package/types/app/ai/tool/tool.module.d.ts +8 -1
- package/types/app/ai/trace/trace.service.d.ts +4 -5
- package/types/app/csr/types.d.ts +10 -0
- package/types/app/html/browser-options.d.ts +1 -1
- package/types/app/html/shell.d.ts +37 -10
- package/types/app/http/api-signing.d.ts +16 -0
- package/types/app/http/body-parser.middleware.d.ts +6 -2
- package/types/app/http/csp-nonce.d.ts +3 -0
- package/types/app/http/error-response.d.ts +1 -0
- package/types/app/http/request-body-parser-policy.d.ts +8 -0
- package/types/app/http/security.guard.d.ts +1 -0
- package/types/app/ssr/artifacts.d.ts +6 -3
- package/types/app/ssr/html.d.ts +7 -0
- package/types/app/ssr/stream.d.ts +1 -1
- package/types/app/ssr/types.d.ts +56 -0
- package/types/bundler/buildSSR.d.ts +2 -1
- package/types/bundler/index.d.ts +1 -1
- package/types/bundler/state.d.ts +13 -0
- package/types/bundler/utils.d.ts +3 -14
- package/types/config/config.default.d.ts +9 -3
- package/types/index.d.ts +4 -1
- package/types/packages/common/ssr/hydration.d.ts +13 -0
- package/types/packages/core/index.d.ts +1 -1
- package/types/packages/core/startup.d.ts +41 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region app/ai/models/chat-sse.ts
|
|
2
|
+
async function* e(e, t) {
|
|
3
|
+
let n = "", r = !1, i = e.pipeThrough(new TextDecoderStream(), { signal: t }).getReader();
|
|
4
|
+
try {
|
|
5
|
+
for (;;) {
|
|
6
|
+
let { done: e, value: t } = await i.read();
|
|
7
|
+
if (e) break;
|
|
8
|
+
let a = r && t.startsWith("\n") ? t.slice(1) : t;
|
|
9
|
+
r = a.endsWith("\r");
|
|
10
|
+
let o = a.split(/\r\n|[\r\n]/);
|
|
11
|
+
o[0] = n + o[0], n = o.pop() ?? "", yield* o;
|
|
12
|
+
}
|
|
13
|
+
n && (yield n);
|
|
14
|
+
} finally {
|
|
15
|
+
try {
|
|
16
|
+
await i.cancel();
|
|
17
|
+
} finally {
|
|
18
|
+
i.releaseLock();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function* t(t, n) {
|
|
23
|
+
if (!t.body) throw Error("Chat stream response has no body");
|
|
24
|
+
let r = "", i = [];
|
|
25
|
+
for await (let a of e(t.body, n)) {
|
|
26
|
+
if (a === "") {
|
|
27
|
+
i.length && (yield {
|
|
28
|
+
event: r,
|
|
29
|
+
data: i.join("\n"),
|
|
30
|
+
atEOF: !1
|
|
31
|
+
}), r = "", i = [];
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let e = a.indexOf(":"), t = e < 0 ? a : a.slice(0, e), n = e < 0 ? "" : a.slice(e + 1).replace(/^ /, "");
|
|
35
|
+
t === "data" ? i.push(n) : t === "event" && (r = n);
|
|
36
|
+
}
|
|
37
|
+
i.length && (yield {
|
|
38
|
+
event: r,
|
|
39
|
+
data: i.join("\n"),
|
|
40
|
+
atEOF: !0
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { t as chatEvents };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { chatEvents as e } from "./chat-sse.js";
|
|
2
|
+
import { ModelHttpError as t, checkModelError as n, createModelHttp as r } from "./http.js";
|
|
3
|
+
//#region app/ai/models/chat.ts
|
|
4
|
+
function i(e) {
|
|
5
|
+
return typeof e == "object" && !!e;
|
|
6
|
+
}
|
|
7
|
+
function a(e) {
|
|
8
|
+
if (!i(e) || !Array.isArray(e.choices)) return;
|
|
9
|
+
let t = e.choices[0];
|
|
10
|
+
return i(t) ? t : void 0;
|
|
11
|
+
}
|
|
12
|
+
function o(e, t) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(e);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
throw t ? Error("Chat stream truncated inside an SSE event", { cause: e }) : e;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function s(s) {
|
|
20
|
+
if ("adapter" in s) return s.adapter;
|
|
21
|
+
let c = r(s, "https://api.openai.com/v1");
|
|
22
|
+
function l({ signal: e, ...t }, n) {
|
|
23
|
+
return e?.throwIfAborted(), c.postJson("chat/completions", {
|
|
24
|
+
...t,
|
|
25
|
+
model: c.model,
|
|
26
|
+
stream: n
|
|
27
|
+
}, e);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
async generate(e) {
|
|
31
|
+
let t = await l(e, !1), r = await t.json();
|
|
32
|
+
n(r, t);
|
|
33
|
+
let o = a(r)?.message;
|
|
34
|
+
if (!i(o) || typeof o.content != "string") throw Error("Chat completion did not contain a text message");
|
|
35
|
+
return o.content;
|
|
36
|
+
},
|
|
37
|
+
async *stream(r) {
|
|
38
|
+
let { signal: s } = r, c = await l(r, !0), u = c.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
|
|
39
|
+
if (u && u !== "text/event-stream" && u !== "text/plain") throw u.endsWith("/json") || u.endsWith("+json") ? n(await c.json(), c) : await c.body?.cancel(), Error(`Expected a Chat event stream, received ${u}`);
|
|
40
|
+
let d = !1;
|
|
41
|
+
for await (let { event: r, data: l, atEOF: u } of e(c, s)) {
|
|
42
|
+
if (s?.throwIfAborted(), r !== "error") {
|
|
43
|
+
let e = l.trim();
|
|
44
|
+
if (e === "[DONE]") return;
|
|
45
|
+
if (!e) continue;
|
|
46
|
+
}
|
|
47
|
+
let e = o(l, u);
|
|
48
|
+
if (r === "error") throw t.fromResponse(c, e);
|
|
49
|
+
n(e, c);
|
|
50
|
+
let f = a(e);
|
|
51
|
+
f?.finish_reason != null && (d = !0);
|
|
52
|
+
let p = f?.delta;
|
|
53
|
+
i(p) && typeof p.content == "string" && p.content && (yield p.content);
|
|
54
|
+
}
|
|
55
|
+
if (s?.throwIfAborted(), !d) throw Error("Chat stream truncated before [DONE] or a finish_reason");
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { t as ModelHttpError, s as createChatModel };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region app/ai/models/http.ts
|
|
2
|
+
var e = class e extends Error {
|
|
3
|
+
static fromResponse(t, n) {
|
|
4
|
+
let r = t.headers.get("x-request-id") ?? t.headers.get("x-tt-logid");
|
|
5
|
+
return new e(t.status, n, r);
|
|
6
|
+
}
|
|
7
|
+
constructor(e, t, n) {
|
|
8
|
+
let r = t && typeof t == "object" && "error" in t ? t.error : t, i = r && typeof r == "object" && "message" in r ? r.message : r;
|
|
9
|
+
super(`Model request failed (HTTP ${e})${typeof i == "string" && i ? `: ${i}` : ""}`), this.status = e, this.body = t, this.requestId = n, this.name = "ModelHttpError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
function t(t, n) {
|
|
13
|
+
if (typeof t == "object" && t && "error" in t && t.error != null) throw e.fromResponse(n, t);
|
|
14
|
+
}
|
|
15
|
+
function n({ apiKey: t, baseURL: n, model: r, fetch: i = globalThis.fetch }, a) {
|
|
16
|
+
if (typeof r != "string" || !r.trim()) throw TypeError("A model name is required");
|
|
17
|
+
let o = new URL(n ?? a);
|
|
18
|
+
if (o.protocol !== "https:" && o.protocol !== "http:") throw TypeError("Model baseURL must be an HTTP or HTTPS URL");
|
|
19
|
+
async function s(n, r = {}) {
|
|
20
|
+
let a = new URL(o);
|
|
21
|
+
a.pathname = `${a.pathname.replace(/\/+$/, "")}/${n.replace(/^\/+/, "")}`;
|
|
22
|
+
let s = new Headers(r.headers);
|
|
23
|
+
t && s.set("authorization", `Bearer ${t}`);
|
|
24
|
+
let c = await i(a, {
|
|
25
|
+
...r,
|
|
26
|
+
headers: s
|
|
27
|
+
});
|
|
28
|
+
if (c.ok) return c;
|
|
29
|
+
let l = await c.text(), u = l;
|
|
30
|
+
try {
|
|
31
|
+
u = JSON.parse(l);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (!(e instanceof SyntaxError)) throw e;
|
|
34
|
+
}
|
|
35
|
+
throw e.fromResponse(c, u);
|
|
36
|
+
}
|
|
37
|
+
function c(e, t, n) {
|
|
38
|
+
return s(e, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: { "content-type": "application/json" },
|
|
41
|
+
body: JSON.stringify(t),
|
|
42
|
+
signal: n
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
model: r,
|
|
47
|
+
request: s,
|
|
48
|
+
postJson: c
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { e as ModelHttpError, t as checkModelError, n as createModelHttp };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ModelHttpError as e, checkModelError as t, createModelHttp as n } from "./http.js";
|
|
2
|
+
//#region app/ai/models/image.ts
|
|
3
|
+
function r(e, t, n) {
|
|
4
|
+
if (!(n instanceof Blob)) throw TypeError(`${t} must be a Blob or File`);
|
|
5
|
+
e.append(t, n);
|
|
6
|
+
}
|
|
7
|
+
function i({ image: e, mask: t, ...n }, i) {
|
|
8
|
+
let a = new FormData();
|
|
9
|
+
for (let [e, t] of Object.entries({
|
|
10
|
+
...n,
|
|
11
|
+
model: i
|
|
12
|
+
})) t !== void 0 && a.set(e, String(t));
|
|
13
|
+
if (Array.isArray(e)) {
|
|
14
|
+
if (!e.length) throw TypeError("Image edits require at least one image");
|
|
15
|
+
for (let t of e) r(a, "image[]", t);
|
|
16
|
+
} else r(a, "image", e);
|
|
17
|
+
return t !== void 0 && r(a, "mask", t), a;
|
|
18
|
+
}
|
|
19
|
+
function a(e) {
|
|
20
|
+
if ("adapter" in e) return e.adapter;
|
|
21
|
+
let r = n(e, "https://api.openai.com/v1"), { model: a } = r;
|
|
22
|
+
return { async generate(e) {
|
|
23
|
+
if (e.stream) throw TypeError("Streaming is unsupported for completed image results");
|
|
24
|
+
if (e.image === void 0 && e.mask !== void 0) throw TypeError("A mask requires an input image");
|
|
25
|
+
let n = e.image === void 0 ? await r.postJson("images/generations", {
|
|
26
|
+
...e,
|
|
27
|
+
model: a
|
|
28
|
+
}) : await r.request("images/edits", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
body: i(e, a)
|
|
31
|
+
}), o = await n.json();
|
|
32
|
+
if (t(o, n), !(o?.data && Array.isArray(o.data) && o.data.length > 0 && o.data.every((e) => typeof e?.url == "string" && e.url.length > 0 || typeof e?.b64_json == "string" && e.b64_json.length > 0)) || typeof o?.created != "number") throw TypeError("OpenAI returned no usable image data");
|
|
33
|
+
return o;
|
|
34
|
+
} };
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { e as ModelHttpError, a as createImageModel };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ModelHttpError as e, checkModelError as t, createModelHttp as n } from "./http.js";
|
|
2
|
+
//#region app/ai/models/video.ts
|
|
3
|
+
var r = "https://ark.cn-beijing.volces.com/api/v3", i = "/contents/generations/tasks";
|
|
4
|
+
async function a(e) {
|
|
5
|
+
let n = await e.json();
|
|
6
|
+
if (typeof n?.id != "string" || !n.id.trim()) throw t(n, e), TypeError("Ark returned a video response without a task ID");
|
|
7
|
+
return n;
|
|
8
|
+
}
|
|
9
|
+
function o(e) {
|
|
10
|
+
return typeof e.status == "string" && e.status.trim().length > 0;
|
|
11
|
+
}
|
|
12
|
+
function s(e) {
|
|
13
|
+
if ("adapter" in e) return e.adapter;
|
|
14
|
+
let t = n(e, r);
|
|
15
|
+
return {
|
|
16
|
+
async submit(e) {
|
|
17
|
+
return a(await t.postJson(i, {
|
|
18
|
+
...e,
|
|
19
|
+
model: t.model
|
|
20
|
+
}));
|
|
21
|
+
},
|
|
22
|
+
async get(e) {
|
|
23
|
+
if (typeof e != "string" || !e.trim() || e === "." || e === "..") throw TypeError("A valid Ark task ID is required");
|
|
24
|
+
let n = await a(await t.request(`${i}/${encodeURIComponent(e)}`, { method: "GET" }));
|
|
25
|
+
if (n.id !== e) throw TypeError("Ark returned a different task ID");
|
|
26
|
+
if (!o(n)) throw TypeError("Ark returned a video task without a status");
|
|
27
|
+
return n;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { e as ModelHttpError, s as createVideoModel };
|
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
import { AiConfigService as e } from "../ai-config.service.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { VECTOR_STORE as t } from "../store/store.interface.js";
|
|
3
|
+
import { StoreModule as n } from "../store/store.module.js";
|
|
4
|
+
import { resolveRagOptions as r } from "./internal/load-rag-config.js";
|
|
5
|
+
import { RetrieverService as i } from "./retriever.service.js";
|
|
6
6
|
import { resolveChunker as a } from "./chunker.js";
|
|
7
|
-
import {
|
|
8
|
-
import s from "@
|
|
7
|
+
import { CHUNKER as o } from "./rag.interface.js";
|
|
8
|
+
import { Module as s } from "@nestjs/common";
|
|
9
|
+
import c from "@oxc-project/runtime/helpers/decorate";
|
|
9
10
|
//#region app/ai/rag/rag.module.ts
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
imports: [
|
|
11
|
+
var l = class {};
|
|
12
|
+
l = c([s({
|
|
13
|
+
imports: [n],
|
|
13
14
|
providers: [{
|
|
14
|
-
provide:
|
|
15
|
-
useFactory: (e) => a(
|
|
15
|
+
provide: o,
|
|
16
|
+
useFactory: (e) => a(r(e)),
|
|
16
17
|
inject: [e]
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
}, {
|
|
19
|
+
provide: i,
|
|
20
|
+
inject: [
|
|
21
|
+
t,
|
|
22
|
+
o,
|
|
23
|
+
e
|
|
24
|
+
],
|
|
25
|
+
useFactory: (e, t, n) => new i(e, t, n)
|
|
26
|
+
}],
|
|
27
|
+
exports: [i]
|
|
28
|
+
})], l);
|
|
20
29
|
//#endregion
|
|
21
|
-
export {
|
|
30
|
+
export { l as RagModule };
|
|
@@ -1,79 +1,62 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import { Inject as o, Injectable as s } from "@nestjs/common";
|
|
7
|
-
import * as c from "path";
|
|
8
|
-
import { globSync as l } from "glob";
|
|
9
|
-
import * as u from "fs";
|
|
10
|
-
import d from "@oxc-project/runtime/helpers/decorateMetadata";
|
|
11
|
-
import f from "@oxc-project/runtime/helpers/decorate";
|
|
12
|
-
import p from "@oxc-project/runtime/helpers/decorateParam";
|
|
1
|
+
import { resolveRagFixturesPath as e, resolveRagOptions as t } from "./internal/load-rag-config.js";
|
|
2
|
+
import { resolveEmbedder as n } from "./internal/load-embedding-config.js";
|
|
3
|
+
import { globSync as r } from "glob";
|
|
4
|
+
import * as i from "path";
|
|
5
|
+
import * as a from "fs";
|
|
13
6
|
//#region app/ai/rag/retriever.service.ts
|
|
14
|
-
var
|
|
15
|
-
function
|
|
16
|
-
return
|
|
7
|
+
var o = "**/*.{md,txt,markdown}";
|
|
8
|
+
function s(e, t) {
|
|
9
|
+
return i.relative(e, t).split(i.sep).join("/");
|
|
17
10
|
}
|
|
18
|
-
var
|
|
11
|
+
var c = class {
|
|
19
12
|
constructor(e, t, n) {
|
|
20
13
|
this.vectorStore = e, this.chunker = t, this.configService = n;
|
|
21
14
|
}
|
|
22
|
-
async ingestFromDirectory(
|
|
23
|
-
let
|
|
24
|
-
if (!
|
|
25
|
-
let
|
|
26
|
-
for (let e of
|
|
27
|
-
let
|
|
28
|
-
this.chunker.chunk(
|
|
29
|
-
docId:
|
|
15
|
+
async ingestFromDirectory(t) {
|
|
16
|
+
let r = t !== void 0 && t.trim() !== "" ? this.configService.resolvePath(t.trim()) : e(this.configService);
|
|
17
|
+
if (!a.existsSync(r)) throw Error(`[ai] rag fixtures 目录不存在:${r}`);
|
|
18
|
+
let i = this.findTextFiles(r), o = [];
|
|
19
|
+
for (let e of i) {
|
|
20
|
+
let t = a.readFileSync(e, "utf8"), n = s(r, e);
|
|
21
|
+
this.chunker.chunk(t, {
|
|
22
|
+
docId: n,
|
|
30
23
|
path: e
|
|
31
24
|
}).forEach((e, t) => {
|
|
32
|
-
let
|
|
33
|
-
|
|
34
|
-
id: `${
|
|
35
|
-
docId: e.docId ??
|
|
36
|
-
text:
|
|
25
|
+
let r = e.text.trim();
|
|
26
|
+
r !== "" && o.push({
|
|
27
|
+
id: `${n}#${t}`,
|
|
28
|
+
docId: e.docId ?? n,
|
|
29
|
+
text: r,
|
|
37
30
|
embedding: [],
|
|
38
31
|
...e.meta ? { meta: e.meta } : {}
|
|
39
32
|
});
|
|
40
33
|
});
|
|
41
34
|
}
|
|
42
|
-
if (
|
|
43
|
-
ingested:
|
|
35
|
+
if (o.length === 0) return {
|
|
36
|
+
ingested: i.length,
|
|
44
37
|
chunks: 0
|
|
45
38
|
};
|
|
46
|
-
let
|
|
47
|
-
if (
|
|
48
|
-
return
|
|
49
|
-
let n =
|
|
39
|
+
let c = await n(this.configService).embed(o.map((e) => e.text));
|
|
40
|
+
if (c.length !== o.length) throw Error(`[ai] embedding 数量与片段数不一致:${c.length} != ${o.length}`);
|
|
41
|
+
return o.forEach((e, t) => {
|
|
42
|
+
let n = c[t];
|
|
50
43
|
if (!n) throw Error(`[ai] embedding 缺少片段结果:${t}`);
|
|
51
44
|
e.embedding = n;
|
|
52
|
-
}), await this.vectorStore.upsert(
|
|
53
|
-
ingested:
|
|
54
|
-
chunks:
|
|
45
|
+
}), await this.vectorStore.upsert(o), {
|
|
46
|
+
ingested: i.length,
|
|
47
|
+
chunks: o.length
|
|
55
48
|
};
|
|
56
49
|
}
|
|
57
|
-
async retrieve(e,
|
|
58
|
-
let
|
|
59
|
-
if (
|
|
60
|
-
let a =
|
|
50
|
+
async retrieve(e, r) {
|
|
51
|
+
let i = e.trim();
|
|
52
|
+
if (i === "") throw Error("[ai] retrieve 的 query 不能为空");
|
|
53
|
+
let a = r ?? t(this.configService).topK, [o] = await n(this.configService).embed([i]);
|
|
61
54
|
if (!o) throw Error("[ai] embedding 返回为空");
|
|
62
55
|
return this.vectorStore.query(o, a);
|
|
63
56
|
}
|
|
64
57
|
findTextFiles(e) {
|
|
65
|
-
return
|
|
58
|
+
return r(`${e.split(i.sep).join("/")}/${o}`).sort();
|
|
66
59
|
}
|
|
67
60
|
};
|
|
68
|
-
_ = f([
|
|
69
|
-
s(),
|
|
70
|
-
p(0, o(t)),
|
|
71
|
-
p(1, o(a)),
|
|
72
|
-
d("design:paramtypes", [
|
|
73
|
-
Object,
|
|
74
|
-
Object,
|
|
75
|
-
typeof (m = e !== void 0 && e) == "function" ? m : Object
|
|
76
|
-
])
|
|
77
|
-
], _);
|
|
78
61
|
//#endregion
|
|
79
|
-
export {
|
|
62
|
+
export { c as RetrieverService };
|
|
@@ -1,47 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { THREAD_STATE_STORE as t } from "../store/store.interface.js";
|
|
3
|
-
import { RETENTION_SWEEP_INTERVAL_MS as n, isExpired as r, resolveRetentionDays as i } from "./retention-policy.js";
|
|
4
|
-
import { THREAD_EXECUTION_COORDINATOR as a } from "../runtime/thread-execution-coordinator.js";
|
|
5
|
-
import { Inject as o, Injectable as s, Logger as c } from "@nestjs/common";
|
|
6
|
-
import l from "@oxc-project/runtime/helpers/decorateMetadata";
|
|
7
|
-
import u from "@oxc-project/runtime/helpers/decorate";
|
|
8
|
-
import d from "@oxc-project/runtime/helpers/decorateParam";
|
|
1
|
+
import { RETENTION_SWEEP_INTERVAL_MS as e, isExpired as t, resolveRetentionDays as n } from "./retention-policy.js";
|
|
9
2
|
//#region app/ai/retention/conversation-retention.service.ts
|
|
10
|
-
var
|
|
11
|
-
logger = new c(p.name);
|
|
3
|
+
var r = class {
|
|
12
4
|
timer;
|
|
13
5
|
cleanupPromise;
|
|
14
|
-
constructor(e, t, n) {
|
|
15
|
-
this.configService = e, this.store = t, this.coordinator = n;
|
|
6
|
+
constructor(e, t, n, r = console) {
|
|
7
|
+
this.configService = e, this.store = t, this.coordinator = n, this.logger = r;
|
|
16
8
|
}
|
|
17
9
|
async onModuleInit() {
|
|
18
10
|
this.retentionDays() !== void 0 && (this.assertStoreCapabilities(), this.timer = setInterval(() => {
|
|
19
11
|
this.runScheduledCleanup();
|
|
20
|
-
},
|
|
12
|
+
}, e), this.timer.unref?.(), await this.runScheduledCleanup());
|
|
21
13
|
}
|
|
22
14
|
async onModuleDestroy() {
|
|
23
15
|
this.timer && clearInterval(this.timer), this.timer = void 0, await this.cleanupPromise;
|
|
24
16
|
}
|
|
25
17
|
async assertNotExpired(e) {
|
|
26
|
-
let
|
|
27
|
-
if (
|
|
18
|
+
let n = this.retentionDays();
|
|
19
|
+
if (n === void 0) return;
|
|
28
20
|
this.assertStoreCapabilities();
|
|
29
|
-
let
|
|
30
|
-
if (
|
|
21
|
+
let r = await this.store.getLastActivityAt?.(e) ?? null;
|
|
22
|
+
if (r !== null && t(r, n)) throw Error(`[ai] conversation has expired: ${e}`);
|
|
31
23
|
}
|
|
32
24
|
async cleanupExpired() {
|
|
33
25
|
let e = this.retentionDays();
|
|
34
26
|
if (e === void 0) return 0;
|
|
35
27
|
this.assertStoreCapabilities();
|
|
36
|
-
let
|
|
37
|
-
for (let i of
|
|
38
|
-
let
|
|
39
|
-
|
|
28
|
+
let n = await this.store.listThreadIds?.() ?? [], r = 0;
|
|
29
|
+
for (let i of n) await this.coordinator.runExclusive(i, async () => {
|
|
30
|
+
let n = await this.store.getLastActivityAt?.(i) ?? null;
|
|
31
|
+
n === null || !t(n, e) || (await this.store.delete?.(i), r += 1);
|
|
40
32
|
});
|
|
41
|
-
return
|
|
33
|
+
return r;
|
|
42
34
|
}
|
|
43
35
|
retentionDays() {
|
|
44
|
-
return
|
|
36
|
+
return n(this.configService, "ai.store.conversationRetentionDays");
|
|
45
37
|
}
|
|
46
38
|
assertStoreCapabilities() {
|
|
47
39
|
if (typeof this.store.getLastActivityAt != "function" || typeof this.store.listThreadIds != "function" || typeof this.store.delete != "function") throw Error("[ai] ai.store.conversationRetentionDays requires ThreadStateStore getLastActivityAt, listThreadIds, and delete");
|
|
@@ -52,15 +44,5 @@ var f, p, m = p = class {
|
|
|
52
44
|
}), this.cleanupPromise;
|
|
53
45
|
}
|
|
54
46
|
};
|
|
55
|
-
m = p = u([
|
|
56
|
-
s(),
|
|
57
|
-
d(1, o(t)),
|
|
58
|
-
d(2, o(a)),
|
|
59
|
-
l("design:paramtypes", [
|
|
60
|
-
typeof (f = e !== void 0 && e) == "function" ? f : Object,
|
|
61
|
-
Object,
|
|
62
|
-
Object
|
|
63
|
-
])
|
|
64
|
-
], m);
|
|
65
47
|
//#endregion
|
|
66
|
-
export {
|
|
48
|
+
export { r as ConversationRetentionService };
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import { AiConfigService as e } from "../ai-config.service.js";
|
|
2
|
-
import { THREAD_STATE_STORE as t } from "../store/store.interface.js";
|
|
3
|
-
import { THREAD_EXECUTION_COORDINATOR as n } from "./thread-execution-coordinator.js";
|
|
4
|
-
import { Inject as r, Injectable as i } from "@nestjs/common";
|
|
5
|
-
import a from "@oxc-project/runtime/helpers/decorateMetadata";
|
|
6
|
-
import o from "@oxc-project/runtime/helpers/decorate";
|
|
7
|
-
import s from "@oxc-project/runtime/helpers/decorateParam";
|
|
8
1
|
//#region app/ai/runtime/ai-runtime-startup.validator.ts
|
|
9
|
-
var
|
|
2
|
+
var e = class {
|
|
10
3
|
constructor(e, t, n) {
|
|
11
4
|
this.configService = e, this.stateStore = t, this.executionCoordinator = n;
|
|
12
5
|
}
|
|
@@ -18,15 +11,5 @@ var c, l = class {
|
|
|
18
11
|
if ((typeof this.executionCoordinator?.runExclusive != "function" || this.executionCoordinator.capabilities?.crossInstanceFifo !== !0) && t.push("ThreadExecutionCoordinator cross-instance FIFO"), (typeof this.stateStore?.appendArchiveMessages != "function" || this.stateStore.capabilities?.crossInstanceAtomicArchiveAppend !== !0) && t.push("ThreadStateStore cross-instance atomic archive batch append"), (typeof this.stateStore?.compareAndSetCheckpoint != "function" || this.stateStore.capabilities?.crossInstanceCheckpointCas !== !0) && t.push("ThreadStateStore cross-instance checkpoint CAS"), (typeof this.stateStore?.commitThreadMigration != "function" || this.stateStore.capabilities?.crossInstanceAtomicMigration !== !0) && t.push("ThreadStateStore cross-instance atomic thread migration"), t.length > 0) throw Error(`[ai] ai.runtime.multiInstance requires ${t.join(", ")}`);
|
|
19
12
|
}
|
|
20
13
|
};
|
|
21
|
-
l = o([
|
|
22
|
-
i(),
|
|
23
|
-
s(1, r(t)),
|
|
24
|
-
s(2, r(n)),
|
|
25
|
-
a("design:paramtypes", [
|
|
26
|
-
typeof (c = e !== void 0 && e) == "function" ? c : Object,
|
|
27
|
-
Object,
|
|
28
|
-
Object
|
|
29
|
-
])
|
|
30
|
-
], l);
|
|
31
14
|
//#endregion
|
|
32
|
-
export {
|
|
15
|
+
export { e as AiRuntimeStartupValidator };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { AgentScannerService as e } from "../agent/agent-scanner.service.js";
|
|
2
|
+
import { AiConfigService as t } from "../ai-config.service.js";
|
|
3
|
+
import { resolveMaxDelegationDepth as n } from "./internal/load-runtime-config.js";
|
|
4
|
+
import { resolveThreadExecutionCoordinator as r } from "./thread-execution-coordinator.js";
|
|
5
|
+
import { LangGraphOrchestratorService as i } from "./langgraph/langgraph-orchestrator.service.js";
|
|
6
|
+
import { ToolRegistryService as a } from "../tool/tool-registry.service.js";
|
|
7
|
+
import { TraceService as o } from "../trace/trace.service.js";
|
|
8
|
+
import { GuardrailService as s } from "../guardrail/guardrail.service.js";
|
|
9
|
+
import { CheckpointService as c } from "../memory/checkpoint.service.js";
|
|
10
|
+
import { resolveTraceStorePath as l } from "../store/internal/load-store-config.js";
|
|
11
|
+
import { createVectorStore as u } from "../store/internal/create-vector-store.js";
|
|
12
|
+
import { resolveThreadStateStore as d } from "../store/internal/load-thread-state-store.js";
|
|
13
|
+
import { FileTraceStore as f } from "../store/reference/file-trace.store.js";
|
|
14
|
+
import { ThreadService as p } from "../memory/thread.service.js";
|
|
15
|
+
import { ConversationRetentionService as m } from "../retention/conversation-retention.service.js";
|
|
16
|
+
import { SkillScannerService as h } from "../skill/skill-scanner.service.js";
|
|
17
|
+
import { ExecutorService as g } from "../tool/executor.service.js";
|
|
18
|
+
import { resolveRagOptions as _ } from "../rag/internal/load-rag-config.js";
|
|
19
|
+
import { RetrieverService as v } from "../rag/retriever.service.js";
|
|
20
|
+
import { createInvokeAgentTool as y } from "../tool/builtin/invoke-agent.tool.js";
|
|
21
|
+
import { createRetrieveTool as b } from "../tool/builtin/retrieve.tool.js";
|
|
22
|
+
import { AiRuntimeStartupValidator as x } from "./ai-runtime-startup.validator.js";
|
|
23
|
+
import { ContextCompactionService as S } from "./internal/context-compaction.service.js";
|
|
24
|
+
import { LangChainPlannerService as C } from "./langchain/langchain-planner.service.js";
|
|
25
|
+
import { McpRegistryService as w } from "../mcp/mcp-registry.service.js";
|
|
26
|
+
import { McpLoader as T } from "../mcp/mcp.loader.js";
|
|
27
|
+
import { resolveChunker as E } from "../rag/chunker.js";
|
|
28
|
+
import { snapshotAgent as D, snapshotExecutable as O } from "./internal/declaration-snapshot.js";
|
|
29
|
+
import { OwnedRuntimeResources as k } from "./internal/owned-runtime-resources.js";
|
|
30
|
+
import A from "node:path";
|
|
31
|
+
//#region app/ai/runtime/ai-runtime.ts
|
|
32
|
+
function j(e) {
|
|
33
|
+
if (typeof e != "object" || !e || Array.isArray(e)) throw Error("[ai] run requires a request object");
|
|
34
|
+
let t = typeof e.agent == "string" ? e.agent.trim() : "";
|
|
35
|
+
if (t === "") throw Error("[ai] run requires a non-empty Agent name");
|
|
36
|
+
if (e.threadId !== void 0 && typeof e.threadId != "string") throw Error("[ai] run threadId must be a string");
|
|
37
|
+
if (e.hardConstraints !== void 0 && (!Array.isArray(e.hardConstraints) || !e.hardConstraints.every((e) => typeof e == "string"))) throw Error("[ai] run hardConstraints must be an array of strings");
|
|
38
|
+
let n = e.threadId?.trim();
|
|
39
|
+
return {
|
|
40
|
+
agent: t,
|
|
41
|
+
input: e.input,
|
|
42
|
+
...n ? { threadId: n } : {},
|
|
43
|
+
...e.hardConstraints === void 0 ? {} : { hardConstraints: [...e.hardConstraints] }
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function M(e) {
|
|
47
|
+
if (typeof e != "object" || !e || Array.isArray(e)) throw Error("[ai] createAiRuntime requires an options object");
|
|
48
|
+
if (typeof e.baseDir != "string" || e.baseDir.trim() === "") throw Error("[ai] createAiRuntime requires a non-empty baseDir");
|
|
49
|
+
if (!Array.isArray(e.agents) || e.agents.length === 0) throw Error("[ai] createAiRuntime requires at least one Agent definition");
|
|
50
|
+
for (let t of ["skills", "tools"]) if (e[t] !== void 0 && !Array.isArray(e[t])) throw Error(`[ai] createAiRuntime ${t} must be an array`);
|
|
51
|
+
if (e.config !== void 0 && (typeof e.config != "object" || e.config === null || Array.isArray(e.config))) throw Error("[ai] createAiRuntime config must be an object");
|
|
52
|
+
}
|
|
53
|
+
function N(e) {
|
|
54
|
+
return {
|
|
55
|
+
run: async (t, n = {}) => {
|
|
56
|
+
let r = j(t);
|
|
57
|
+
if (typeof n != "object" || !n || Array.isArray(n)) throw Error("[ai] run options must be an object");
|
|
58
|
+
if (n.onEvent !== void 0 && typeof n.onEvent != "function") throw Error("[ai] run onEvent must be a function");
|
|
59
|
+
return e.run(r.agent, r.input, r.threadId, n.onEvent, r.hardConstraints === void 0 ? {} : { hardConstraints: r.hardConstraints });
|
|
60
|
+
},
|
|
61
|
+
async *runStream(t) {
|
|
62
|
+
let n = j(t);
|
|
63
|
+
return yield* e.runStream(n.agent, n.input, n.threadId, n.hardConstraints === void 0 ? {} : { hardConstraints: n.hardConstraints });
|
|
64
|
+
},
|
|
65
|
+
getGraph: () => e.getGraph(),
|
|
66
|
+
inspectThreadContext: (t) => e.inspectThreadContext(t),
|
|
67
|
+
previewThreadMigration: (t, n) => e.previewThreadMigration(t, n),
|
|
68
|
+
confirmThreadMigration: (t) => e.confirmThreadMigration(t),
|
|
69
|
+
close: () => e.close()
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
async function P(j) {
|
|
73
|
+
M(j);
|
|
74
|
+
let P = new t({
|
|
75
|
+
baseDir: A.resolve(j.baseDir.trim()),
|
|
76
|
+
config: j.config ?? {}
|
|
77
|
+
}), F = new e(P), I = new h(P), L = new a(), R = new k(), { threadStateStore: z, threadExecutionCoordinator: B } = j.sharedAdapters ?? {};
|
|
78
|
+
try {
|
|
79
|
+
let e = j.agents.map(D), t = j.skills?.map((e) => O(e)) ?? [], a = j.tools?.map((e) => O(e)) ?? [];
|
|
80
|
+
for (let t of e) await F.register(t);
|
|
81
|
+
for (let e of t) I.register(e);
|
|
82
|
+
let h = z ?? await R.initialize(d(P)), k = B ?? await R.initialize(r(P));
|
|
83
|
+
new x(P, h, k).onModuleInit();
|
|
84
|
+
let A = await R.initialize(new f(l(P))), M = new C(P), V = new c(h), H = await R.initialize(new o(A, P)), U = await R.initialize(new m(P, h, k)), W = new i(M, new g(I, L), F, I, L, H, new s(P), new p(P), V, P, new S(M, V), k, U, () => R.close()), G = _(P), K;
|
|
85
|
+
L.register(b(G.tool, (e) => (K ??= new v(u(P), E(G), P), K.retrieve(e)))), L.register(y({
|
|
86
|
+
maxDepth: n(P),
|
|
87
|
+
findAgent: (e) => F.get(e),
|
|
88
|
+
run: (...e) => W.run(...e)
|
|
89
|
+
}));
|
|
90
|
+
let q = new T(P);
|
|
91
|
+
await R.initialize({ close: () => q.closeAll() }), (await q.load()).length > 0 && new w(q).registerAll(L);
|
|
92
|
+
for (let e of a) L.register(e);
|
|
93
|
+
return N(W);
|
|
94
|
+
} catch (e) {
|
|
95
|
+
try {
|
|
96
|
+
await R.close();
|
|
97
|
+
} catch (t) {
|
|
98
|
+
throw AggregateError([e, t], "[ai] standalone runtime initialization and cleanup failed");
|
|
99
|
+
}
|
|
100
|
+
throw e;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
export { P as createAiRuntime };
|