@elizaos/plugin-elizacloud 2.0.0-alpha.7 → 2.0.0-beta.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/LICENSE +21 -0
- package/README.md +220 -0
- package/auto-enable.ts +17 -0
- package/dist/browser/index.browser.js +2 -21
- package/dist/browser/index.browser.js.map +5 -37
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.node.cjs +12173 -2271
- package/dist/cjs/index.node.js.map +135 -27
- package/dist/cloud/auth.d.ts +19 -0
- package/dist/cloud/auth.d.ts.map +1 -0
- package/dist/cloud/auth.js +330 -0
- package/dist/cloud/auth.js.map +12 -0
- package/dist/cloud/backup.d.ts +18 -0
- package/dist/cloud/backup.d.ts.map +1 -0
- package/dist/cloud/backup.js +63 -0
- package/dist/cloud/backup.js.map +10 -0
- package/dist/cloud/base-url.d.ts +3 -0
- package/dist/cloud/base-url.d.ts.map +1 -0
- package/dist/cloud/base-url.js +77 -0
- package/dist/cloud/base-url.js.map +10 -0
- package/dist/cloud/bridge-client.d.ts +126 -0
- package/dist/cloud/bridge-client.d.ts.map +1 -0
- package/dist/cloud/bridge-client.js +432 -0
- package/dist/cloud/bridge-client.js.map +11 -0
- package/dist/cloud/cloud-api-key.d.ts +26 -0
- package/dist/cloud/cloud-api-key.d.ts.map +1 -0
- package/dist/cloud/cloud-api-key.js +60 -0
- package/dist/cloud/cloud-api-key.js.map +10 -0
- package/dist/cloud/cloud-manager.d.ts +33 -0
- package/dist/cloud/cloud-manager.d.ts.map +1 -0
- package/dist/cloud/cloud-manager.js +853 -0
- package/dist/cloud/cloud-manager.js.map +16 -0
- package/dist/cloud/cloud-proxy.d.ts +20 -0
- package/dist/cloud/cloud-proxy.d.ts.map +1 -0
- package/dist/cloud/cloud-proxy.js +54 -0
- package/dist/cloud/cloud-proxy.js.map +10 -0
- package/dist/cloud/cloud-wallet.d.ts +94 -0
- package/dist/cloud/cloud-wallet.d.ts.map +1 -0
- package/dist/cloud/cloud-wallet.js +5195 -0
- package/dist/cloud/cloud-wallet.js.map +92 -0
- package/dist/cloud/index.d.ts +9 -0
- package/dist/cloud/index.d.ts.map +1 -0
- package/dist/cloud/index.js +30 -0
- package/dist/cloud/index.js.map +9 -0
- package/dist/cloud/reconnect.d.ts +26 -0
- package/dist/cloud/reconnect.d.ts.map +1 -0
- package/dist/cloud/reconnect.js +104 -0
- package/dist/cloud/reconnect.js.map +10 -0
- package/dist/cloud/validate-url.d.ts +2 -0
- package/dist/cloud/validate-url.d.ts.map +1 -0
- package/dist/cloud/validate-url.js +174 -0
- package/dist/cloud/validate-url.js.map +10 -0
- package/dist/cloud-providers/cloud-status.d.ts.map +1 -1
- package/dist/cloud-providers/cloud-status.js +78 -0
- package/dist/cloud-providers/cloud-status.js.map +10 -0
- package/dist/cloud-providers/container-health.d.ts.map +1 -1
- package/dist/cloud-providers/container-health.js +74 -0
- package/dist/cloud-providers/container-health.js.map +10 -0
- package/dist/cloud-providers/credit-balance.d.ts.map +1 -1
- package/dist/cloud-providers/credit-balance.js +85 -0
- package/dist/cloud-providers/credit-balance.js.map +10 -0
- package/dist/cloud-providers/index.d.ts.map +1 -1
- package/dist/cloud-providers/index.js +24 -0
- package/dist/cloud-providers/index.js.map +9 -0
- package/dist/cloud-providers/model-registry.d.ts.map +1 -1
- package/dist/cloud-providers/model-registry.js +71 -0
- package/dist/cloud-providers/model-registry.js.map +10 -0
- package/dist/index.browser.d.ts +4 -2
- package/dist/index.browser.d.ts.map +1 -1
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12851 -0
- package/dist/index.js.map +145 -0
- package/dist/index.node.d.ts +15 -2
- package/dist/index.node.d.ts.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +169 -0
- package/dist/init.js.map +12 -0
- package/dist/lib/cloud-connection.d.ts +78 -0
- package/dist/lib/cloud-connection.d.ts.map +1 -0
- package/dist/lib/cloud-connection.js +731 -0
- package/dist/lib/cloud-connection.js.map +14 -0
- package/dist/lib/cloud-secrets.d.ts +23 -0
- package/dist/lib/cloud-secrets.d.ts.map +1 -0
- package/dist/lib/cloud-secrets.js +64 -0
- package/dist/lib/cloud-secrets.js.map +10 -0
- package/dist/lib/config-env.d.ts +5 -0
- package/dist/lib/config-env.d.ts.map +1 -0
- package/dist/lib/config-env.js +191 -0
- package/dist/lib/config-env.js.map +11 -0
- package/dist/lib/config-like.d.ts +40 -0
- package/dist/lib/config-like.d.ts.map +1 -0
- package/dist/lib/config-like.js +103 -0
- package/dist/lib/config-like.js.map +10 -0
- package/dist/lib/credential-type-map.d.ts +53 -0
- package/dist/lib/credential-type-map.d.ts.map +1 -0
- package/dist/lib/credential-type-map.js +88 -0
- package/dist/lib/credential-type-map.js.map +10 -0
- package/dist/lib/feature-flags.d.ts +2 -0
- package/dist/lib/feature-flags.d.ts.map +1 -0
- package/dist/lib/feature-flags.js +40 -0
- package/dist/lib/feature-flags.js.map +10 -0
- package/dist/lib/http.d.ts +22 -0
- package/dist/lib/http.d.ts.map +1 -0
- package/dist/lib/http.js +107 -0
- package/dist/lib/http.js.map +10 -0
- package/dist/lib/server-cloud-tts.d.ts +34 -0
- package/dist/lib/server-cloud-tts.d.ts.map +1 -0
- package/dist/lib/server-cloud-tts.js +549 -0
- package/dist/lib/server-cloud-tts.js.map +13 -0
- package/dist/lib/state-paths.d.ts +4 -0
- package/dist/lib/state-paths.d.ts.map +1 -0
- package/dist/lib/state-paths.js +52 -0
- package/dist/lib/state-paths.js.map +10 -0
- package/dist/lib/tts-debug.d.ts +4 -0
- package/dist/lib/tts-debug.d.ts.map +1 -0
- package/dist/lib/tts-debug.js +57 -0
- package/dist/lib/tts-debug.js.map +10 -0
- package/dist/models/embeddings.d.ts.map +1 -1
- package/dist/models/embeddings.js +319 -0
- package/dist/models/embeddings.js.map +13 -0
- package/dist/models/image.d.ts.map +1 -1
- package/dist/models/image.js +374 -0
- package/dist/models/image.js.map +14 -0
- package/dist/models/index.d.ts +1 -2
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +1386 -0
- package/dist/models/index.js.map +20 -0
- package/dist/models/research.d.ts.map +1 -1
- package/dist/models/research.js +324 -0
- package/dist/models/research.js.map +13 -0
- package/dist/models/speech.d.ts.map +1 -1
- package/dist/models/speech.js +273 -0
- package/dist/models/speech.js.map +13 -0
- package/dist/models/text.d.ts +5 -2
- package/dist/models/text.d.ts.map +1 -1
- package/dist/models/text.js +803 -0
- package/dist/models/text.js.map +15 -0
- package/dist/models/tokenization.d.ts.map +1 -1
- package/dist/models/tokenization.js +65 -0
- package/dist/models/tokenization.js.map +10 -0
- package/dist/models/transcription.d.ts.map +1 -1
- package/dist/models/transcription.js +283 -0
- package/dist/models/transcription.js.map +13 -0
- package/dist/node/index.d.ts +2 -2
- package/dist/node/index.node.js +12171 -2266
- package/dist/node/index.node.js.map +135 -27
- package/dist/onboarding.d.ts +35 -0
- package/dist/onboarding.d.ts.map +1 -0
- package/dist/onboarding.js +883 -0
- package/dist/onboarding.js.map +14 -0
- package/dist/plugin.d.ts +20 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +7611 -0
- package/dist/plugin.js.map +104 -0
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +127 -0
- package/dist/providers/openai.js.map +11 -0
- package/dist/register-routes.d.ts +2 -0
- package/dist/register-routes.d.ts.map +1 -0
- package/dist/register-routes.js +7612 -0
- package/dist/register-routes.js.map +105 -0
- package/dist/routes/cloud-billing-routes.d.ts +9 -0
- package/dist/routes/cloud-billing-routes.d.ts.map +1 -0
- package/dist/routes/cloud-billing-routes.js +807 -0
- package/dist/routes/cloud-billing-routes.js.map +14 -0
- package/dist/routes/cloud-compat-routes.d.ts +10 -0
- package/dist/routes/cloud-compat-routes.d.ts.map +1 -0
- package/dist/routes/cloud-compat-routes.js +538 -0
- package/dist/routes/cloud-compat-routes.js.map +14 -0
- package/dist/routes/cloud-features-routes.d.ts +9 -0
- package/dist/routes/cloud-features-routes.d.ts.map +1 -0
- package/dist/routes/cloud-features-routes.js +124 -0
- package/dist/routes/cloud-features-routes.js.map +11 -0
- package/dist/routes/cloud-provisioning.d.ts +14 -0
- package/dist/routes/cloud-provisioning.d.ts.map +1 -0
- package/dist/routes/cloud-provisioning.js +37 -0
- package/dist/routes/cloud-provisioning.js.map +10 -0
- package/dist/routes/cloud-relay-routes.d.ts +22 -0
- package/dist/routes/cloud-relay-routes.d.ts.map +1 -0
- package/dist/routes/cloud-relay-routes.js +60 -0
- package/dist/routes/cloud-relay-routes.js.map +10 -0
- package/dist/routes/cloud-routes-autonomous.d.ts +83 -0
- package/dist/routes/cloud-routes-autonomous.d.ts.map +1 -0
- package/dist/routes/cloud-routes-autonomous.js +6134 -0
- package/dist/routes/cloud-routes-autonomous.js.map +97 -0
- package/dist/routes/cloud-routes.d.ts +35 -0
- package/dist/routes/cloud-routes.d.ts.map +1 -0
- package/dist/routes/cloud-routes.js +6888 -0
- package/dist/routes/cloud-routes.js.map +100 -0
- package/dist/routes/cloud-status-routes-autonomous.d.ts +15 -0
- package/dist/routes/cloud-status-routes-autonomous.d.ts.map +1 -0
- package/dist/routes/cloud-status-routes-autonomous.js +396 -0
- package/dist/routes/cloud-status-routes-autonomous.js.map +13 -0
- package/dist/routes/cloud-status-routes.d.ts +4 -0
- package/dist/routes/cloud-status-routes.d.ts.map +1 -0
- package/dist/routes/cloud-status-routes.js +771 -0
- package/dist/routes/cloud-status-routes.js.map +15 -0
- package/dist/services/cloud-auth.d.ts +140 -5
- package/dist/services/cloud-auth.d.ts.map +1 -1
- package/dist/services/cloud-auth.js +363 -0
- package/dist/services/cloud-auth.js.map +12 -0
- package/dist/services/cloud-backup.d.ts.map +1 -1
- package/dist/services/cloud-backup.js +176 -0
- package/dist/services/cloud-backup.js.map +11 -0
- package/dist/services/cloud-bootstrap.d.ts +38 -0
- package/dist/services/cloud-bootstrap.d.ts.map +1 -0
- package/dist/services/cloud-bootstrap.js +84 -0
- package/dist/services/cloud-bootstrap.js.map +10 -0
- package/dist/services/cloud-bridge.d.ts +1 -1
- package/dist/services/cloud-bridge.d.ts.map +1 -1
- package/dist/services/cloud-bridge.js +308 -0
- package/dist/services/cloud-bridge.js.map +11 -0
- package/dist/services/cloud-container.d.ts.map +1 -1
- package/dist/services/cloud-container.js +241 -0
- package/dist/services/cloud-container.js.map +11 -0
- package/dist/services/cloud-credential-provider.d.ts +55 -0
- package/dist/services/cloud-credential-provider.d.ts.map +1 -0
- package/dist/services/cloud-credential-provider.js +190 -0
- package/dist/services/cloud-credential-provider.js.map +11 -0
- package/dist/services/cloud-managed-gateway-relay.d.ts +38 -0
- package/dist/services/cloud-managed-gateway-relay.d.ts.map +1 -0
- package/dist/services/cloud-managed-gateway-relay.js +479 -0
- package/dist/services/cloud-managed-gateway-relay.js.map +10 -0
- package/dist/services/cloud-model-registry.d.ts.map +1 -1
- package/dist/services/cloud-model-registry.js +175 -0
- package/dist/services/cloud-model-registry.js.map +10 -0
- package/dist/services/index.d.ts +3 -1
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +29 -0
- package/dist/services/index.js.map +9 -0
- package/dist/types/cloud.d.ts +41 -19
- package/dist/types/cloud.d.ts.map +1 -1
- package/dist/types/cloud.js +52 -0
- package/dist/types/cloud.js.map +10 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +24 -0
- package/dist/types/index.js.map +9 -0
- package/dist/utils/cloud-api.d.ts +2 -27
- package/dist/utils/cloud-api.d.ts.map +1 -1
- package/dist/utils/cloud-api.js +33 -0
- package/dist/utils/cloud-api.js.map +10 -0
- package/dist/utils/cloud-sdk/client.d.ts +133 -0
- package/dist/utils/cloud-sdk/client.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/client.js +3561 -0
- package/dist/utils/cloud-sdk/client.js.map +13 -0
- package/dist/utils/cloud-sdk/http.d.ts +37 -0
- package/dist/utils/cloud-sdk/http.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/http.js +237 -0
- package/dist/utils/cloud-sdk/http.js.map +11 -0
- package/dist/utils/cloud-sdk/index.d.ts +6 -0
- package/dist/utils/cloud-sdk/index.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/index.js +29 -0
- package/dist/utils/cloud-sdk/index.js.map +9 -0
- package/dist/utils/cloud-sdk/public-routes.d.ts +5377 -0
- package/dist/utils/cloud-sdk/public-routes.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/public-routes.js +2950 -0
- package/dist/utils/cloud-sdk/public-routes.js.map +10 -0
- package/dist/utils/cloud-sdk/types.cloud-api.d.ts +101 -0
- package/dist/utils/cloud-sdk/types.cloud-api.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/types.cloud-api.js +2 -0
- package/dist/utils/cloud-sdk/types.cloud-api.js.map +9 -0
- package/dist/utils/cloud-sdk/types.d.ts +655 -0
- package/dist/utils/cloud-sdk/types.d.ts.map +1 -0
- package/dist/utils/cloud-sdk/types.js +29 -0
- package/dist/utils/cloud-sdk/types.js.map +10 -0
- package/dist/utils/config.d.ts +7 -3
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/config.js +137 -0
- package/dist/utils/config.js.map +10 -0
- package/dist/utils/events.d.ts.map +1 -1
- package/dist/utils/events.js +43 -0
- package/dist/utils/events.js.map +10 -0
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/utils/helpers.js +103 -0
- package/dist/utils/helpers.js.map +10 -0
- package/dist/utils/responses-output.d.ts +13 -0
- package/dist/utils/responses-output.d.ts.map +1 -0
- package/dist/utils/responses-output.js +102 -0
- package/dist/utils/responses-output.js.map +10 -0
- package/dist/utils/sdk-client.d.ts +5 -0
- package/dist/utils/sdk-client.d.ts.map +1 -0
- package/dist/utils/sdk-client.js +144 -0
- package/dist/utils/sdk-client.js.map +11 -0
- package/package.json +108 -19
- package/src/cloud/auth.ts +175 -0
- package/src/cloud/backup.ts +46 -0
- package/src/cloud/base-url.ts +62 -0
- package/src/cloud/bridge-client.ts +602 -0
- package/src/cloud/cloud-api-key.ts +80 -0
- package/src/cloud/cloud-manager.ts +163 -0
- package/src/cloud/cloud-proxy.ts +52 -0
- package/src/cloud/cloud-wallet.ts +341 -0
- package/src/cloud/index.ts +28 -0
- package/src/cloud/reconnect.ts +111 -0
- package/src/cloud/validate-url.ts +181 -0
- package/src/cloud-providers/cloud-status.ts +75 -0
- package/src/cloud-providers/container-health.ts +68 -0
- package/src/cloud-providers/credit-balance.ts +70 -0
- package/src/cloud-providers/index.ts +3 -0
- package/src/cloud-providers/model-registry.ts +74 -0
- package/src/index.browser.ts +10 -0
- package/src/index.node.ts +39 -0
- package/src/index.ts +347 -0
- package/src/init.ts +39 -0
- package/src/lib/cloud-connection.ts +663 -0
- package/src/lib/cloud-secrets.ts +58 -0
- package/src/lib/config-env.ts +168 -0
- package/src/lib/config-like.ts +149 -0
- package/src/lib/credential-type-map.ts +130 -0
- package/src/lib/feature-flags.ts +26 -0
- package/src/lib/http.ts +139 -0
- package/src/lib/server-cloud-tts.ts +609 -0
- package/src/lib/state-paths.ts +28 -0
- package/src/lib/tts-debug.ts +34 -0
- package/src/models/embeddings.ts +234 -0
- package/src/models/image.ts +219 -0
- package/src/models/index.ts +16 -0
- package/src/models/research.ts +265 -0
- package/src/models/speech.ts +78 -0
- package/src/models/text.ts +899 -0
- package/src/models/tokenization.ts +67 -0
- package/src/models/transcription.ts +97 -0
- package/src/onboarding.ts +396 -0
- package/src/plugin.ts +243 -0
- package/src/providers/openai.ts +16 -0
- package/src/register-routes.ts +6 -0
- package/src/routes/cloud-billing-routes.ts +754 -0
- package/src/routes/cloud-compat-routes.ts +314 -0
- package/src/routes/cloud-features-routes.ts +57 -0
- package/src/routes/cloud-provisioning.ts +37 -0
- package/src/routes/cloud-relay-routes.ts +89 -0
- package/src/routes/cloud-routes-autonomous.ts +996 -0
- package/src/routes/cloud-routes.ts +576 -0
- package/src/routes/cloud-status-routes-autonomous.ts +234 -0
- package/src/routes/cloud-status-routes.ts +73 -0
- package/src/services/cloud-auth.ts +567 -0
- package/src/services/cloud-backup.ts +208 -0
- package/src/services/cloud-bootstrap.ts +108 -0
- package/src/services/cloud-bridge.ts +386 -0
- package/src/services/cloud-container.ts +297 -0
- package/src/services/cloud-credential-provider.ts +210 -0
- package/src/services/cloud-managed-gateway-relay.ts +663 -0
- package/src/services/cloud-model-registry.ts +202 -0
- package/src/services/index.ts +17 -0
- package/{types → src/types}/cloud.ts +52 -29
- package/{types → src/types}/index.ts +6 -0
- package/src/utils/cloud-api.ts +10 -0
- package/src/utils/cloud-sdk/client.ts +735 -0
- package/src/utils/cloud-sdk/http.ts +291 -0
- package/src/utils/cloud-sdk/index.ts +23 -0
- package/src/utils/cloud-sdk/public-routes.ts +5070 -0
- package/src/utils/cloud-sdk/types.cloud-api.ts +120 -0
- package/src/utils/cloud-sdk/types.ts +762 -0
- package/src/utils/config.ts +174 -0
- package/src/utils/events.ts +37 -0
- package/src/utils/helpers.ts +107 -0
- package/src/utils/responses-output.ts +115 -0
- package/src/utils/sdk-client.ts +37 -0
- package/dist/actions/check-credits.d.ts +0 -6
- package/dist/actions/check-credits.d.ts.map +0 -1
- package/dist/actions/freeze-agent.d.ts +0 -9
- package/dist/actions/freeze-agent.d.ts.map +0 -1
- package/dist/actions/index.d.ts +0 -5
- package/dist/actions/index.d.ts.map +0 -1
- package/dist/actions/provision-agent.d.ts +0 -8
- package/dist/actions/provision-agent.d.ts.map +0 -1
- package/dist/actions/resume-agent.d.ts +0 -9
- package/dist/actions/resume-agent.d.ts.map +0 -1
- package/dist/build.d.ts +0 -3
- package/dist/build.d.ts.map +0 -1
- package/dist/generated/specs/specs.d.ts +0 -55
- package/dist/generated/specs/specs.d.ts.map +0 -1
- package/dist/models/object.d.ts +0 -4
- package/dist/models/object.d.ts.map +0 -1
- package/dist/utils/forwarded-settings.d.ts +0 -8
- package/dist/utils/forwarded-settings.d.ts.map +0 -1
|
@@ -0,0 +1,1386 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
17
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
18
|
+
|
|
19
|
+
// src/utils/config.ts
|
|
20
|
+
import { logger } from "@elizaos/core";
|
|
21
|
+
import { DEFAULT_ELIZA_CLOUD_TEXT_MODEL } from "@elizaos/core";
|
|
22
|
+
var DEFAULT_ELIZA_CLOUD_LARGE_MODEL = "deepseek/deepseek-v4-pro";
|
|
23
|
+
function getEnvValue(key) {
|
|
24
|
+
if (typeof process === "undefined") {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const value = process.env[key];
|
|
28
|
+
return value === undefined ? undefined : String(value);
|
|
29
|
+
}
|
|
30
|
+
function getSetting(runtime, key, defaultValue) {
|
|
31
|
+
const value = runtime.getSetting(key);
|
|
32
|
+
if (value !== undefined && value !== null) {
|
|
33
|
+
return String(value);
|
|
34
|
+
}
|
|
35
|
+
return getEnvValue(key) ?? defaultValue;
|
|
36
|
+
}
|
|
37
|
+
function isBrowser() {
|
|
38
|
+
return typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
|
|
39
|
+
}
|
|
40
|
+
function isProxyMode(runtime) {
|
|
41
|
+
return isBrowser() && !!getSetting(runtime, "ELIZAOS_CLOUD_BROWSER_BASE_URL");
|
|
42
|
+
}
|
|
43
|
+
function getBaseURL(runtime) {
|
|
44
|
+
const browserURL = getSetting(runtime, "ELIZAOS_CLOUD_BROWSER_BASE_URL");
|
|
45
|
+
const baseURL = isBrowser() && browserURL ? browserURL : getSetting(runtime, "ELIZAOS_CLOUD_BASE_URL", "https://www.elizacloud.ai/api/v1");
|
|
46
|
+
return baseURL;
|
|
47
|
+
}
|
|
48
|
+
function getEmbeddingBaseURL(runtime) {
|
|
49
|
+
const embeddingURL = isBrowser() ? getSetting(runtime, "ELIZAOS_CLOUD_BROWSER_EMBEDDING_URL") || getSetting(runtime, "ELIZAOS_CLOUD_BROWSER_BASE_URL") : getSetting(runtime, "ELIZAOS_CLOUD_EMBEDDING_URL");
|
|
50
|
+
if (embeddingURL) {
|
|
51
|
+
logger.debug(`[ELIZAOS_CLOUD] Using specific embedding base URL: ${embeddingURL}`);
|
|
52
|
+
return embeddingURL;
|
|
53
|
+
}
|
|
54
|
+
logger.debug("[ELIZAOS_CLOUD] Falling back to general base URL for embeddings.");
|
|
55
|
+
return getBaseURL(runtime);
|
|
56
|
+
}
|
|
57
|
+
function getApiKey(runtime) {
|
|
58
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_API_KEY");
|
|
59
|
+
}
|
|
60
|
+
function getEmbeddingApiKey(runtime) {
|
|
61
|
+
const embeddingApiKey = getSetting(runtime, "ELIZAOS_CLOUD_EMBEDDING_API_KEY");
|
|
62
|
+
if (embeddingApiKey) {
|
|
63
|
+
logger.debug("[ELIZAOS_CLOUD] Using specific embedding API key (present)");
|
|
64
|
+
return embeddingApiKey;
|
|
65
|
+
}
|
|
66
|
+
logger.debug("[ELIZAOS_CLOUD] Falling back to general API key for embeddings.");
|
|
67
|
+
return getApiKey(runtime);
|
|
68
|
+
}
|
|
69
|
+
function getSmallModel(runtime) {
|
|
70
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", DEFAULT_ELIZA_CLOUD_TEXT_MODEL);
|
|
71
|
+
}
|
|
72
|
+
function getNanoModel(runtime) {
|
|
73
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_NANO_MODEL") ?? getSetting(runtime, "NANO_MODEL") ?? getSmallModel(runtime);
|
|
74
|
+
}
|
|
75
|
+
function getMediumModel(runtime) {
|
|
76
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_MEDIUM_MODEL") ?? getSetting(runtime, "MEDIUM_MODEL") ?? getSmallModel(runtime);
|
|
77
|
+
}
|
|
78
|
+
function getLargeModel(runtime) {
|
|
79
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", DEFAULT_ELIZA_CLOUD_LARGE_MODEL);
|
|
80
|
+
}
|
|
81
|
+
function getMegaModel(runtime) {
|
|
82
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_MEGA_MODEL") ?? getSetting(runtime, "MEGA_MODEL") ?? getLargeModel(runtime);
|
|
83
|
+
}
|
|
84
|
+
function getResponseHandlerModel(runtime) {
|
|
85
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "ELIZAOS_CLOUD_SHOULD_RESPOND_MODEL") ?? getSetting(runtime, "RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "SHOULD_RESPOND_MODEL") ?? getSmallModel(runtime);
|
|
86
|
+
}
|
|
87
|
+
function getActionPlannerModel(runtime) {
|
|
88
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_ACTION_PLANNER_MODEL") ?? getSetting(runtime, "ELIZAOS_CLOUD_PLANNER_MODEL") ?? getSetting(runtime, "ACTION_PLANNER_MODEL") ?? getSetting(runtime, "PLANNER_MODEL") ?? getLargeModel(runtime);
|
|
89
|
+
}
|
|
90
|
+
function getResponseModel(runtime) {
|
|
91
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_RESPONSE_MODEL") ?? getSetting(runtime, "RESPONSE_MODEL") ?? getLargeModel(runtime);
|
|
92
|
+
}
|
|
93
|
+
function getImageDescriptionModel(runtime) {
|
|
94
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_IMAGE_DESCRIPTION_MODEL", "gpt-5.4-mini");
|
|
95
|
+
}
|
|
96
|
+
function getImageGenerationModel(runtime) {
|
|
97
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_IMAGE_GENERATION_MODEL", "google/gemini-2.5-flash-image") ?? "google/gemini-2.5-flash-image";
|
|
98
|
+
}
|
|
99
|
+
function getResearchModel(runtime) {
|
|
100
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_RESEARCH_MODEL") ?? getSetting(runtime, "RESEARCH_MODEL", "o3-deep-research");
|
|
101
|
+
}
|
|
102
|
+
function getTTSModel(runtime) {
|
|
103
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_TTS_MODEL", "gpt-5-mini-tts");
|
|
104
|
+
}
|
|
105
|
+
function getTranscriptionModel(runtime) {
|
|
106
|
+
return getSetting(runtime, "ELIZAOS_CLOUD_TRANSCRIPTION_MODEL", "gpt-5-mini-transcribe");
|
|
107
|
+
}
|
|
108
|
+
function getExperimentalTelemetry(runtime) {
|
|
109
|
+
const setting = getSetting(runtime, "ELIZAOS_CLOUD_EXPERIMENTAL_TELEMETRY", "false");
|
|
110
|
+
return String(setting).toLowerCase() === "true";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/utils/sdk-client.ts
|
|
114
|
+
import { CloudApiClient, ElizaCloudClient } from "@elizaos/cloud-sdk";
|
|
115
|
+
function trimTrailingSlash(value) {
|
|
116
|
+
return value.replace(/\/+$/, "");
|
|
117
|
+
}
|
|
118
|
+
function apiBaseToSiteBaseUrl(apiBaseUrl) {
|
|
119
|
+
const trimmed = trimTrailingSlash(apiBaseUrl);
|
|
120
|
+
return trimmed.endsWith("/api/v1") ? trimmed.slice(0, -"/api/v1".length) : trimmed;
|
|
121
|
+
}
|
|
122
|
+
function apiKeyForRuntime(runtime, embedding = false) {
|
|
123
|
+
if (isBrowser())
|
|
124
|
+
return;
|
|
125
|
+
return embedding ? getEmbeddingApiKey(runtime) : getApiKey(runtime);
|
|
126
|
+
}
|
|
127
|
+
function createCloudApiClient(runtime, embedding = false) {
|
|
128
|
+
const baseUrl = embedding ? getEmbeddingBaseURL(runtime) : getBaseURL(runtime);
|
|
129
|
+
return new CloudApiClient(trimTrailingSlash(baseUrl), apiKeyForRuntime(runtime, embedding));
|
|
130
|
+
}
|
|
131
|
+
function createElizaCloudClient(runtime) {
|
|
132
|
+
const apiBaseUrl = trimTrailingSlash(getBaseURL(runtime));
|
|
133
|
+
return new ElizaCloudClient({
|
|
134
|
+
apiBaseUrl,
|
|
135
|
+
baseUrl: apiBaseToSiteBaseUrl(apiBaseUrl),
|
|
136
|
+
apiKey: apiKeyForRuntime(runtime)
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/utils/events.ts
|
|
141
|
+
import {
|
|
142
|
+
EventType
|
|
143
|
+
} from "@elizaos/core";
|
|
144
|
+
function emitModelUsageEvent(runtime, type, _prompt, usage) {
|
|
145
|
+
const inputTokens = Number(usage.inputTokens || 0);
|
|
146
|
+
const outputTokens = Number(usage.outputTokens || 0);
|
|
147
|
+
const totalTokens = Number(usage.totalTokens != null ? usage.totalTokens : inputTokens + outputTokens);
|
|
148
|
+
const payload = {
|
|
149
|
+
runtime,
|
|
150
|
+
source: "elizacloud",
|
|
151
|
+
type,
|
|
152
|
+
tokens: {
|
|
153
|
+
prompt: inputTokens,
|
|
154
|
+
completion: outputTokens,
|
|
155
|
+
total: totalTokens
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
runtime.emitEvent(EventType.MODEL_USED, payload);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/models/embeddings.ts
|
|
162
|
+
import { logger as logger2, ModelType, VECTOR_DIMS } from "@elizaos/core";
|
|
163
|
+
var MAX_BATCH_SIZE = 100;
|
|
164
|
+
function extractRateLimitInfo(response) {
|
|
165
|
+
return {
|
|
166
|
+
remainingRequests: parseInt(response.headers.get("x-ratelimit-remaining-requests") || "", 10) || undefined,
|
|
167
|
+
remainingTokens: parseInt(response.headers.get("x-ratelimit-remaining-tokens") || "", 10) || undefined,
|
|
168
|
+
limitRequests: parseInt(response.headers.get("x-ratelimit-limit-requests") || "", 10) || undefined,
|
|
169
|
+
limitTokens: parseInt(response.headers.get("x-ratelimit-limit-tokens") || "", 10) || undefined,
|
|
170
|
+
resetRequests: response.headers.get("x-ratelimit-reset-requests") || undefined,
|
|
171
|
+
resetTokens: response.headers.get("x-ratelimit-reset-tokens") || undefined,
|
|
172
|
+
retryAfter: parseInt(response.headers.get("retry-after") || "", 10) || undefined
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function getEmbeddingConfig(runtime) {
|
|
176
|
+
const embeddingModelName = getSetting(runtime, "ELIZAOS_CLOUD_EMBEDDING_MODEL", "text-embedding-3-small");
|
|
177
|
+
const embeddingDimension = Number.parseInt(getSetting(runtime, "ELIZAOS_CLOUD_EMBEDDING_DIMENSIONS", "1536") || "1536", 10);
|
|
178
|
+
if (!Object.values(VECTOR_DIMS).includes(embeddingDimension)) {
|
|
179
|
+
const errorMsg = `Invalid embedding dimension: ${embeddingDimension}. Must be one of: ${Object.values(VECTOR_DIMS).join(", ")}`;
|
|
180
|
+
logger2.error(errorMsg);
|
|
181
|
+
throw new Error(errorMsg);
|
|
182
|
+
}
|
|
183
|
+
return { embeddingModelName, embeddingDimension };
|
|
184
|
+
}
|
|
185
|
+
function createErrorVector(dimension, marker) {
|
|
186
|
+
const vector = Array(dimension).fill(0);
|
|
187
|
+
vector[0] = marker;
|
|
188
|
+
return vector;
|
|
189
|
+
}
|
|
190
|
+
async function handleTextEmbedding(runtime, params) {
|
|
191
|
+
const { embeddingDimension } = getEmbeddingConfig(runtime);
|
|
192
|
+
if (params === null) {
|
|
193
|
+
logger2.debug("Creating test embedding for initialization");
|
|
194
|
+
return createErrorVector(embeddingDimension, 0.1);
|
|
195
|
+
}
|
|
196
|
+
let text;
|
|
197
|
+
if (typeof params === "string") {
|
|
198
|
+
text = params;
|
|
199
|
+
} else if (typeof params === "object" && params.text) {
|
|
200
|
+
text = params.text;
|
|
201
|
+
} else {
|
|
202
|
+
logger2.warn("Invalid input format for embedding");
|
|
203
|
+
return createErrorVector(embeddingDimension, 0.2);
|
|
204
|
+
}
|
|
205
|
+
if (!text.trim()) {
|
|
206
|
+
logger2.warn("Empty text for embedding");
|
|
207
|
+
return createErrorVector(embeddingDimension, 0.3);
|
|
208
|
+
}
|
|
209
|
+
const results = await handleBatchTextEmbedding(runtime, [text]);
|
|
210
|
+
return results[0];
|
|
211
|
+
}
|
|
212
|
+
async function handleBatchTextEmbedding(runtime, texts) {
|
|
213
|
+
const { embeddingModelName, embeddingDimension } = getEmbeddingConfig(runtime);
|
|
214
|
+
const client = createCloudApiClient(runtime, true);
|
|
215
|
+
if (!texts || texts.length === 0) {
|
|
216
|
+
logger2.warn("[BatchEmbeddings] Empty texts array");
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
const validTexts = [];
|
|
220
|
+
const results = new Array(texts.length);
|
|
221
|
+
for (let i = 0;i < texts.length; i++) {
|
|
222
|
+
const text = texts[i]?.trim();
|
|
223
|
+
if (text) {
|
|
224
|
+
validTexts.push({ text, originalIndex: i });
|
|
225
|
+
} else {
|
|
226
|
+
results[i] = createErrorVector(embeddingDimension, 0.3);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (validTexts.length === 0) {
|
|
230
|
+
logger2.warn("[BatchEmbeddings] All texts were empty");
|
|
231
|
+
return results;
|
|
232
|
+
}
|
|
233
|
+
for (let batchStart = 0;batchStart < validTexts.length; batchStart += MAX_BATCH_SIZE) {
|
|
234
|
+
const batchEnd = Math.min(batchStart + MAX_BATCH_SIZE, validTexts.length);
|
|
235
|
+
const batch = validTexts.slice(batchStart, batchEnd);
|
|
236
|
+
const batchTexts = batch.map((b) => b.text);
|
|
237
|
+
logger2.info(`[BatchEmbeddings] Processing batch ${Math.floor(batchStart / MAX_BATCH_SIZE) + 1}/${Math.ceil(validTexts.length / MAX_BATCH_SIZE)}: ${batch.length} texts`);
|
|
238
|
+
try {
|
|
239
|
+
const response = await client.requestRaw("POST", "/embeddings", {
|
|
240
|
+
json: {
|
|
241
|
+
model: embeddingModelName,
|
|
242
|
+
input: batchTexts
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
const rateLimitInfo = extractRateLimitInfo(response);
|
|
246
|
+
if (rateLimitInfo.remainingRequests !== undefined && rateLimitInfo.remainingRequests < 50) {
|
|
247
|
+
logger2.warn(`[BatchEmbeddings] Rate limit: ${rateLimitInfo.remainingRequests}/${rateLimitInfo.limitRequests} requests remaining`);
|
|
248
|
+
}
|
|
249
|
+
if (response.status === 429) {
|
|
250
|
+
const retryAfter = rateLimitInfo.retryAfter || 30;
|
|
251
|
+
logger2.warn(`[BatchEmbeddings] Rate limited, waiting ${retryAfter}s...`);
|
|
252
|
+
await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
|
|
253
|
+
const retryResponse = await client.requestRaw("POST", "/embeddings", {
|
|
254
|
+
json: {
|
|
255
|
+
model: embeddingModelName,
|
|
256
|
+
input: batchTexts
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
if (!retryResponse.ok) {
|
|
260
|
+
logger2.error(`[BatchEmbeddings] Retry failed: ${retryResponse.status}`);
|
|
261
|
+
for (const item of batch) {
|
|
262
|
+
results[item.originalIndex] = createErrorVector(embeddingDimension, 0.4);
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const retryData = await retryResponse.json();
|
|
267
|
+
if (retryData?.data) {
|
|
268
|
+
for (const item of retryData.data) {
|
|
269
|
+
const originalIndex = batch[item.index].originalIndex;
|
|
270
|
+
results[originalIndex] = item.embedding;
|
|
271
|
+
}
|
|
272
|
+
logger2.info(`[BatchEmbeddings] Retry successful for ${batch.length} embeddings`);
|
|
273
|
+
}
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (!response.ok) {
|
|
277
|
+
logger2.error(`[BatchEmbeddings] API error: ${response.status} - ${response.statusText}`);
|
|
278
|
+
for (const item of batch) {
|
|
279
|
+
results[item.originalIndex] = createErrorVector(embeddingDimension, 0.4);
|
|
280
|
+
}
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const data = await response.json();
|
|
284
|
+
if (!data?.data || !Array.isArray(data.data)) {
|
|
285
|
+
logger2.error("[BatchEmbeddings] API returned invalid structure");
|
|
286
|
+
for (const item of batch) {
|
|
287
|
+
results[item.originalIndex] = createErrorVector(embeddingDimension, 0.5);
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
for (const item of data.data) {
|
|
292
|
+
const originalIndex = batch[item.index].originalIndex;
|
|
293
|
+
results[originalIndex] = item.embedding;
|
|
294
|
+
}
|
|
295
|
+
if (data.usage) {
|
|
296
|
+
const usage = {
|
|
297
|
+
inputTokens: data.usage.prompt_tokens,
|
|
298
|
+
outputTokens: 0,
|
|
299
|
+
totalTokens: data.usage.total_tokens
|
|
300
|
+
};
|
|
301
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_EMBEDDING, `batch:${batch.length}`, usage);
|
|
302
|
+
}
|
|
303
|
+
logger2.debug(`[BatchEmbeddings] Got ${batch.length} embeddings (${embeddingDimension}d), remaining: ${rateLimitInfo.remainingRequests ?? "unknown"}`);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
306
|
+
logger2.error(`[BatchEmbeddings] Error: ${message}`);
|
|
307
|
+
for (const item of batch) {
|
|
308
|
+
results[item.originalIndex] = createErrorVector(embeddingDimension, 0.6);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return results;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/utils/helpers.ts
|
|
316
|
+
import { logger as logger3 } from "@elizaos/core";
|
|
317
|
+
import { JSONParseError } from "ai";
|
|
318
|
+
function getJsonRepairFunction() {
|
|
319
|
+
return async ({ text, error }) => {
|
|
320
|
+
try {
|
|
321
|
+
if (error instanceof JSONParseError) {
|
|
322
|
+
const cleanedText = text.replace(/```json\n|\n```|```/g, "");
|
|
323
|
+
JSON.parse(cleanedText);
|
|
324
|
+
return cleanedText;
|
|
325
|
+
}
|
|
326
|
+
return null;
|
|
327
|
+
} catch (jsonError) {
|
|
328
|
+
const message = jsonError instanceof Error ? jsonError.message : String(jsonError);
|
|
329
|
+
logger3.warn(`Failed to repair JSON text: ${message}`);
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function detectAudioMimeType(buffer) {
|
|
335
|
+
if (buffer.length < 12) {
|
|
336
|
+
return "application/octet-stream";
|
|
337
|
+
}
|
|
338
|
+
if (buffer[0] === 82 && buffer[1] === 73 && buffer[2] === 70 && buffer[3] === 70 && buffer[8] === 87 && buffer[9] === 65 && buffer[10] === 86 && buffer[11] === 69) {
|
|
339
|
+
return "audio/wav";
|
|
340
|
+
}
|
|
341
|
+
if (buffer[0] === 73 && buffer[1] === 68 && buffer[2] === 51 || buffer[0] === 255 && (buffer[1] & 224) === 224) {
|
|
342
|
+
return "audio/mpeg";
|
|
343
|
+
}
|
|
344
|
+
if (buffer[0] === 79 && buffer[1] === 103 && buffer[2] === 103 && buffer[3] === 83) {
|
|
345
|
+
return "audio/ogg";
|
|
346
|
+
}
|
|
347
|
+
if (buffer[0] === 102 && buffer[1] === 76 && buffer[2] === 97 && buffer[3] === 67) {
|
|
348
|
+
return "audio/flac";
|
|
349
|
+
}
|
|
350
|
+
if (buffer[4] === 102 && buffer[5] === 116 && buffer[6] === 121 && buffer[7] === 112) {
|
|
351
|
+
return "audio/mp4";
|
|
352
|
+
}
|
|
353
|
+
if (buffer[0] === 26 && buffer[1] === 69 && buffer[2] === 223 && buffer[3] === 163) {
|
|
354
|
+
return "audio/webm";
|
|
355
|
+
}
|
|
356
|
+
logger3.warn("Could not detect audio format from buffer, using generic binary type");
|
|
357
|
+
return "application/octet-stream";
|
|
358
|
+
}
|
|
359
|
+
async function webStreamToNodeStream(webStream) {
|
|
360
|
+
try {
|
|
361
|
+
const { Readable } = await import("node:stream");
|
|
362
|
+
const reader = webStream.getReader();
|
|
363
|
+
return new Readable({
|
|
364
|
+
async read() {
|
|
365
|
+
try {
|
|
366
|
+
const { done, value } = await reader.read();
|
|
367
|
+
if (done) {
|
|
368
|
+
this.push(null);
|
|
369
|
+
} else {
|
|
370
|
+
this.push(value);
|
|
371
|
+
}
|
|
372
|
+
} catch (error) {
|
|
373
|
+
this.destroy(error);
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
destroy(error, callback) {
|
|
377
|
+
reader.cancel().finally(() => callback(error));
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
} catch (error) {
|
|
381
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
382
|
+
logger3.error(`Failed to load node:stream module: ${message}`);
|
|
383
|
+
throw new Error(`Cannot convert stream: node:stream module unavailable. This feature requires a Node.js environment.`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function parseImageDescriptionResponse(responseText) {
|
|
387
|
+
const titleMatch = responseText.match(/title[:\s]+(.+?)(?:\n|$)/i);
|
|
388
|
+
const title = titleMatch?.[1]?.trim() || "Image Analysis";
|
|
389
|
+
const description = responseText.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
390
|
+
return { title, description };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/models/image.ts
|
|
394
|
+
import { logger as logger4, ModelType as ModelType2 } from "@elizaos/core";
|
|
395
|
+
async function handleImageGeneration(runtime, params) {
|
|
396
|
+
const numImages = params.count || 1;
|
|
397
|
+
const size = params.size || "1024x1024";
|
|
398
|
+
const prompt = params.prompt;
|
|
399
|
+
const modelName = getImageGenerationModel(runtime);
|
|
400
|
+
logger4.log(`[ELIZAOS_CLOUD] Using IMAGE model: ${modelName}`);
|
|
401
|
+
const aspectRatioMap = {
|
|
402
|
+
"1024x1024": "1:1",
|
|
403
|
+
"1792x1024": "16:9",
|
|
404
|
+
"1024x1792": "9:16"
|
|
405
|
+
};
|
|
406
|
+
const aspectRatio = aspectRatioMap[size] || "1:1";
|
|
407
|
+
try {
|
|
408
|
+
const requestBody = {
|
|
409
|
+
prompt,
|
|
410
|
+
numImages,
|
|
411
|
+
aspectRatio,
|
|
412
|
+
model: modelName
|
|
413
|
+
};
|
|
414
|
+
const typedData = await createElizaCloudClient(runtime).generateImage(requestBody);
|
|
415
|
+
const result = typedData.images.map((img) => ({
|
|
416
|
+
url: img.url ?? img.image ?? ""
|
|
417
|
+
}));
|
|
418
|
+
return result;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
421
|
+
logger4.error(`[ELIZAOS_CLOUD] Image generation error: ${message}`);
|
|
422
|
+
throw error;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
async function handleImageDescription(runtime, params) {
|
|
426
|
+
const disableSetting = getSetting(runtime, "DISABLE_IMAGE_DESCRIPTION", "");
|
|
427
|
+
const disabled = disableSetting === "true" || disableSetting === "1" || process.env.DISABLE_IMAGE_DESCRIPTION === "true" || process.env.DISABLE_IMAGE_DESCRIPTION === "1";
|
|
428
|
+
if (disabled) {
|
|
429
|
+
logger4.debug("[ELIZAOS_CLOUD] IMAGE_DESCRIPTION skipped — DISABLE_IMAGE_DESCRIPTION is set");
|
|
430
|
+
return {
|
|
431
|
+
title: "Image description disabled",
|
|
432
|
+
description: "Image description is disabled by configuration."
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
let imageUrl;
|
|
436
|
+
let promptText;
|
|
437
|
+
const modelName = getImageDescriptionModel(runtime);
|
|
438
|
+
logger4.log(`[ELIZAOS_CLOUD] Using IMAGE_DESCRIPTION model: ${modelName}`);
|
|
439
|
+
const maxTokens = Number.parseInt(getSetting(runtime, "ELIZAOS_CLOUD_IMAGE_DESCRIPTION_MAX_TOKENS", "8192") || "8192", 10);
|
|
440
|
+
if (typeof params === "string") {
|
|
441
|
+
imageUrl = params;
|
|
442
|
+
promptText = "Please analyze this image and provide a title and detailed description.";
|
|
443
|
+
} else {
|
|
444
|
+
imageUrl = params.imageUrl;
|
|
445
|
+
promptText = params.prompt || "Please analyze this image and provide a title and detailed description.";
|
|
446
|
+
}
|
|
447
|
+
const messages = [
|
|
448
|
+
{
|
|
449
|
+
role: "user",
|
|
450
|
+
content: [
|
|
451
|
+
{ type: "text", text: promptText },
|
|
452
|
+
{ type: "image_url", image_url: { url: imageUrl } }
|
|
453
|
+
]
|
|
454
|
+
}
|
|
455
|
+
];
|
|
456
|
+
const client = createElizaCloudClient(runtime);
|
|
457
|
+
try {
|
|
458
|
+
const requestBody = {
|
|
459
|
+
model: modelName,
|
|
460
|
+
messages,
|
|
461
|
+
max_tokens: maxTokens
|
|
462
|
+
};
|
|
463
|
+
let response = null;
|
|
464
|
+
let attemptedRetry = false;
|
|
465
|
+
for (let attempt = 0;attempt < 2; attempt++) {
|
|
466
|
+
response = await client.routes.postApiV1ChatCompletionsRaw({
|
|
467
|
+
json: requestBody
|
|
468
|
+
});
|
|
469
|
+
if (response.status !== 429 || attemptedRetry)
|
|
470
|
+
break;
|
|
471
|
+
const headerValue = response.headers.get("retry-after");
|
|
472
|
+
const headerRetryAfter = headerValue !== null && Number.isFinite(Number(headerValue)) ? Number(headerValue) : undefined;
|
|
473
|
+
let bodyRetryAfter;
|
|
474
|
+
try {
|
|
475
|
+
const peek = await response.clone().json();
|
|
476
|
+
bodyRetryAfter = typeof peek?.retryAfter === "number" && Number.isFinite(peek.retryAfter) ? peek.retryAfter : undefined;
|
|
477
|
+
} catch {}
|
|
478
|
+
const retryAfter = headerRetryAfter ?? bodyRetryAfter ?? 0;
|
|
479
|
+
if (retryAfter > 0 && retryAfter <= 5) {
|
|
480
|
+
logger4.warn(`[ELIZAOS_CLOUD] Image analysis rate-limited (429), retrying once after ${retryAfter}s...`);
|
|
481
|
+
await new Promise((r) => setTimeout(r, retryAfter * 1000));
|
|
482
|
+
attemptedRetry = true;
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
logger4.warn(`[ELIZAOS_CLOUD] Image analysis rate-limited (429); upstream retryAfter=${retryAfter || "unknown"}s — failing fast`);
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
if (!response?.ok) {
|
|
489
|
+
const status = response?.status ?? 0;
|
|
490
|
+
if (status === 402) {
|
|
491
|
+
throw new Error("Eliza Cloud credits exhausted — top up at https://www.elizacloud.ai/dashboard/settings?tab=billing");
|
|
492
|
+
}
|
|
493
|
+
if (status === 429) {
|
|
494
|
+
throw new Error("Eliza Cloud rate limit exceeded for image description — try again in a minute");
|
|
495
|
+
}
|
|
496
|
+
throw new Error(`ElizaOS Cloud API error: ${status}`);
|
|
497
|
+
}
|
|
498
|
+
const typedResult = await response.json();
|
|
499
|
+
const content = typedResult.choices?.[0]?.message?.content;
|
|
500
|
+
if (typedResult.usage) {
|
|
501
|
+
emitModelUsageEvent(runtime, ModelType2.IMAGE_DESCRIPTION, typeof params === "string" ? params : params.prompt || "", {
|
|
502
|
+
inputTokens: typedResult.usage.prompt_tokens,
|
|
503
|
+
outputTokens: typedResult.usage.completion_tokens,
|
|
504
|
+
totalTokens: typedResult.usage.total_tokens
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
if (!content) {
|
|
508
|
+
return {
|
|
509
|
+
title: "Failed to analyze image",
|
|
510
|
+
description: "No response from API"
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
return parseImageDescriptionResponse(content);
|
|
514
|
+
} catch (error) {
|
|
515
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
516
|
+
logger4.error(`Error analyzing image: ${message}`);
|
|
517
|
+
return {
|
|
518
|
+
title: "Failed to analyze image",
|
|
519
|
+
description: `Error: ${message}`
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// src/models/research.ts
|
|
525
|
+
import { logger as logger5, ModelType as ModelType3 } from "@elizaos/core";
|
|
526
|
+
function normalizeInput(input) {
|
|
527
|
+
if (typeof input !== "string") {
|
|
528
|
+
return input;
|
|
529
|
+
}
|
|
530
|
+
return [
|
|
531
|
+
{
|
|
532
|
+
role: "user",
|
|
533
|
+
content: [
|
|
534
|
+
{
|
|
535
|
+
type: "input_text",
|
|
536
|
+
text: input
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
}
|
|
540
|
+
];
|
|
541
|
+
}
|
|
542
|
+
function buildResearchApiError(status, errorText) {
|
|
543
|
+
try {
|
|
544
|
+
const parsed = JSON.parse(errorText);
|
|
545
|
+
const message = parsed.error?.message;
|
|
546
|
+
const param = parsed.error?.param;
|
|
547
|
+
if (param === "tools.0.type" && message?.includes('expected "function"')) {
|
|
548
|
+
return new Error(`Research API error: ${status} Eliza Cloud /responses rejected deep-research tool types; the provider currently only accepts function tools on this route`);
|
|
549
|
+
}
|
|
550
|
+
} catch {}
|
|
551
|
+
return new Error(`Research API error: ${status} ${errorText}`);
|
|
552
|
+
}
|
|
553
|
+
function parseAnnotations(raw) {
|
|
554
|
+
return raw.filter((a) => a.url !== undefined).map((a) => ({
|
|
555
|
+
url: a.url,
|
|
556
|
+
title: a.title ?? "",
|
|
557
|
+
startIndex: a.start_index ?? 0,
|
|
558
|
+
endIndex: a.end_index ?? 0
|
|
559
|
+
}));
|
|
560
|
+
}
|
|
561
|
+
function parseOutputItems(raw) {
|
|
562
|
+
const items = [];
|
|
563
|
+
for (const item of raw) {
|
|
564
|
+
switch (item.type) {
|
|
565
|
+
case "web_search_call":
|
|
566
|
+
items.push({
|
|
567
|
+
id: item.id ?? "",
|
|
568
|
+
type: "web_search_call",
|
|
569
|
+
status: item.status ?? "completed",
|
|
570
|
+
action: {
|
|
571
|
+
type: item.action?.type ?? "search",
|
|
572
|
+
query: item.action?.query,
|
|
573
|
+
url: item.action?.url
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
break;
|
|
577
|
+
case "file_search_call":
|
|
578
|
+
items.push({
|
|
579
|
+
id: item.id ?? "",
|
|
580
|
+
type: "file_search_call",
|
|
581
|
+
status: item.status ?? "completed",
|
|
582
|
+
query: item.query ?? "",
|
|
583
|
+
results: item.results?.map((r) => ({
|
|
584
|
+
fileId: r.file_id,
|
|
585
|
+
fileName: r.file_name,
|
|
586
|
+
score: r.score
|
|
587
|
+
}))
|
|
588
|
+
});
|
|
589
|
+
break;
|
|
590
|
+
case "code_interpreter_call":
|
|
591
|
+
items.push({
|
|
592
|
+
id: item.id ?? "",
|
|
593
|
+
type: "code_interpreter_call",
|
|
594
|
+
status: item.status ?? "completed",
|
|
595
|
+
code: item.code ?? "",
|
|
596
|
+
output: item.output
|
|
597
|
+
});
|
|
598
|
+
break;
|
|
599
|
+
case "mcp_tool_call":
|
|
600
|
+
items.push({
|
|
601
|
+
id: item.id ?? "",
|
|
602
|
+
type: "mcp_tool_call",
|
|
603
|
+
status: item.status ?? "completed",
|
|
604
|
+
serverLabel: item.server_label ?? "",
|
|
605
|
+
toolName: item.tool_name ?? "",
|
|
606
|
+
arguments: item.arguments ?? {},
|
|
607
|
+
result: item.result
|
|
608
|
+
});
|
|
609
|
+
break;
|
|
610
|
+
case "message": {
|
|
611
|
+
const content = item.content ?? [];
|
|
612
|
+
items.push({
|
|
613
|
+
type: "message",
|
|
614
|
+
content: content.filter((c) => c.type === "output_text").map((c) => ({
|
|
615
|
+
type: "output_text",
|
|
616
|
+
text: c.text ?? "",
|
|
617
|
+
annotations: parseAnnotations(c.annotations ?? [])
|
|
618
|
+
}))
|
|
619
|
+
});
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return items;
|
|
625
|
+
}
|
|
626
|
+
async function handleResearch(runtime, params) {
|
|
627
|
+
const modelName = params.model ?? getResearchModel(runtime);
|
|
628
|
+
logger5.log(`[ELIZAOS_CLOUD] Using RESEARCH model: ${modelName}`);
|
|
629
|
+
const tools = params.tools ?? [{ type: "web_search_preview" }];
|
|
630
|
+
const requestBody = {
|
|
631
|
+
model: modelName,
|
|
632
|
+
input: normalizeInput(params.input),
|
|
633
|
+
tools
|
|
634
|
+
};
|
|
635
|
+
if (params.instructions) {
|
|
636
|
+
requestBody.instructions = params.instructions;
|
|
637
|
+
}
|
|
638
|
+
if (params.background !== undefined) {
|
|
639
|
+
requestBody.background = params.background;
|
|
640
|
+
}
|
|
641
|
+
if (params.maxToolCalls !== undefined) {
|
|
642
|
+
requestBody.max_tool_calls = params.maxToolCalls;
|
|
643
|
+
}
|
|
644
|
+
if (params.reasoningSummary) {
|
|
645
|
+
requestBody.reasoning = { summary: params.reasoningSummary };
|
|
646
|
+
}
|
|
647
|
+
const response = await createCloudApiClient(runtime).requestRaw("POST", "/responses", {
|
|
648
|
+
json: requestBody
|
|
649
|
+
});
|
|
650
|
+
if (!response.ok) {
|
|
651
|
+
const errorText = await response.text();
|
|
652
|
+
throw buildResearchApiError(response.status, errorText);
|
|
653
|
+
}
|
|
654
|
+
const data = await response.json();
|
|
655
|
+
if (data.usage) {
|
|
656
|
+
emitModelUsageEvent(runtime, ModelType3.RESEARCH, params.input, {
|
|
657
|
+
inputTokens: data.usage.input_tokens,
|
|
658
|
+
outputTokens: data.usage.output_tokens,
|
|
659
|
+
totalTokens: data.usage.total_tokens
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
const outputItems = parseOutputItems(data.output);
|
|
663
|
+
let text = "";
|
|
664
|
+
const annotations = [];
|
|
665
|
+
for (const item of outputItems) {
|
|
666
|
+
if (item.type === "message") {
|
|
667
|
+
for (const content of item.content) {
|
|
668
|
+
if (content.type === "output_text") {
|
|
669
|
+
text += content.text;
|
|
670
|
+
annotations.push(...content.annotations);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return {
|
|
676
|
+
id: data.id,
|
|
677
|
+
text,
|
|
678
|
+
annotations,
|
|
679
|
+
outputItems,
|
|
680
|
+
status: data.status
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/models/speech.ts
|
|
685
|
+
import { logger as logger6 } from "@elizaos/core";
|
|
686
|
+
async function fetchTextToSpeech(runtime, options) {
|
|
687
|
+
const defaultModel = getSetting(runtime, "ELIZAOS_CLOUD_TTS_MODEL", "gpt-5-mini-tts");
|
|
688
|
+
const defaultVoice = getSetting(runtime, "ELIZAOS_CLOUD_TTS_VOICE", "nova");
|
|
689
|
+
const model = options.model || defaultModel;
|
|
690
|
+
const voice = options.voice || defaultVoice;
|
|
691
|
+
const format = options.format || "mp3";
|
|
692
|
+
const modelId = model.startsWith("elevenlabs/") ? model.split("/").slice(1).join("/") : model.startsWith("eleven_") ? model : undefined;
|
|
693
|
+
const voiceId = voice && voice !== "nova" ? voice : undefined;
|
|
694
|
+
try {
|
|
695
|
+
const res = await createElizaCloudClient(runtime).routes.postApiV1VoiceTts({
|
|
696
|
+
headers: {
|
|
697
|
+
...format === "mp3" ? { Accept: "audio/mpeg" } : {}
|
|
698
|
+
},
|
|
699
|
+
json: {
|
|
700
|
+
text: options.text,
|
|
701
|
+
...voiceId ? { voiceId } : {},
|
|
702
|
+
...modelId ? { modelId } : {}
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
if (!res.ok) {
|
|
706
|
+
const err = await res.text();
|
|
707
|
+
throw new Error(`ElizaOS Cloud TTS error ${res.status}: ${err}`);
|
|
708
|
+
}
|
|
709
|
+
if (!res.body) {
|
|
710
|
+
throw new Error("ElizaOS Cloud TTS response body is null");
|
|
711
|
+
}
|
|
712
|
+
if (!isBrowser()) {
|
|
713
|
+
return await webStreamToNodeStream(res.body);
|
|
714
|
+
}
|
|
715
|
+
return res.body;
|
|
716
|
+
} catch (err) {
|
|
717
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
718
|
+
throw new Error(`Failed to fetch speech from ElizaOS Cloud TTS: ${message}`);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
async function handleTextToSpeech(runtime, input) {
|
|
722
|
+
const options = typeof input === "string" ? { text: input } : input;
|
|
723
|
+
const resolvedModel = options.model || getSetting(runtime, "ELIZAOS_CLOUD_TTS_MODEL", "gpt-5-mini-tts");
|
|
724
|
+
logger6.log(`[ELIZAOS_CLOUD] Using TEXT_TO_SPEECH model: ${resolvedModel}`);
|
|
725
|
+
try {
|
|
726
|
+
const speechStream = await fetchTextToSpeech(runtime, options);
|
|
727
|
+
return speechStream;
|
|
728
|
+
} catch (error) {
|
|
729
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
730
|
+
logger6.error(`Error in TEXT_TO_SPEECH: ${message}`);
|
|
731
|
+
throw error;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
// src/providers/openai.ts
|
|
735
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
736
|
+
function createOpenAIClient(runtime) {
|
|
737
|
+
const baseURL = getBaseURL(runtime);
|
|
738
|
+
const apiKey = getApiKey(runtime) ?? (isProxyMode(runtime) ? "eliza-proxy" : undefined);
|
|
739
|
+
return createOpenAI({
|
|
740
|
+
apiKey: apiKey ?? "",
|
|
741
|
+
baseURL
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// src/utils/responses-output.ts
|
|
746
|
+
function asRecord(value) {
|
|
747
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
return value;
|
|
751
|
+
}
|
|
752
|
+
function normalizeContentItems(value) {
|
|
753
|
+
if (Array.isArray(value))
|
|
754
|
+
return value;
|
|
755
|
+
if (typeof value === "string")
|
|
756
|
+
return [{ type: "text", text: value }];
|
|
757
|
+
return value && typeof value === "object" ? [value] : [];
|
|
758
|
+
}
|
|
759
|
+
function extractTextFromContentItem(value) {
|
|
760
|
+
if (typeof value === "string") {
|
|
761
|
+
return [value];
|
|
762
|
+
}
|
|
763
|
+
const record = asRecord(value);
|
|
764
|
+
if (!record)
|
|
765
|
+
return [];
|
|
766
|
+
const text = typeof record.text === "string" ? record.text : typeof record.output_text === "string" ? record.output_text : typeof record.content === "string" ? record.content : "";
|
|
767
|
+
const type = typeof record.type === "string" ? record.type : undefined;
|
|
768
|
+
if (text && (!type || type === "output_text" || type === "text")) {
|
|
769
|
+
return [text];
|
|
770
|
+
}
|
|
771
|
+
return [];
|
|
772
|
+
}
|
|
773
|
+
function extractTextFromOutputItem(value) {
|
|
774
|
+
const record = asRecord(value);
|
|
775
|
+
if (!record)
|
|
776
|
+
return [];
|
|
777
|
+
const directContent = normalizeContentItems(record.content);
|
|
778
|
+
if (directContent.length > 0) {
|
|
779
|
+
return directContent.flatMap(extractTextFromContentItem);
|
|
780
|
+
}
|
|
781
|
+
const nestedMessage = asRecord(record.message);
|
|
782
|
+
if (nestedMessage) {
|
|
783
|
+
return normalizeContentItems(nestedMessage.content).flatMap(extractTextFromContentItem);
|
|
784
|
+
}
|
|
785
|
+
const type = typeof record.type === "string" ? record.type : undefined;
|
|
786
|
+
const text = typeof record.text === "string" ? record.text : typeof record.output_text === "string" ? record.output_text : "";
|
|
787
|
+
if (text && (type === "output_text" || type === "text")) {
|
|
788
|
+
return [text];
|
|
789
|
+
}
|
|
790
|
+
return [];
|
|
791
|
+
}
|
|
792
|
+
function extractTextFromChoice(value) {
|
|
793
|
+
const record = asRecord(value);
|
|
794
|
+
if (!record)
|
|
795
|
+
return [];
|
|
796
|
+
if (typeof record.text === "string" && record.text) {
|
|
797
|
+
return [record.text];
|
|
798
|
+
}
|
|
799
|
+
const message = asRecord(record.message);
|
|
800
|
+
if (!message) {
|
|
801
|
+
return [];
|
|
802
|
+
}
|
|
803
|
+
return normalizeContentItems(message.content).flatMap(extractTextFromContentItem);
|
|
804
|
+
}
|
|
805
|
+
function extractResponsesOutputText(data) {
|
|
806
|
+
const record = asRecord(data);
|
|
807
|
+
if (!record)
|
|
808
|
+
return "";
|
|
809
|
+
if (typeof record.output_text === "string" && record.output_text.trim()) {
|
|
810
|
+
return record.output_text;
|
|
811
|
+
}
|
|
812
|
+
if (Array.isArray(record.output)) {
|
|
813
|
+
const fromOutput = record.output.flatMap(extractTextFromOutputItem).join("");
|
|
814
|
+
if (fromOutput.trim())
|
|
815
|
+
return fromOutput;
|
|
816
|
+
}
|
|
817
|
+
if (Array.isArray(record.choices)) {
|
|
818
|
+
const fromChoices = record.choices.flatMap(extractTextFromChoice).join("");
|
|
819
|
+
if (fromChoices.trim())
|
|
820
|
+
return fromChoices;
|
|
821
|
+
}
|
|
822
|
+
return "";
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/models/text.ts
|
|
826
|
+
import {
|
|
827
|
+
buildCanonicalSystemPrompt,
|
|
828
|
+
logger as logger7,
|
|
829
|
+
ModelType as ModelType4,
|
|
830
|
+
renderChatMessagesForPrompt,
|
|
831
|
+
resolveEffectiveSystemPrompt
|
|
832
|
+
} from "@elizaos/core";
|
|
833
|
+
var TEXT_NANO_MODEL_TYPE = ModelType4.TEXT_NANO ?? "TEXT_NANO";
|
|
834
|
+
var TEXT_MEDIUM_MODEL_TYPE = ModelType4.TEXT_MEDIUM ?? "TEXT_MEDIUM";
|
|
835
|
+
var TEXT_SMALL_MODEL_TYPE = ModelType4.TEXT_SMALL;
|
|
836
|
+
var TEXT_LARGE_MODEL_TYPE = ModelType4.TEXT_LARGE;
|
|
837
|
+
var TEXT_MEGA_MODEL_TYPE = ModelType4.TEXT_MEGA ?? "TEXT_MEGA";
|
|
838
|
+
var RESPONSE_HANDLER_MODEL_TYPE = ModelType4.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
|
|
839
|
+
var ACTION_PLANNER_MODEL_TYPE = ModelType4.ACTION_PLANNER ?? "ACTION_PLANNER";
|
|
840
|
+
var REASONING_MODEL_PATTERNS = [
|
|
841
|
+
"o1",
|
|
842
|
+
"o3",
|
|
843
|
+
"o4",
|
|
844
|
+
"deepseek-r1",
|
|
845
|
+
"deepseek-reasoner",
|
|
846
|
+
"claude-opus-4.7",
|
|
847
|
+
"claude-opus-4-7",
|
|
848
|
+
"gpt-5"
|
|
849
|
+
];
|
|
850
|
+
var RESPONSES_ROUTED_PREFIXES = ["openai/", "anthropic/"];
|
|
851
|
+
function buildUserContent(params) {
|
|
852
|
+
const content = [{ type: "text", text: params.prompt ?? "" }];
|
|
853
|
+
for (const attachment of params.attachments ?? []) {
|
|
854
|
+
content.push({
|
|
855
|
+
type: "file",
|
|
856
|
+
data: attachment.data,
|
|
857
|
+
mediaType: attachment.mediaType,
|
|
858
|
+
...attachment.filename ? { filename: attachment.filename } : {}
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
return content;
|
|
862
|
+
}
|
|
863
|
+
function isReasoningModel(modelName) {
|
|
864
|
+
const lower = modelName.toLowerCase();
|
|
865
|
+
return REASONING_MODEL_PATTERNS.some((pattern) => lower.includes(pattern));
|
|
866
|
+
}
|
|
867
|
+
function supportsStopSequences(modelName) {
|
|
868
|
+
const lower = modelName.toLowerCase();
|
|
869
|
+
return !RESPONSES_ROUTED_PREFIXES.some((prefix) => lower.startsWith(prefix));
|
|
870
|
+
}
|
|
871
|
+
function isRecord(value) {
|
|
872
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
873
|
+
}
|
|
874
|
+
function asRecord2(value) {
|
|
875
|
+
return isRecord(value) ? value : {};
|
|
876
|
+
}
|
|
877
|
+
function recordAt(value, key) {
|
|
878
|
+
return asRecord2(value[key]);
|
|
879
|
+
}
|
|
880
|
+
function firstString(...values) {
|
|
881
|
+
for (const value of values) {
|
|
882
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
883
|
+
return value.trim();
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
function firstNumber(...values) {
|
|
889
|
+
for (const value of values) {
|
|
890
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
891
|
+
return value;
|
|
892
|
+
}
|
|
893
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
894
|
+
const parsed = Number(value);
|
|
895
|
+
if (Number.isFinite(parsed)) {
|
|
896
|
+
return parsed;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
function parseJsonIfPossible(value) {
|
|
903
|
+
if (typeof value !== "string") {
|
|
904
|
+
return value ?? {};
|
|
905
|
+
}
|
|
906
|
+
try {
|
|
907
|
+
return JSON.parse(value);
|
|
908
|
+
} catch {
|
|
909
|
+
return value;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
function stringifyMessageContent(content) {
|
|
913
|
+
if (typeof content === "string") {
|
|
914
|
+
return content;
|
|
915
|
+
}
|
|
916
|
+
if (content == null) {
|
|
917
|
+
return "";
|
|
918
|
+
}
|
|
919
|
+
return typeof content === "object" ? JSON.stringify(content) : String(content);
|
|
920
|
+
}
|
|
921
|
+
function hasNativeTransportOptions(params) {
|
|
922
|
+
return Boolean(params.messages || params.tools || params.toolChoice || params.responseSchema || params.providerOptions);
|
|
923
|
+
}
|
|
924
|
+
function shouldReturnNativeResult(params) {
|
|
925
|
+
return Boolean(params.messages || params.tools || params.toolChoice || params.responseSchema);
|
|
926
|
+
}
|
|
927
|
+
function buildNativeMessages(params, promptText, systemPrompt) {
|
|
928
|
+
if (Array.isArray(params.messages) && params.messages.length > 0) {
|
|
929
|
+
const messages2 = params.messages.map((message) => isRecord(message) ? { ...message } : { role: "user", content: stringifyMessageContent(message) });
|
|
930
|
+
const first = asRecord2(messages2[0]);
|
|
931
|
+
if (systemPrompt && first.role !== "system") {
|
|
932
|
+
return [{ role: "system", content: systemPrompt }, ...messages2];
|
|
933
|
+
}
|
|
934
|
+
return messages2;
|
|
935
|
+
}
|
|
936
|
+
const messages = [];
|
|
937
|
+
if (systemPrompt) {
|
|
938
|
+
messages.push({ role: "system", content: systemPrompt });
|
|
939
|
+
}
|
|
940
|
+
messages.push({ role: "user", content: promptText });
|
|
941
|
+
return messages;
|
|
942
|
+
}
|
|
943
|
+
function unwrapJsonSchema(value) {
|
|
944
|
+
const record = asRecord2(value);
|
|
945
|
+
return record.schema ?? record.jsonSchema ?? value;
|
|
946
|
+
}
|
|
947
|
+
function normalizeNativeTools(tools) {
|
|
948
|
+
if (!tools) {
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
if (Array.isArray(tools)) {
|
|
952
|
+
return tools;
|
|
953
|
+
}
|
|
954
|
+
const toolSet = asRecord2(tools);
|
|
955
|
+
const normalized = [];
|
|
956
|
+
for (const [name, rawTool] of Object.entries(toolSet)) {
|
|
957
|
+
const tool = asRecord2(rawTool);
|
|
958
|
+
const inputSchema = unwrapJsonSchema(tool.inputSchema ?? tool.parameters ?? tool.schema ?? { type: "object" });
|
|
959
|
+
normalized.push({
|
|
960
|
+
type: "function",
|
|
961
|
+
function: {
|
|
962
|
+
name,
|
|
963
|
+
...typeof tool.description === "string" ? { description: tool.description } : {},
|
|
964
|
+
parameters: inputSchema
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
969
|
+
}
|
|
970
|
+
function normalizeNativeToolChoice(toolChoice) {
|
|
971
|
+
if (!toolChoice) {
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
if (typeof toolChoice === "string" && (toolChoice === "auto" || toolChoice === "none" || toolChoice === "required")) {
|
|
975
|
+
return toolChoice;
|
|
976
|
+
}
|
|
977
|
+
const choice = asRecord2(toolChoice);
|
|
978
|
+
if (choice.type === "function") {
|
|
979
|
+
return toolChoice;
|
|
980
|
+
}
|
|
981
|
+
if (choice.type === "tool") {
|
|
982
|
+
const toolName2 = firstString(choice.toolName, choice.name);
|
|
983
|
+
return toolName2 ? { type: "function", function: { name: toolName2 } } : toolChoice;
|
|
984
|
+
}
|
|
985
|
+
const functionChoice = asRecord2(choice.function);
|
|
986
|
+
const toolName = firstString(choice.toolName, choice.name, functionChoice.name);
|
|
987
|
+
return toolName ? { type: "function", function: { name: toolName } } : toolChoice;
|
|
988
|
+
}
|
|
989
|
+
function buildNativeResponseFormat(responseSchema) {
|
|
990
|
+
if (!responseSchema) {
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
const schemaRecord = asRecord2(responseSchema);
|
|
994
|
+
if (schemaRecord.responseFormat) {
|
|
995
|
+
return schemaRecord.responseFormat;
|
|
996
|
+
}
|
|
997
|
+
const schemaOptions = "schema" in schemaRecord ? {
|
|
998
|
+
schema: schemaRecord.schema,
|
|
999
|
+
name: firstString(schemaRecord.name) ?? "structured_response",
|
|
1000
|
+
description: firstString(schemaRecord.description)
|
|
1001
|
+
} : { schema: responseSchema, name: "structured_response", description: undefined };
|
|
1002
|
+
return {
|
|
1003
|
+
type: "json_schema",
|
|
1004
|
+
json_schema: {
|
|
1005
|
+
name: schemaOptions.name,
|
|
1006
|
+
...schemaOptions.description ? { description: schemaOptions.description } : {},
|
|
1007
|
+
schema: schemaOptions.schema
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
function resolvePromptCacheKey(providerOptions) {
|
|
1012
|
+
const eliza = recordAt(providerOptions, "eliza");
|
|
1013
|
+
const openrouter = recordAt(providerOptions, "openrouter");
|
|
1014
|
+
const openai = recordAt(providerOptions, "openai");
|
|
1015
|
+
const cerebras = recordAt(providerOptions, "cerebras");
|
|
1016
|
+
return firstString(providerOptions.promptCacheKey, providerOptions.prompt_cache_key, eliza.promptCacheKey, eliza.prompt_cache_key, openrouter.promptCacheKey, openrouter.prompt_cache_key, openai.promptCacheKey, openai.prompt_cache_key, cerebras.promptCacheKey, cerebras.prompt_cache_key);
|
|
1017
|
+
}
|
|
1018
|
+
function resolveNativeProviderOptions(params) {
|
|
1019
|
+
const raw = asRecord2(params.providerOptions);
|
|
1020
|
+
if (Object.keys(raw).length === 0) {
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
const { agentName: _agentName, eliza: _eliza, ...rest } = raw;
|
|
1024
|
+
const providerOptions = { ...rest };
|
|
1025
|
+
const promptCacheKey = resolvePromptCacheKey(raw);
|
|
1026
|
+
if (promptCacheKey) {
|
|
1027
|
+
providerOptions.openai = {
|
|
1028
|
+
...recordAt(providerOptions, "openai"),
|
|
1029
|
+
promptCacheKey,
|
|
1030
|
+
prompt_cache_key: promptCacheKey
|
|
1031
|
+
};
|
|
1032
|
+
providerOptions.openrouter = {
|
|
1033
|
+
...recordAt(providerOptions, "openrouter"),
|
|
1034
|
+
promptCacheKey,
|
|
1035
|
+
prompt_cache_key: promptCacheKey
|
|
1036
|
+
};
|
|
1037
|
+
providerOptions.cerebras = {
|
|
1038
|
+
...recordAt(providerOptions, "cerebras"),
|
|
1039
|
+
prompt_cache_key: promptCacheKey
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
return Object.keys(providerOptions).length > 0 ? providerOptions : undefined;
|
|
1043
|
+
}
|
|
1044
|
+
function applyOpenRouterPassthroughFields(requestBody, providerOptions) {
|
|
1045
|
+
if (!providerOptions) {
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
const openrouter = recordAt(providerOptions, "openrouter");
|
|
1049
|
+
if (Object.keys(openrouter).length > 0) {
|
|
1050
|
+
const provider = openrouter.provider;
|
|
1051
|
+
if (provider !== undefined) {
|
|
1052
|
+
requestBody.provider = provider;
|
|
1053
|
+
}
|
|
1054
|
+
for (const key of ["models", "route", "transforms", "reasoning"]) {
|
|
1055
|
+
if (openrouter[key] !== undefined) {
|
|
1056
|
+
requestBody[key] = openrouter[key];
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
const gateway = providerOptions.gateway;
|
|
1061
|
+
if (gateway !== undefined) {
|
|
1062
|
+
requestBody.gateway = gateway;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
function buildNativeRequestBody(params, modelName, promptText, systemPrompt) {
|
|
1066
|
+
const providerOptions = resolveNativeProviderOptions(params);
|
|
1067
|
+
const promptCacheKey = providerOptions ? resolvePromptCacheKey(providerOptions) : undefined;
|
|
1068
|
+
const tools = normalizeNativeTools(params.tools);
|
|
1069
|
+
const toolChoice = normalizeNativeToolChoice(params.toolChoice);
|
|
1070
|
+
const responseFormat = buildNativeResponseFormat(params.responseSchema);
|
|
1071
|
+
const requestBody = {
|
|
1072
|
+
model: modelName,
|
|
1073
|
+
messages: buildNativeMessages(params, promptText, systemPrompt),
|
|
1074
|
+
max_tokens: params.maxTokens ?? 8192
|
|
1075
|
+
};
|
|
1076
|
+
if (!isReasoningModel(modelName) && typeof params.temperature === "number") {
|
|
1077
|
+
requestBody.temperature = params.temperature;
|
|
1078
|
+
}
|
|
1079
|
+
if (tools) {
|
|
1080
|
+
requestBody.tools = tools;
|
|
1081
|
+
}
|
|
1082
|
+
if (toolChoice) {
|
|
1083
|
+
requestBody.tool_choice = toolChoice;
|
|
1084
|
+
}
|
|
1085
|
+
if (responseFormat) {
|
|
1086
|
+
requestBody.response_format = responseFormat;
|
|
1087
|
+
}
|
|
1088
|
+
if (providerOptions) {
|
|
1089
|
+
requestBody.providerOptions = providerOptions;
|
|
1090
|
+
requestBody.provider_options = providerOptions;
|
|
1091
|
+
}
|
|
1092
|
+
if (promptCacheKey) {
|
|
1093
|
+
requestBody.promptCacheKey = promptCacheKey;
|
|
1094
|
+
requestBody.prompt_cache_key = promptCacheKey;
|
|
1095
|
+
}
|
|
1096
|
+
applyOpenRouterPassthroughFields(requestBody, providerOptions);
|
|
1097
|
+
return requestBody;
|
|
1098
|
+
}
|
|
1099
|
+
function extractTextFromContent(content) {
|
|
1100
|
+
if (typeof content === "string") {
|
|
1101
|
+
return content;
|
|
1102
|
+
}
|
|
1103
|
+
if (!Array.isArray(content)) {
|
|
1104
|
+
return "";
|
|
1105
|
+
}
|
|
1106
|
+
return content.map((item) => {
|
|
1107
|
+
if (typeof item === "string")
|
|
1108
|
+
return item;
|
|
1109
|
+
const record = asRecord2(item);
|
|
1110
|
+
return firstString(record.text, record.output_text, record.content) ?? "";
|
|
1111
|
+
}).join("");
|
|
1112
|
+
}
|
|
1113
|
+
function extractChatCompletionText(data) {
|
|
1114
|
+
const firstChoice = data.choices?.[0];
|
|
1115
|
+
if (!firstChoice) {
|
|
1116
|
+
return "";
|
|
1117
|
+
}
|
|
1118
|
+
return firstString(firstChoice.text, extractTextFromContent(firstChoice.message?.content)) ?? "";
|
|
1119
|
+
}
|
|
1120
|
+
function extractNativeToolCalls(data) {
|
|
1121
|
+
const rawCalls = data.choices?.[0]?.message?.tool_calls ?? [];
|
|
1122
|
+
if (!Array.isArray(rawCalls)) {
|
|
1123
|
+
return [];
|
|
1124
|
+
}
|
|
1125
|
+
return rawCalls.map((rawCall) => {
|
|
1126
|
+
const call = asRecord2(rawCall);
|
|
1127
|
+
const fn = recordAt(call, "function");
|
|
1128
|
+
const toolName = firstString(call.name, call.toolName, fn.name);
|
|
1129
|
+
if (!toolName) {
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
return {
|
|
1133
|
+
type: "tool-call",
|
|
1134
|
+
toolCallId: firstString(call.id, call.toolCallId) ?? `call_${toolName}`,
|
|
1135
|
+
toolName,
|
|
1136
|
+
input: parseJsonIfPossible(call.input ?? call.arguments ?? fn.arguments ?? {})
|
|
1137
|
+
};
|
|
1138
|
+
}).filter((call) => call !== undefined);
|
|
1139
|
+
}
|
|
1140
|
+
function convertNativeUsage(usage) {
|
|
1141
|
+
const root = asRecord2(usage);
|
|
1142
|
+
if (Object.keys(root).length === 0) {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
const inputTokenDetails = recordAt(root, "inputTokenDetails");
|
|
1146
|
+
const promptTokenDetails = recordAt(root, "prompt_tokens_details");
|
|
1147
|
+
const inputTokenDetailsSnake = recordAt(root, "input_tokens_details");
|
|
1148
|
+
const promptTokens = firstNumber(root.inputTokens, root.input_tokens, root.promptTokens, root.prompt_tokens) ?? 0;
|
|
1149
|
+
const completionTokens = firstNumber(root.outputTokens, root.output_tokens, root.completionTokens, root.completion_tokens) ?? 0;
|
|
1150
|
+
const cacheReadInputTokens = firstNumber(root.cacheReadInputTokens, root.cache_read_input_tokens, root.cachedInputTokens, root.cached_input_tokens, root.cachedTokens, root.cached_tokens, inputTokenDetails.cacheReadTokens, inputTokenDetails.cachedInputTokens, inputTokenDetails.cachedTokens, promptTokenDetails.cached_tokens, inputTokenDetailsSnake.cache_read_input_tokens, inputTokenDetailsSnake.cached_tokens);
|
|
1151
|
+
const cacheCreationInputTokens = firstNumber(root.cacheCreationInputTokens, root.cache_creation_input_tokens, root.cacheWriteInputTokens, root.cache_write_input_tokens, inputTokenDetails.cacheCreationInputTokens, inputTokenDetails.cacheCreationTokens, inputTokenDetails.cacheWriteTokens, inputTokenDetailsSnake.cache_creation_input_tokens);
|
|
1152
|
+
return {
|
|
1153
|
+
promptTokens,
|
|
1154
|
+
completionTokens,
|
|
1155
|
+
totalTokens: firstNumber(root.totalTokens, root.total_tokens) ?? promptTokens + completionTokens,
|
|
1156
|
+
cachedPromptTokens: cacheReadInputTokens,
|
|
1157
|
+
cacheReadInputTokens,
|
|
1158
|
+
cacheCreationInputTokens
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
function getPurposeForModelType(modelType) {
|
|
1162
|
+
switch (modelType) {
|
|
1163
|
+
case RESPONSE_HANDLER_MODEL_TYPE:
|
|
1164
|
+
return "should_respond";
|
|
1165
|
+
case ACTION_PLANNER_MODEL_TYPE:
|
|
1166
|
+
return "action_planner";
|
|
1167
|
+
default:
|
|
1168
|
+
return "response";
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
function getModelNameForType(runtime, modelType) {
|
|
1172
|
+
switch (modelType) {
|
|
1173
|
+
case TEXT_NANO_MODEL_TYPE:
|
|
1174
|
+
return getNanoModel(runtime);
|
|
1175
|
+
case TEXT_MEDIUM_MODEL_TYPE:
|
|
1176
|
+
return getMediumModel(runtime);
|
|
1177
|
+
case TEXT_SMALL_MODEL_TYPE:
|
|
1178
|
+
return getSmallModel(runtime);
|
|
1179
|
+
case TEXT_LARGE_MODEL_TYPE:
|
|
1180
|
+
return getLargeModel(runtime);
|
|
1181
|
+
case TEXT_MEGA_MODEL_TYPE:
|
|
1182
|
+
return getMegaModel(runtime);
|
|
1183
|
+
case RESPONSE_HANDLER_MODEL_TYPE:
|
|
1184
|
+
return getResponseHandlerModel(runtime);
|
|
1185
|
+
case ACTION_PLANNER_MODEL_TYPE:
|
|
1186
|
+
return getActionPlannerModel(runtime);
|
|
1187
|
+
default:
|
|
1188
|
+
return getLargeModel(runtime);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function buildGenerateParams(runtime, modelType, params) {
|
|
1192
|
+
const paramsWithAttachments = params;
|
|
1193
|
+
const prompt = params.prompt ?? "";
|
|
1194
|
+
const maxTokens = params.maxTokens ?? 8192;
|
|
1195
|
+
const openai = createOpenAIClient(runtime);
|
|
1196
|
+
const modelName = getModelNameForType(runtime, modelType);
|
|
1197
|
+
const experimentalTelemetry = getExperimentalTelemetry(runtime);
|
|
1198
|
+
const userContent = (paramsWithAttachments.attachments?.length ?? 0) > 0 ? buildUserContent(paramsWithAttachments) : undefined;
|
|
1199
|
+
const model = openai.chat(modelName);
|
|
1200
|
+
const reasoning = isReasoningModel(modelName);
|
|
1201
|
+
const stopSequences = !reasoning && supportsStopSequences(modelName) && Array.isArray(params.stopSequences) && params.stopSequences.length > 0 ? params.stopSequences : undefined;
|
|
1202
|
+
const systemPrompt = resolveEffectiveSystemPrompt({
|
|
1203
|
+
params,
|
|
1204
|
+
fallback: buildCanonicalSystemPrompt({ character: runtime.character })
|
|
1205
|
+
});
|
|
1206
|
+
const promptText = renderChatMessagesForPrompt(params.messages, {
|
|
1207
|
+
omitDuplicateSystem: systemPrompt
|
|
1208
|
+
}) ?? prompt;
|
|
1209
|
+
const generateParams = {
|
|
1210
|
+
model,
|
|
1211
|
+
...userContent ? { messages: [{ role: "user", content: userContent }] } : { prompt: promptText },
|
|
1212
|
+
system: systemPrompt,
|
|
1213
|
+
...stopSequences ? { stopSequences } : {},
|
|
1214
|
+
maxOutputTokens: maxTokens,
|
|
1215
|
+
experimental_telemetry: {
|
|
1216
|
+
isEnabled: experimentalTelemetry
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
return { generateParams, modelName, modelType, prompt: promptText, systemPrompt };
|
|
1220
|
+
}
|
|
1221
|
+
async function generateTextWithModel(runtime, modelType, params) {
|
|
1222
|
+
const { modelName, prompt, systemPrompt } = buildGenerateParams(runtime, modelType, params);
|
|
1223
|
+
const paramsWithNative = params;
|
|
1224
|
+
logger7.debug(`[ELIZAOS_CLOUD] Generating text with ${modelType} model: ${modelName}`);
|
|
1225
|
+
if (params.stream) {
|
|
1226
|
+
logger7.debug("[ELIZAOS_CLOUD] Streaming text disabled for responses compatibility; falling back to buffered response.");
|
|
1227
|
+
}
|
|
1228
|
+
logger7.log(`[ELIZAOS_CLOUD] Using ${modelType} model: ${modelName}`);
|
|
1229
|
+
logger7.log(prompt);
|
|
1230
|
+
if (hasNativeTransportOptions(paramsWithNative)) {
|
|
1231
|
+
const nativeResult = await generateNativeChatCompletion(runtime, modelType, paramsWithNative, {
|
|
1232
|
+
modelName,
|
|
1233
|
+
prompt,
|
|
1234
|
+
systemPrompt
|
|
1235
|
+
});
|
|
1236
|
+
return shouldReturnNativeResult(paramsWithNative) ? nativeResult : nativeResult.text;
|
|
1237
|
+
}
|
|
1238
|
+
const reasoning = isReasoningModel(modelName);
|
|
1239
|
+
const input = [];
|
|
1240
|
+
if (systemPrompt) {
|
|
1241
|
+
input.push({
|
|
1242
|
+
role: "system",
|
|
1243
|
+
content: [{ type: "input_text", text: systemPrompt }]
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
input.push({
|
|
1247
|
+
role: "user",
|
|
1248
|
+
content: [{ type: "input_text", text: prompt }]
|
|
1249
|
+
});
|
|
1250
|
+
const requestBody = {
|
|
1251
|
+
model: modelName,
|
|
1252
|
+
input,
|
|
1253
|
+
max_output_tokens: params.maxTokens ?? 8192
|
|
1254
|
+
};
|
|
1255
|
+
if (!reasoning && typeof params.temperature === "number") {
|
|
1256
|
+
requestBody.temperature = params.temperature;
|
|
1257
|
+
}
|
|
1258
|
+
const response = await createCloudApiClient(runtime).requestRaw("POST", "/responses", {
|
|
1259
|
+
headers: {
|
|
1260
|
+
"X-Eliza-Llm-Purpose": getPurposeForModelType(modelType),
|
|
1261
|
+
"X-Eliza-Model-Type": modelType
|
|
1262
|
+
},
|
|
1263
|
+
json: requestBody
|
|
1264
|
+
});
|
|
1265
|
+
const responseText = await response.text();
|
|
1266
|
+
let data = {};
|
|
1267
|
+
if (responseText) {
|
|
1268
|
+
try {
|
|
1269
|
+
data = JSON.parse(responseText);
|
|
1270
|
+
} catch (parseErr) {
|
|
1271
|
+
logger7.error(`[ELIZAOS_CLOUD] Failed to parse responses JSON: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
if (!response.ok) {
|
|
1275
|
+
const errorBody = typeof data === "object" && data ? data.error : undefined;
|
|
1276
|
+
const errorMessage = typeof errorBody?.message === "string" && errorBody.message.trim() ? errorBody.message.trim() : `elizaOS Cloud error ${response.status}`;
|
|
1277
|
+
const requestError = new Error(errorMessage);
|
|
1278
|
+
requestError.status = response.status;
|
|
1279
|
+
if (errorBody) {
|
|
1280
|
+
requestError.error = errorBody;
|
|
1281
|
+
}
|
|
1282
|
+
throw requestError;
|
|
1283
|
+
}
|
|
1284
|
+
if (data.usage) {
|
|
1285
|
+
emitModelUsageEvent(runtime, modelType, prompt, {
|
|
1286
|
+
inputTokens: data.usage.input_tokens ?? 0,
|
|
1287
|
+
outputTokens: data.usage.output_tokens ?? 0,
|
|
1288
|
+
totalTokens: data.usage.total_tokens ?? 0
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
const text = extractResponsesOutputText(data);
|
|
1292
|
+
if (!text.trim()) {
|
|
1293
|
+
throw new Error("elizaOS Cloud returned no text response");
|
|
1294
|
+
}
|
|
1295
|
+
return text;
|
|
1296
|
+
}
|
|
1297
|
+
async function generateNativeChatCompletion(runtime, modelType, params, context) {
|
|
1298
|
+
const requestBody = buildNativeRequestBody(params, context.modelName, context.prompt, context.systemPrompt);
|
|
1299
|
+
const response = await createCloudApiClient(runtime).requestRaw("POST", "/chat/completions", {
|
|
1300
|
+
headers: {
|
|
1301
|
+
"X-Eliza-Llm-Purpose": getPurposeForModelType(modelType),
|
|
1302
|
+
"X-Eliza-Model-Type": modelType
|
|
1303
|
+
},
|
|
1304
|
+
json: requestBody
|
|
1305
|
+
});
|
|
1306
|
+
const responseText = await response.text();
|
|
1307
|
+
let data = {};
|
|
1308
|
+
if (responseText) {
|
|
1309
|
+
try {
|
|
1310
|
+
data = JSON.parse(responseText);
|
|
1311
|
+
} catch (parseErr) {
|
|
1312
|
+
logger7.error(`[ELIZAOS_CLOUD] Failed to parse chat completions JSON: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`);
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
if (!response.ok) {
|
|
1316
|
+
const errorBody = typeof data === "object" && data ? data.error : undefined;
|
|
1317
|
+
const errorMessage = typeof errorBody?.message === "string" && errorBody.message.trim() ? errorBody.message.trim() : `elizaOS Cloud error ${response.status}`;
|
|
1318
|
+
const requestError = new Error(errorMessage);
|
|
1319
|
+
requestError.status = response.status;
|
|
1320
|
+
if (errorBody) {
|
|
1321
|
+
requestError.error = errorBody;
|
|
1322
|
+
}
|
|
1323
|
+
throw requestError;
|
|
1324
|
+
}
|
|
1325
|
+
const usage = convertNativeUsage(data.usage);
|
|
1326
|
+
if (usage) {
|
|
1327
|
+
emitModelUsageEvent(runtime, modelType, context.prompt, usage);
|
|
1328
|
+
}
|
|
1329
|
+
const text = extractChatCompletionText(data);
|
|
1330
|
+
const toolCalls = extractNativeToolCalls(data);
|
|
1331
|
+
if (!text.trim() && toolCalls.length === 0) {
|
|
1332
|
+
throw new Error("elizaOS Cloud returned no text or tool calls");
|
|
1333
|
+
}
|
|
1334
|
+
return {
|
|
1335
|
+
text,
|
|
1336
|
+
toolCalls,
|
|
1337
|
+
finishReason: data.choices?.[0]?.finish_reason,
|
|
1338
|
+
usage,
|
|
1339
|
+
providerMetadata: {
|
|
1340
|
+
modelName: context.modelName,
|
|
1341
|
+
usage: data.usage
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
async function handleTextSmall(runtime, params) {
|
|
1346
|
+
return generateTextWithModel(runtime, TEXT_SMALL_MODEL_TYPE, params);
|
|
1347
|
+
}
|
|
1348
|
+
async function handleTextNano(runtime, params) {
|
|
1349
|
+
return generateTextWithModel(runtime, TEXT_NANO_MODEL_TYPE, params);
|
|
1350
|
+
}
|
|
1351
|
+
async function handleTextMedium(runtime, params) {
|
|
1352
|
+
return generateTextWithModel(runtime, TEXT_MEDIUM_MODEL_TYPE, params);
|
|
1353
|
+
}
|
|
1354
|
+
async function handleTextLarge(runtime, params) {
|
|
1355
|
+
return generateTextWithModel(runtime, TEXT_LARGE_MODEL_TYPE, params);
|
|
1356
|
+
}
|
|
1357
|
+
async function handleTextMega(runtime, params) {
|
|
1358
|
+
return generateTextWithModel(runtime, TEXT_MEGA_MODEL_TYPE, params);
|
|
1359
|
+
}
|
|
1360
|
+
async function handleResponseHandler(runtime, params) {
|
|
1361
|
+
return generateTextWithModel(runtime, RESPONSE_HANDLER_MODEL_TYPE, params);
|
|
1362
|
+
}
|
|
1363
|
+
async function handleActionPlanner(runtime, params) {
|
|
1364
|
+
return generateTextWithModel(runtime, ACTION_PLANNER_MODEL_TYPE, params);
|
|
1365
|
+
}
|
|
1366
|
+
export {
|
|
1367
|
+
handleTranscription,
|
|
1368
|
+
handleTokenizerEncode,
|
|
1369
|
+
handleTokenizerDecode,
|
|
1370
|
+
handleTextToSpeech,
|
|
1371
|
+
handleTextSmall,
|
|
1372
|
+
handleTextNano,
|
|
1373
|
+
handleTextMega,
|
|
1374
|
+
handleTextMedium,
|
|
1375
|
+
handleTextLarge,
|
|
1376
|
+
handleTextEmbedding,
|
|
1377
|
+
handleResponseHandler,
|
|
1378
|
+
handleResearch,
|
|
1379
|
+
handleImageGeneration,
|
|
1380
|
+
handleImageDescription,
|
|
1381
|
+
handleBatchTextEmbedding,
|
|
1382
|
+
handleActionPlanner,
|
|
1383
|
+
fetchTextToSpeech
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
//# debugId=06464BB61850EEE264756E2164756E21
|