beddel 0.2.2 → 0.3.0
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/CHANGELOG.md +50 -0
- package/README.md +117 -14
- package/dist/agents/chat/chat.handler.d.ts +12 -0
- package/dist/agents/chat/chat.handler.d.ts.map +1 -0
- package/dist/agents/chat/chat.handler.js +145 -0
- package/dist/agents/chat/chat.handler.js.map +1 -0
- package/dist/agents/chat/chat.schema.d.ts +38 -0
- package/dist/agents/chat/chat.schema.d.ts.map +1 -0
- package/dist/agents/chat/chat.schema.js +31 -0
- package/dist/agents/chat/chat.schema.js.map +1 -0
- package/dist/agents/chat/chat.types.d.ts +42 -0
- package/dist/agents/chat/chat.types.d.ts.map +1 -0
- package/dist/agents/chat/chat.types.js +6 -0
- package/dist/agents/chat/chat.types.js.map +1 -0
- package/dist/agents/chat/chat.yaml +148 -0
- package/dist/agents/chat/index.d.ts +16 -0
- package/dist/agents/chat/index.d.ts.map +1 -0
- package/dist/agents/chat/index.js +21 -0
- package/dist/agents/chat/index.js.map +1 -0
- package/dist/agents/chromadb/chromadb.handler.d.ts +12 -0
- package/dist/agents/chromadb/chromadb.handler.d.ts.map +1 -0
- package/dist/agents/chromadb/chromadb.handler.js +139 -0
- package/dist/agents/chromadb/chromadb.handler.js.map +1 -0
- package/dist/agents/chromadb/chromadb.schema.d.ts +36 -0
- package/dist/agents/chromadb/chromadb.schema.d.ts.map +1 -0
- package/dist/agents/chromadb/chromadb.schema.js +33 -0
- package/dist/agents/chromadb/chromadb.schema.js.map +1 -0
- package/dist/agents/chromadb/chromadb.types.d.ts +49 -0
- package/dist/agents/chromadb/chromadb.types.d.ts.map +1 -0
- package/dist/agents/chromadb/chromadb.types.js +6 -0
- package/dist/agents/chromadb/chromadb.types.js.map +1 -0
- package/dist/agents/chromadb/chromadb.yaml +128 -0
- package/dist/agents/chromadb/index.d.ts +15 -0
- package/dist/agents/chromadb/index.d.ts.map +1 -0
- package/dist/agents/chromadb/index.js +20 -0
- package/dist/agents/chromadb/index.js.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.handler.d.ts +8 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.handler.d.ts.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.handler.js +61 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.handler.js.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.schema.d.ts +22 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.schema.d.ts.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.schema.js +20 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.schema.js.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.types.d.ts +32 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.types.d.ts.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.types.js +6 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.types.js.map +1 -0
- package/dist/agents/gemini-vectorize/gemini-vectorize.yaml +84 -0
- package/dist/agents/gemini-vectorize/index.d.ts +15 -0
- package/dist/agents/gemini-vectorize/index.d.ts.map +1 -0
- package/dist/agents/gemini-vectorize/index.js +20 -0
- package/dist/agents/gemini-vectorize/index.js.map +1 -0
- package/dist/agents/gitmcp/gitmcp.handler.d.ts +12 -0
- package/dist/agents/gitmcp/gitmcp.handler.d.ts.map +1 -0
- package/dist/agents/gitmcp/gitmcp.handler.js +95 -0
- package/dist/agents/gitmcp/gitmcp.handler.js.map +1 -0
- package/dist/agents/gitmcp/gitmcp.schema.d.ts +17 -0
- package/dist/agents/gitmcp/gitmcp.schema.d.ts.map +1 -0
- package/dist/agents/gitmcp/gitmcp.schema.js +18 -0
- package/dist/agents/gitmcp/gitmcp.schema.js.map +1 -0
- package/dist/agents/gitmcp/gitmcp.types.d.ts +31 -0
- package/dist/agents/gitmcp/gitmcp.types.d.ts.map +1 -0
- package/dist/agents/gitmcp/gitmcp.types.js +6 -0
- package/dist/agents/gitmcp/gitmcp.types.js.map +1 -0
- package/dist/agents/gitmcp/gitmcp.yaml +71 -0
- package/dist/agents/gitmcp/index.d.ts +16 -0
- package/dist/agents/gitmcp/index.d.ts.map +1 -0
- package/dist/agents/gitmcp/index.js +21 -0
- package/dist/agents/gitmcp/index.js.map +1 -0
- package/dist/agents/image/image.handler.d.ts +8 -0
- package/dist/agents/image/image.handler.d.ts.map +1 -0
- package/dist/agents/image/image.handler.js +69 -0
- package/dist/agents/image/image.handler.js.map +1 -0
- package/dist/agents/image/image.schema.d.ts +29 -0
- package/dist/agents/image/image.schema.d.ts.map +1 -0
- package/dist/agents/image/image.schema.js +26 -0
- package/dist/agents/image/image.schema.js.map +1 -0
- package/dist/agents/image/image.types.d.ts +43 -0
- package/dist/agents/image/image.types.d.ts.map +1 -0
- package/dist/agents/image/image.types.js +6 -0
- package/dist/agents/image/image.types.js.map +1 -0
- package/{src/agents/image-agent.yaml → dist/agents/image/image.yaml} +21 -21
- package/dist/agents/image/index.d.ts +14 -0
- package/dist/agents/image/index.d.ts.map +1 -0
- package/dist/agents/image/index.js +19 -0
- package/dist/agents/image/index.js.map +1 -0
- package/dist/agents/index.d.ts +104 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +82 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/joker/index.d.ts +14 -0
- package/dist/agents/joker/index.d.ts.map +1 -0
- package/dist/agents/joker/index.js +19 -0
- package/dist/agents/joker/index.js.map +1 -0
- package/dist/agents/joker/joker.handler.d.ts +8 -0
- package/dist/agents/joker/joker.handler.d.ts.map +1 -0
- package/dist/agents/joker/joker.handler.js +44 -0
- package/dist/agents/joker/joker.handler.js.map +1 -0
- package/dist/agents/joker/joker.schema.d.ts +12 -0
- package/dist/agents/joker/joker.schema.d.ts.map +1 -0
- package/dist/agents/joker/joker.schema.js +13 -0
- package/dist/agents/joker/joker.schema.js.map +1 -0
- package/dist/agents/joker/joker.types.d.ts +36 -0
- package/dist/agents/joker/joker.types.d.ts.map +1 -0
- package/dist/agents/joker/joker.types.js +6 -0
- package/dist/agents/joker/joker.types.js.map +1 -0
- package/{src/agents/joker-agent.yaml → dist/agents/joker/joker.yaml} +4 -4
- package/dist/agents/llm/index.d.ts +15 -0
- package/dist/agents/llm/index.d.ts.map +1 -0
- package/dist/agents/llm/index.js +20 -0
- package/dist/agents/llm/index.js.map +1 -0
- package/dist/agents/llm/llm.handler.d.ts +8 -0
- package/dist/agents/llm/llm.handler.d.ts.map +1 -0
- package/dist/agents/llm/llm.handler.js +64 -0
- package/dist/agents/llm/llm.handler.js.map +1 -0
- package/dist/agents/llm/llm.schema.d.ts +26 -0
- package/dist/agents/llm/llm.schema.d.ts.map +1 -0
- package/dist/agents/llm/llm.schema.js +23 -0
- package/dist/agents/llm/llm.schema.js.map +1 -0
- package/dist/agents/llm/llm.types.d.ts +34 -0
- package/dist/agents/llm/llm.types.d.ts.map +1 -0
- package/dist/agents/llm/llm.types.js +7 -0
- package/dist/agents/llm/llm.types.js.map +1 -0
- package/dist/agents/llm/llm.yaml +87 -0
- package/dist/agents/mcp-tool/index.d.ts +14 -0
- package/dist/agents/mcp-tool/index.d.ts.map +1 -0
- package/dist/agents/mcp-tool/index.js +19 -0
- package/dist/agents/mcp-tool/index.js.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.handler.d.ts +12 -0
- package/dist/agents/mcp-tool/mcp-tool.handler.d.ts.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.handler.js +116 -0
- package/dist/agents/mcp-tool/mcp-tool.handler.js.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.schema.d.ts +19 -0
- package/dist/agents/mcp-tool/mcp-tool.schema.d.ts.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.schema.js +20 -0
- package/dist/agents/mcp-tool/mcp-tool.schema.js.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.types.d.ts +31 -0
- package/dist/agents/mcp-tool/mcp-tool.types.d.ts.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.types.js +6 -0
- package/dist/agents/mcp-tool/mcp-tool.types.js.map +1 -0
- package/dist/agents/mcp-tool/mcp-tool.yaml +71 -0
- package/dist/agents/rag/index.d.ts +15 -0
- package/dist/agents/rag/index.d.ts.map +1 -0
- package/dist/agents/rag/index.js +20 -0
- package/dist/agents/rag/index.js.map +1 -0
- package/dist/agents/rag/rag.handler.d.ts +9 -0
- package/dist/agents/rag/rag.handler.d.ts.map +1 -0
- package/dist/agents/rag/rag.handler.js +78 -0
- package/dist/agents/rag/rag.handler.js.map +1 -0
- package/dist/agents/rag/rag.schema.d.ts +27 -0
- package/dist/agents/rag/rag.schema.d.ts.map +1 -0
- package/dist/agents/rag/rag.schema.js +24 -0
- package/dist/agents/rag/rag.schema.js.map +1 -0
- package/dist/agents/rag/rag.types.d.ts +42 -0
- package/dist/agents/rag/rag.types.d.ts.map +1 -0
- package/dist/agents/rag/rag.types.js +7 -0
- package/dist/agents/rag/rag.types.js.map +1 -0
- package/dist/agents/rag/rag.yaml +89 -0
- package/dist/agents/{agentRegistry.d.ts → registry/agentRegistry.d.ts} +30 -1
- package/dist/agents/registry/agentRegistry.d.ts.map +1 -0
- package/dist/agents/{agentRegistry.js → registry/agentRegistry.js} +187 -5
- package/dist/agents/registry/agentRegistry.js.map +1 -0
- package/dist/agents/registry/index.d.ts +6 -0
- package/dist/agents/registry/index.d.ts.map +1 -0
- package/dist/agents/registry/index.js +10 -0
- package/dist/agents/registry/index.js.map +1 -0
- package/dist/agents/translator/index.d.ts +14 -0
- package/dist/agents/translator/index.d.ts.map +1 -0
- package/dist/agents/translator/index.js +19 -0
- package/dist/agents/translator/index.js.map +1 -0
- package/dist/agents/translator/translator.handler.d.ts +8 -0
- package/dist/agents/translator/translator.handler.d.ts.map +1 -0
- package/dist/agents/translator/translator.handler.js +81 -0
- package/dist/agents/translator/translator.handler.js.map +1 -0
- package/dist/agents/translator/translator.schema.d.ts +27 -0
- package/dist/agents/translator/translator.schema.d.ts.map +1 -0
- package/dist/agents/translator/translator.schema.js +28 -0
- package/dist/agents/translator/translator.schema.js.map +1 -0
- package/dist/agents/translator/translator.types.d.ts +41 -0
- package/dist/agents/translator/translator.types.d.ts.map +1 -0
- package/dist/agents/translator/translator.types.js +6 -0
- package/dist/agents/translator/translator.types.js.map +1 -0
- package/{src/agents/translator-agent.yaml → dist/agents/translator/translator.yaml} +27 -27
- package/dist/client/index.d.ts +89 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +93 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +17 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +7 -0
- package/dist/client/types.js.map +1 -0
- package/dist/firebase/tenantManager.d.ts +34 -2
- package/dist/firebase/tenantManager.d.ts.map +1 -1
- package/dist/firebase/tenantManager.js +67 -1
- package/dist/firebase/tenantManager.js.map +1 -1
- package/dist/index.d.ts +10 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -7
- package/dist/index.js.map +1 -1
- package/dist/runtime/declarativeAgentRuntime.d.ts +17 -52
- package/dist/runtime/declarativeAgentRuntime.d.ts.map +1 -1
- package/dist/runtime/declarativeAgentRuntime.js +259 -361
- package/dist/runtime/declarativeAgentRuntime.js.map +1 -1
- package/dist/runtime/index.d.ts +14 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +38 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/llmProviderFactory.d.ts +47 -0
- package/dist/runtime/llmProviderFactory.d.ts.map +1 -0
- package/dist/runtime/llmProviderFactory.js +119 -0
- package/dist/runtime/llmProviderFactory.js.map +1 -0
- package/dist/runtime/workflowExecutor.d.ts +31 -0
- package/dist/runtime/workflowExecutor.d.ts.map +1 -0
- package/dist/runtime/workflowExecutor.js +80 -0
- package/dist/runtime/workflowExecutor.js.map +1 -0
- package/dist/server/api/graphql.js +3 -3
- package/dist/server/api/graphql.js.map +1 -1
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -2
- package/dist/server/index.js.map +1 -1
- package/dist/shared/index.d.ts +7 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +23 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/shared/types/agent.types.d.ts +63 -0
- package/dist/shared/types/agent.types.d.ts.map +1 -0
- package/dist/shared/types/agent.types.js +18 -0
- package/dist/shared/types/agent.types.js.map +1 -0
- package/dist/shared/types/execution.types.d.ts +42 -0
- package/dist/shared/types/execution.types.d.ts.map +1 -0
- package/dist/shared/types/execution.types.js +7 -0
- package/dist/shared/types/execution.types.js.map +1 -0
- package/dist/shared/types/index.d.ts +7 -0
- package/dist/shared/types/index.d.ts.map +1 -0
- package/dist/shared/types/index.js +23 -0
- package/dist/shared/types/index.js.map +1 -0
- package/dist/shared/types/schema.types.d.ts +52 -0
- package/dist/shared/types/schema.types.d.ts.map +1 -0
- package/dist/shared/types/schema.types.js +7 -0
- package/dist/shared/types/schema.types.js.map +1 -0
- package/dist/shared/utils/index.d.ts +5 -0
- package/dist/shared/utils/index.d.ts.map +1 -0
- package/dist/shared/utils/index.js +21 -0
- package/dist/shared/utils/index.js.map +1 -0
- package/dist/shared/utils/validation.d.ts +42 -0
- package/dist/shared/utils/validation.d.ts.map +1 -0
- package/dist/shared/utils/validation.js +93 -0
- package/dist/shared/utils/validation.js.map +1 -0
- package/dist/tenant/TenantManager.d.ts +152 -0
- package/dist/tenant/TenantManager.d.ts.map +1 -0
- package/dist/tenant/TenantManager.js +392 -0
- package/dist/tenant/TenantManager.js.map +1 -0
- package/dist/tenant/index.d.ts +47 -0
- package/dist/tenant/index.d.ts.map +1 -0
- package/dist/tenant/index.js +74 -0
- package/dist/tenant/index.js.map +1 -0
- package/dist/tenant/interfaces.d.ts +170 -0
- package/dist/tenant/interfaces.d.ts.map +1 -0
- package/dist/tenant/interfaces.js +67 -0
- package/dist/tenant/interfaces.js.map +1 -0
- package/dist/tenant/providerFactory.d.ts +43 -0
- package/dist/tenant/providerFactory.d.ts.map +1 -0
- package/dist/tenant/providerFactory.js +70 -0
- package/dist/tenant/providerFactory.js.map +1 -0
- package/dist/tenant/providerRegistry.d.ts +47 -0
- package/dist/tenant/providerRegistry.d.ts.map +1 -0
- package/dist/tenant/providerRegistry.js +79 -0
- package/dist/tenant/providerRegistry.js.map +1 -0
- package/dist/tenant/providers/FirebaseTenantProvider.d.ts +41 -0
- package/dist/tenant/providers/FirebaseTenantProvider.d.ts.map +1 -0
- package/dist/tenant/providers/FirebaseTenantProvider.js +290 -0
- package/dist/tenant/providers/FirebaseTenantProvider.js.map +1 -0
- package/dist/tenant/providers/InMemoryTenantProvider.d.ts +18 -0
- package/dist/tenant/providers/InMemoryTenantProvider.d.ts.map +1 -0
- package/dist/tenant/providers/InMemoryTenantProvider.js +137 -0
- package/dist/tenant/providers/InMemoryTenantProvider.js.map +1 -0
- package/package.json +46 -10
- package/src/agents/chat/chat.handler.ts +211 -0
- package/src/agents/chat/chat.schema.ts +33 -0
- package/src/agents/chat/chat.types.ts +46 -0
- package/src/agents/chat/chat.yaml +148 -0
- package/src/agents/chat/index.ts +21 -0
- package/src/agents/chromadb/chromadb.handler.ts +130 -0
- package/src/agents/chromadb/chromadb.schema.ts +35 -0
- package/src/agents/chromadb/chromadb.types.ts +52 -0
- package/src/agents/chromadb/chromadb.yaml +128 -0
- package/src/agents/chromadb/index.ts +20 -0
- package/src/agents/gemini-vectorize/gemini-vectorize.handler.ts +75 -0
- package/src/agents/gemini-vectorize/gemini-vectorize.schema.ts +22 -0
- package/src/agents/gemini-vectorize/gemini-vectorize.types.ts +34 -0
- package/src/agents/gemini-vectorize/gemini-vectorize.yaml +84 -0
- package/src/agents/gemini-vectorize/index.ts +20 -0
- package/src/agents/gitmcp/gitmcp.handler.ts +122 -0
- package/src/agents/gitmcp/gitmcp.schema.ts +20 -0
- package/src/agents/gitmcp/gitmcp.types.ts +33 -0
- package/src/agents/gitmcp/gitmcp.yaml +71 -0
- package/src/agents/gitmcp/index.ts +21 -0
- package/src/agents/image/image.handler.ts +86 -0
- package/src/agents/image/image.schema.ts +28 -0
- package/src/agents/image/image.types.ts +46 -0
- package/src/agents/image/image.yaml +86 -0
- package/src/agents/image/index.ts +19 -0
- package/src/agents/index.ts +63 -0
- package/src/agents/joker/index.ts +19 -0
- package/src/agents/joker/joker.handler.ts +55 -0
- package/src/agents/joker/joker.schema.ts +15 -0
- package/src/agents/joker/joker.types.ts +38 -0
- package/src/agents/joker/joker.yaml +47 -0
- package/src/agents/llm/index.ts +20 -0
- package/src/agents/llm/llm.handler.ts +82 -0
- package/src/agents/llm/llm.schema.ts +25 -0
- package/src/agents/llm/llm.types.ts +37 -0
- package/src/agents/llm/llm.yaml +87 -0
- package/src/agents/mcp-tool/index.ts +19 -0
- package/src/agents/mcp-tool/mcp-tool.handler.ts +112 -0
- package/src/agents/mcp-tool/mcp-tool.schema.ts +22 -0
- package/src/agents/mcp-tool/mcp-tool.types.ts +33 -0
- package/src/agents/mcp-tool/mcp-tool.yaml +71 -0
- package/src/agents/rag/index.ts +20 -0
- package/src/agents/rag/rag.handler.ts +95 -0
- package/src/agents/rag/rag.schema.ts +26 -0
- package/src/agents/rag/rag.types.ts +45 -0
- package/src/agents/rag/rag.yaml +89 -0
- package/src/agents/{agentRegistry.ts → registry/agentRegistry.ts} +195 -6
- package/src/agents/registry/index.ts +6 -0
- package/src/agents/translator/index.ts +19 -0
- package/src/agents/translator/translator.handler.ts +97 -0
- package/src/agents/translator/translator.schema.ts +30 -0
- package/src/agents/translator/translator.types.ts +43 -0
- package/src/agents/translator/translator.yaml +80 -0
- package/src/client/index.ts +53 -0
- package/src/client/types.ts +38 -0
- package/src/index.ts +56 -5
- package/src/runtime/declarativeAgentRuntime.ts +378 -495
- package/src/runtime/index.ts +36 -0
- package/src/runtime/llmProviderFactory.ts +145 -0
- package/src/runtime/workflowExecutor.ts +107 -0
- package/src/server/api/graphql.ts +1 -1
- package/src/server/index.ts +2 -2
- package/src/shared/index.ts +7 -0
- package/src/shared/types/agent.types.ts +100 -0
- package/src/shared/types/execution.types.ts +45 -0
- package/src/shared/types/index.ts +7 -0
- package/src/shared/types/schema.types.ts +55 -0
- package/src/shared/utils/index.ts +5 -0
- package/src/shared/utils/validation.ts +100 -0
- package/src/tenant/TenantManager.ts +488 -0
- package/src/tenant/index.ts +101 -0
- package/src/tenant/interfaces.ts +231 -0
- package/src/tenant/providerFactory.ts +75 -0
- package/src/tenant/providerRegistry.ts +86 -0
- package/src/tenant/providers/InMemoryTenantProvider.ts +168 -0
- package/dist/agents/agentRegistry.d.ts.map +0 -1
- package/dist/agents/agentRegistry.js.map +0 -1
- package/src/firebase/tenantManager.ts +0 -443
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Declarative Agent Runtime - YAML Interpreter for Beddel Declarative Protocol
|
|
3
3
|
* Safely interprets declarative YAML agent definitions without dynamic code execution
|
|
4
|
+
*
|
|
5
|
+
* Phase 3 Refactored: Delegates to individual agent handlers via workflowExecutor
|
|
4
6
|
*/
|
|
5
7
|
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
import
|
|
9
|
-
import { type ZodTypeAny } from
|
|
10
|
-
import { ExecutionContext } from
|
|
11
|
-
import { agentRegistry } from
|
|
8
|
+
import 'server-only';
|
|
9
|
+
|
|
10
|
+
import * as yaml from 'js-yaml';
|
|
11
|
+
import { type ZodTypeAny } from 'zod';
|
|
12
|
+
import { ExecutionContext } from '../types/executionContext';
|
|
13
|
+
import { agentRegistry } from '../agents/registry';
|
|
12
14
|
import {
|
|
13
15
|
DeclarativeSchemaCompiler,
|
|
14
16
|
DeclarativeSchemaValidationError,
|
|
15
17
|
type DeclarativeSchemaPhase,
|
|
16
|
-
} from
|
|
18
|
+
} from './schemaCompiler';
|
|
19
|
+
|
|
20
|
+
// Import handlers from workflowExecutor
|
|
21
|
+
import {
|
|
22
|
+
executeJokeHandler,
|
|
23
|
+
executeTranslationHandler,
|
|
24
|
+
executeImageHandler,
|
|
25
|
+
executeMcpToolHandler,
|
|
26
|
+
executeVectorizeHandler,
|
|
27
|
+
executeChromaDBHandler,
|
|
28
|
+
executeGitMcpHandler,
|
|
29
|
+
executeRagHandler,
|
|
30
|
+
executeChatHandler,
|
|
31
|
+
} from './workflowExecutor';
|
|
17
32
|
|
|
18
33
|
export interface YamlAgentDefinition {
|
|
19
34
|
agent: {
|
|
@@ -63,14 +78,12 @@ export type YamlExecutionResult = Record<string, any>;
|
|
|
63
78
|
|
|
64
79
|
/**
|
|
65
80
|
* Safe declarative YAML interpreter - no dynamic code execution
|
|
81
|
+
* Delegates execution to individual agent handlers
|
|
66
82
|
*/
|
|
67
83
|
export class DeclarativeAgentInterpreter {
|
|
68
|
-
private readonly MAX_VARIABLE_SIZE = 1024;
|
|
69
|
-
private readonly MAX_WORKFLOW_STEPS = 100;
|
|
70
|
-
private readonly MAX_OUTPUT_SIZE = 5 * 1024 * 1024;
|
|
71
|
-
private readonly GEMINI_MODEL = "models/gemini-2.5-flash";
|
|
72
|
-
private readonly GEMINI_IMAGE_MODEL = "imagen-4.0-fast-generate-001";
|
|
73
|
-
private readonly SUPPORTED_TRANSLATION_LANGUAGES = ["pt", "en", "es", "fr"];
|
|
84
|
+
private readonly MAX_VARIABLE_SIZE = 1024;
|
|
85
|
+
private readonly MAX_WORKFLOW_STEPS = 100;
|
|
86
|
+
private readonly MAX_OUTPUT_SIZE = 5 * 1024 * 1024;
|
|
74
87
|
private readonly schemaCompiler = new DeclarativeSchemaCompiler();
|
|
75
88
|
|
|
76
89
|
/**
|
|
@@ -82,16 +95,14 @@ export class DeclarativeAgentInterpreter {
|
|
|
82
95
|
const startTime = Date.now();
|
|
83
96
|
|
|
84
97
|
try {
|
|
85
|
-
// Parse and validate YAML
|
|
86
98
|
const agent = this.parseYaml(options.yamlContent);
|
|
87
99
|
this.validateAgentDefinition(agent);
|
|
88
100
|
|
|
89
|
-
// Compile schemas and validate input up front
|
|
90
101
|
const schemas = this.buildSchemaSet(agent);
|
|
91
102
|
const validatedInput = this.validateAgainstSchema(
|
|
92
103
|
options.input,
|
|
93
104
|
schemas.input,
|
|
94
|
-
|
|
105
|
+
'input',
|
|
95
106
|
options.context
|
|
96
107
|
);
|
|
97
108
|
|
|
@@ -100,14 +111,12 @@ export class DeclarativeAgentInterpreter {
|
|
|
100
111
|
input: validatedInput,
|
|
101
112
|
};
|
|
102
113
|
|
|
103
|
-
// Execute declarative logic
|
|
104
114
|
const result = await this.executeWorkflow(agent, executionOptions);
|
|
105
115
|
|
|
106
|
-
// Validate output
|
|
107
116
|
const validatedOutput = this.validateAgainstSchema(
|
|
108
117
|
result,
|
|
109
118
|
schemas.output,
|
|
110
|
-
|
|
119
|
+
'output',
|
|
111
120
|
options.context
|
|
112
121
|
);
|
|
113
122
|
this.enforceOutputSize(validatedOutput);
|
|
@@ -117,30 +126,24 @@ export class DeclarativeAgentInterpreter {
|
|
|
117
126
|
|
|
118
127
|
return validatedOutput;
|
|
119
128
|
} catch (error) {
|
|
120
|
-
const executionTime = Date.now() - startTime;
|
|
121
129
|
options.context.log(`Declarative agent execution failed: ${error}`);
|
|
122
130
|
options.context.setError(
|
|
123
|
-
error instanceof Error
|
|
124
|
-
? error.message
|
|
125
|
-
: "Unknown declarative agent error"
|
|
131
|
+
error instanceof Error ? error.message : 'Unknown declarative agent error'
|
|
126
132
|
);
|
|
127
133
|
throw error;
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
/**
|
|
132
|
-
* Parse and validate YAML content
|
|
133
|
-
*/
|
|
134
137
|
private parseYaml(yamlContent: string): YamlAgentDefinition {
|
|
135
138
|
try {
|
|
136
139
|
const parsed = yaml.load(yamlContent) as YamlAgentDefinition;
|
|
137
140
|
|
|
138
|
-
if (!parsed || typeof parsed !==
|
|
139
|
-
throw new Error(
|
|
141
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
142
|
+
throw new Error('Invalid YAML: expected object');
|
|
140
143
|
}
|
|
141
144
|
|
|
142
145
|
if (!parsed.agent || !parsed.logic || !parsed.schema) {
|
|
143
|
-
throw new Error(
|
|
146
|
+
throw new Error('Invalid agent definition: missing required sections');
|
|
144
147
|
}
|
|
145
148
|
|
|
146
149
|
return parsed;
|
|
@@ -149,32 +152,21 @@ export class DeclarativeAgentInterpreter {
|
|
|
149
152
|
}
|
|
150
153
|
}
|
|
151
154
|
|
|
152
|
-
/**
|
|
153
|
-
* Validate agent definition structure
|
|
154
|
-
*/
|
|
155
155
|
private validateAgentDefinition(agent: YamlAgentDefinition): void {
|
|
156
|
-
|
|
157
|
-
if (agent.agent.protocol !== "beddel-declarative-protocol/v2.0") {
|
|
156
|
+
if (agent.agent.protocol !== 'beddel-declarative-protocol/v2.0') {
|
|
158
157
|
throw new Error(`Unsupported protocol: ${agent.agent.protocol}`);
|
|
159
158
|
}
|
|
160
159
|
|
|
161
|
-
// Validate schema
|
|
162
160
|
if (!agent.schema.input || !agent.schema.output) {
|
|
163
|
-
throw new Error(
|
|
161
|
+
throw new Error('Invalid schema: missing input or output definition');
|
|
164
162
|
}
|
|
165
163
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
!Array.isArray(agent.logic.workflow) ||
|
|
169
|
-
agent.logic.workflow.length === 0
|
|
170
|
-
) {
|
|
171
|
-
throw new Error("Invalid workflow: must be non-empty array");
|
|
164
|
+
if (!Array.isArray(agent.logic.workflow) || agent.logic.workflow.length === 0) {
|
|
165
|
+
throw new Error('Invalid workflow: must be non-empty array');
|
|
172
166
|
}
|
|
173
167
|
|
|
174
168
|
if (agent.logic.workflow.length > this.MAX_WORKFLOW_STEPS) {
|
|
175
|
-
throw new Error(
|
|
176
|
-
`Workflow too complex: max ${this.MAX_WORKFLOW_STEPS} steps allowed`
|
|
177
|
-
);
|
|
169
|
+
throw new Error(`Workflow too complex: max ${this.MAX_WORKFLOW_STEPS} steps allowed`);
|
|
178
170
|
}
|
|
179
171
|
}
|
|
180
172
|
|
|
@@ -183,8 +175,8 @@ export class DeclarativeAgentInterpreter {
|
|
|
183
175
|
output: ZodTypeAny;
|
|
184
176
|
} {
|
|
185
177
|
return {
|
|
186
|
-
input: this.schemaCompiler.compile(agent.schema.input,
|
|
187
|
-
output: this.schemaCompiler.compile(agent.schema.output,
|
|
178
|
+
input: this.schemaCompiler.compile(agent.schema.input, 'schema.input'),
|
|
179
|
+
output: this.schemaCompiler.compile(agent.schema.output, 'schema.output'),
|
|
188
180
|
};
|
|
189
181
|
}
|
|
190
182
|
|
|
@@ -198,29 +190,23 @@ export class DeclarativeAgentInterpreter {
|
|
|
198
190
|
if (!validationResult.success) {
|
|
199
191
|
const issues = validationResult.error.issues;
|
|
200
192
|
const issueSummary = issues
|
|
201
|
-
.map((issue) => `${issue.path.join(
|
|
202
|
-
.join(
|
|
203
|
-
const label = phase ===
|
|
193
|
+
.map((issue) => `${issue.path.join('.') || 'root'}: ${issue.message}`)
|
|
194
|
+
.join('; ');
|
|
195
|
+
const label = phase === 'input' ? 'Input' : 'Output';
|
|
204
196
|
const message = `${label} validation failed: ${issueSummary}`;
|
|
205
197
|
context.setError(message);
|
|
206
198
|
throw new DeclarativeSchemaValidationError(message, phase, issues);
|
|
207
199
|
}
|
|
208
|
-
|
|
209
200
|
return validationResult.data;
|
|
210
201
|
}
|
|
211
202
|
|
|
212
203
|
private enforceOutputSize(output: any): void {
|
|
213
204
|
const outputSize = JSON.stringify(output).length;
|
|
214
205
|
if (outputSize > this.MAX_OUTPUT_SIZE) {
|
|
215
|
-
throw new Error(
|
|
216
|
-
`Output size exceeds maximum allowed: ${outputSize} > ${this.MAX_OUTPUT_SIZE}`
|
|
217
|
-
);
|
|
206
|
+
throw new Error(`Output size exceeds maximum allowed: ${outputSize} > ${this.MAX_OUTPUT_SIZE}`);
|
|
218
207
|
}
|
|
219
208
|
}
|
|
220
209
|
|
|
221
|
-
/**
|
|
222
|
-
* Execute declarative workflow
|
|
223
|
-
*/
|
|
224
210
|
private async executeWorkflow(
|
|
225
211
|
agent: YamlAgentDefinition,
|
|
226
212
|
options: YamlAgentInterpreterOptions
|
|
@@ -228,7 +214,6 @@ export class DeclarativeAgentInterpreter {
|
|
|
228
214
|
const variables = new Map<string, any>();
|
|
229
215
|
let output: any = undefined;
|
|
230
216
|
|
|
231
|
-
// Initialize variables
|
|
232
217
|
if (agent.logic.variables) {
|
|
233
218
|
for (const variable of agent.logic.variables) {
|
|
234
219
|
this.validateVariable(variable);
|
|
@@ -237,7 +222,6 @@ export class DeclarativeAgentInterpreter {
|
|
|
237
222
|
}
|
|
238
223
|
}
|
|
239
224
|
|
|
240
|
-
// Execute workflow steps
|
|
241
225
|
for (const step of agent.logic.workflow) {
|
|
242
226
|
output = await this.executeWorkflowStep(step, variables, options);
|
|
243
227
|
}
|
|
@@ -245,9 +229,6 @@ export class DeclarativeAgentInterpreter {
|
|
|
245
229
|
return output;
|
|
246
230
|
}
|
|
247
231
|
|
|
248
|
-
/**
|
|
249
|
-
* Execute single workflow step
|
|
250
|
-
*/
|
|
251
232
|
private async executeWorkflowStep(
|
|
252
233
|
step: any,
|
|
253
234
|
variables: Map<string, any>,
|
|
@@ -256,53 +237,65 @@ export class DeclarativeAgentInterpreter {
|
|
|
256
237
|
options.context.log(`Executing workflow step: ${step.name} (${step.type})`);
|
|
257
238
|
|
|
258
239
|
switch (step.type) {
|
|
259
|
-
case
|
|
240
|
+
case 'output-generator':
|
|
260
241
|
return this.executeOutputGenerator(step, variables, options);
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
case
|
|
264
|
-
return this.
|
|
265
|
-
case
|
|
266
|
-
|
|
267
|
-
|
|
242
|
+
// Preferred step types
|
|
243
|
+
case 'joke':
|
|
244
|
+
case 'genkit-joke':
|
|
245
|
+
return this.executeJoke(step, variables, options);
|
|
246
|
+
case 'translation':
|
|
247
|
+
case 'genkit-translation':
|
|
248
|
+
return this.executeTranslation(step, variables, options);
|
|
249
|
+
case 'image':
|
|
250
|
+
case 'genkit-image':
|
|
251
|
+
return this.executeImage(step, variables, options);
|
|
252
|
+
case 'vectorize':
|
|
253
|
+
case 'gemini-vectorize':
|
|
254
|
+
return this.executeVectorize(step, variables, options);
|
|
255
|
+
case 'custom-action':
|
|
268
256
|
return this.executeCustomAction(step, variables, options);
|
|
257
|
+
case 'mcp-tool':
|
|
258
|
+
return this.executeMcpTool(step, variables, options);
|
|
259
|
+
case 'chromadb':
|
|
260
|
+
return this.executeChromaDB(step, variables, options);
|
|
261
|
+
case 'gitmcp':
|
|
262
|
+
return this.executeGitMcp(step, variables, options);
|
|
263
|
+
case 'rag':
|
|
264
|
+
return this.executeRag(step, variables, options);
|
|
265
|
+
case 'chat':
|
|
266
|
+
return this.executeChat(step, variables, options);
|
|
267
|
+
case 'builtin-agent':
|
|
268
|
+
return this.executeBuiltinAgent(step, variables, options);
|
|
269
269
|
default:
|
|
270
270
|
throw new Error(`Unsupported workflow step type: ${step.type}`);
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
274
|
+
// ============================================================================
|
|
275
|
+
// Output Generator (kept inline - orchestration logic)
|
|
276
|
+
// ============================================================================
|
|
277
|
+
|
|
277
278
|
private executeOutputGenerator(
|
|
278
279
|
step: any,
|
|
279
280
|
variables: Map<string, any>,
|
|
280
281
|
options: YamlAgentInterpreterOptions
|
|
281
282
|
): any {
|
|
282
|
-
if (step.action?.type !==
|
|
283
|
-
throw new Error(
|
|
283
|
+
if (step.action?.type !== 'generate' || !step.action.output) {
|
|
284
|
+
throw new Error('Invalid output generator configuration');
|
|
284
285
|
}
|
|
285
286
|
|
|
286
|
-
// Build output object
|
|
287
287
|
const output: any = {};
|
|
288
288
|
|
|
289
|
-
// Debug: Log available variables
|
|
290
289
|
options.context.log(
|
|
291
|
-
`Output generator: Available variables: ${Array.from(variables.keys()).join(
|
|
290
|
+
`Output generator: Available variables: ${Array.from(variables.keys()).join(', ')}`
|
|
292
291
|
);
|
|
293
292
|
|
|
294
293
|
for (const [key, valueExpr] of Object.entries(step.action.output)) {
|
|
295
|
-
if (typeof valueExpr ===
|
|
294
|
+
if (typeof valueExpr === 'string' && valueExpr.startsWith('$')) {
|
|
296
295
|
try {
|
|
297
296
|
const reference = valueExpr.substring(1);
|
|
298
|
-
options.context.log(
|
|
299
|
-
`Output generator: Resolving reference ${valueExpr} -> ${reference}`
|
|
300
|
-
);
|
|
301
297
|
const resolved = this.resolveReference(reference, variables);
|
|
302
298
|
output[key] = resolved;
|
|
303
|
-
options.context.log(
|
|
304
|
-
`Output generator: Resolved ${key} = ${typeof resolved === "string" ? resolved.substring(0, 50) + "..." : JSON.stringify(resolved).substring(0, 100)}`
|
|
305
|
-
);
|
|
306
299
|
} catch (error) {
|
|
307
300
|
options.context.log(
|
|
308
301
|
`Output generator: Failed to resolve ${valueExpr}: ${error instanceof Error ? error.message : String(error)}`
|
|
@@ -314,72 +307,62 @@ export class DeclarativeAgentInterpreter {
|
|
|
314
307
|
}
|
|
315
308
|
}
|
|
316
309
|
|
|
317
|
-
options.context.log(
|
|
318
|
-
`Output generator: Final output keys: ${Object.keys(output).join(", ")}`
|
|
319
|
-
);
|
|
320
|
-
|
|
321
310
|
return output;
|
|
322
311
|
}
|
|
323
312
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
313
|
+
// ============================================================================
|
|
314
|
+
// Delegated Handlers - Using extracted agent handlers
|
|
315
|
+
// ============================================================================
|
|
316
|
+
|
|
317
|
+
private async executeJoke(
|
|
328
318
|
step: any,
|
|
329
319
|
variables: Map<string, any>,
|
|
330
320
|
options: YamlAgentInterpreterOptions
|
|
331
321
|
): Promise<any> {
|
|
332
322
|
const prompt =
|
|
333
|
-
typeof step.action?.prompt ===
|
|
323
|
+
typeof step.action?.prompt === 'string' && step.action.prompt.trim().length
|
|
334
324
|
? step.action.prompt.trim()
|
|
335
|
-
:
|
|
325
|
+
: 'Tell a short and original joke that works for any audience.';
|
|
336
326
|
const temperature =
|
|
337
|
-
typeof step.action?.temperature ===
|
|
338
|
-
? step.action.temperature
|
|
339
|
-
: 0.8;
|
|
327
|
+
typeof step.action?.temperature === 'number' ? step.action.temperature : 0.8;
|
|
340
328
|
const maxTokens =
|
|
341
|
-
typeof step.action?.maxTokens ===
|
|
342
|
-
? step.action.maxTokens
|
|
343
|
-
: undefined;
|
|
329
|
+
typeof step.action?.maxTokens === 'number' ? step.action.maxTokens : undefined;
|
|
344
330
|
const resultVar =
|
|
345
|
-
typeof step.action?.result ===
|
|
331
|
+
typeof step.action?.result === 'string' && step.action.result.length > 0
|
|
346
332
|
? step.action.result
|
|
347
|
-
:
|
|
333
|
+
: 'jokerResult';
|
|
348
334
|
|
|
349
|
-
const
|
|
335
|
+
const result = await executeJokeHandler(
|
|
350
336
|
{ prompt, temperature, maxTokens },
|
|
351
337
|
options.props,
|
|
352
338
|
options.context
|
|
353
339
|
);
|
|
354
340
|
|
|
355
|
-
variables.set(resultVar,
|
|
356
|
-
return
|
|
341
|
+
variables.set(resultVar, result);
|
|
342
|
+
return result;
|
|
357
343
|
}
|
|
358
344
|
|
|
359
|
-
|
|
360
|
-
* Execute translation step backed by Gemini Flash
|
|
361
|
-
*/
|
|
362
|
-
private async executeGenkitTranslation(
|
|
345
|
+
private async executeTranslation(
|
|
363
346
|
step: any,
|
|
364
347
|
variables: Map<string, any>,
|
|
365
348
|
options: YamlAgentInterpreterOptions
|
|
366
349
|
): Promise<any> {
|
|
367
|
-
const
|
|
368
|
-
const
|
|
369
|
-
const
|
|
350
|
+
const text = options.input?.texto || options.input?.text;
|
|
351
|
+
const sourceLanguage = options.input?.idioma_origem || options.input?.source_language;
|
|
352
|
+
const targetLanguage = options.input?.idioma_destino || options.input?.target_language;
|
|
370
353
|
|
|
371
354
|
const resultVar =
|
|
372
|
-
typeof step.action?.result ===
|
|
355
|
+
typeof step.action?.result === 'string' && step.action.result.length > 0
|
|
373
356
|
? step.action.result
|
|
374
|
-
:
|
|
357
|
+
: 'translationResult';
|
|
375
358
|
|
|
376
|
-
const
|
|
359
|
+
const result = await executeTranslationHandler(
|
|
377
360
|
{
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
361
|
+
text,
|
|
362
|
+
source_language: sourceLanguage,
|
|
363
|
+
target_language: targetLanguage,
|
|
381
364
|
promptTemplate:
|
|
382
|
-
typeof step.action?.promptTemplate ===
|
|
365
|
+
typeof step.action?.promptTemplate === 'string'
|
|
383
366
|
? step.action.promptTemplate
|
|
384
367
|
: undefined,
|
|
385
368
|
},
|
|
@@ -387,80 +370,228 @@ export class DeclarativeAgentInterpreter {
|
|
|
387
370
|
options.context
|
|
388
371
|
);
|
|
389
372
|
|
|
390
|
-
variables.set(resultVar,
|
|
391
|
-
return
|
|
373
|
+
variables.set(resultVar, result);
|
|
374
|
+
return result;
|
|
392
375
|
}
|
|
393
376
|
|
|
394
|
-
|
|
395
|
-
* Execute image generation step backed by Gemini Flash
|
|
396
|
-
*/
|
|
397
|
-
private async executeGenkitImage(
|
|
377
|
+
private async executeImage(
|
|
398
378
|
step: any,
|
|
399
379
|
variables: Map<string, any>,
|
|
400
380
|
options: YamlAgentInterpreterOptions
|
|
401
381
|
): Promise<any> {
|
|
402
|
-
const
|
|
403
|
-
typeof options.input?.descricao ===
|
|
382
|
+
const description =
|
|
383
|
+
typeof options.input?.descricao === 'string'
|
|
404
384
|
? options.input.descricao.trim()
|
|
405
|
-
:
|
|
406
|
-
|
|
407
|
-
|
|
385
|
+
: typeof options.input?.description === 'string'
|
|
386
|
+
? options.input.description.trim()
|
|
387
|
+
: '';
|
|
388
|
+
const style =
|
|
389
|
+
typeof options.input?.estilo === 'string'
|
|
408
390
|
? options.input.estilo.trim()
|
|
409
|
-
:
|
|
410
|
-
|
|
411
|
-
|
|
391
|
+
: typeof options.input?.style === 'string'
|
|
392
|
+
? options.input.style.trim()
|
|
393
|
+
: '';
|
|
394
|
+
const resolution =
|
|
395
|
+
typeof options.input?.resolucao === 'string'
|
|
412
396
|
? options.input.resolucao.trim()
|
|
413
|
-
:
|
|
397
|
+
: typeof options.input?.resolution === 'string'
|
|
398
|
+
? options.input.resolution.trim()
|
|
399
|
+
: '';
|
|
414
400
|
|
|
415
|
-
if (!
|
|
416
|
-
throw new Error(
|
|
401
|
+
if (!description) {
|
|
402
|
+
throw new Error('Missing required image input: description');
|
|
417
403
|
}
|
|
418
|
-
if (!
|
|
419
|
-
throw new Error(
|
|
404
|
+
if (!style) {
|
|
405
|
+
throw new Error('Missing required image input: style');
|
|
420
406
|
}
|
|
421
|
-
if (!
|
|
422
|
-
throw new Error(
|
|
407
|
+
if (!resolution) {
|
|
408
|
+
throw new Error('Missing required image input: resolution');
|
|
423
409
|
}
|
|
424
410
|
|
|
425
411
|
const promptTemplate =
|
|
426
|
-
typeof step.action?.promptTemplate ===
|
|
427
|
-
|
|
412
|
+
typeof step.action?.promptTemplate === 'string' &&
|
|
413
|
+
step.action.promptTemplate.trim().length > 0
|
|
428
414
|
? step.action.promptTemplate
|
|
429
|
-
:
|
|
430
|
-
|
|
431
|
-
const prompt = promptTemplate
|
|
432
|
-
.replace(/{{descricao}}/g, descricao)
|
|
433
|
-
.replace(/{{estilo}}/g, estilo)
|
|
434
|
-
.trim();
|
|
415
|
+
: 'Create a detailed image in {{style}} style focusing on: {{description}}';
|
|
435
416
|
|
|
436
417
|
const resultVar =
|
|
437
|
-
typeof step.action?.result ===
|
|
418
|
+
typeof step.action?.result === 'string' && step.action.result.length > 0
|
|
438
419
|
? step.action.result
|
|
439
|
-
:
|
|
420
|
+
: 'imageResult';
|
|
440
421
|
|
|
441
|
-
const
|
|
422
|
+
const result = await executeImageHandler(
|
|
442
423
|
{
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
424
|
+
description,
|
|
425
|
+
style,
|
|
426
|
+
resolution,
|
|
427
|
+
promptTemplate,
|
|
446
428
|
},
|
|
447
429
|
options.props,
|
|
448
430
|
options.context
|
|
449
431
|
);
|
|
450
432
|
|
|
451
|
-
variables.set(resultVar,
|
|
452
|
-
|
|
453
|
-
|
|
433
|
+
variables.set(resultVar, result);
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
private async executeMcpTool(
|
|
438
|
+
step: any,
|
|
439
|
+
variables: Map<string, any>,
|
|
440
|
+
options: YamlAgentInterpreterOptions
|
|
441
|
+
): Promise<any> {
|
|
442
|
+
const serverUrl = this.resolveInputValue(step.action?.server_url, options.input, variables);
|
|
443
|
+
const toolName = this.resolveInputValue(step.action?.tool_name, options.input, variables);
|
|
444
|
+
const toolArguments =
|
|
445
|
+
this.resolveInputValue(step.action?.tool_arguments, options.input, variables) || {};
|
|
446
|
+
const resultVar = step.action?.result || 'mcpResult';
|
|
447
|
+
|
|
448
|
+
const result = await executeMcpToolHandler(
|
|
449
|
+
{
|
|
450
|
+
server_url: serverUrl,
|
|
451
|
+
tool_name: toolName,
|
|
452
|
+
tool_arguments: toolArguments,
|
|
453
|
+
},
|
|
454
|
+
options.props,
|
|
455
|
+
options.context
|
|
454
456
|
);
|
|
455
|
-
|
|
456
|
-
|
|
457
|
+
|
|
458
|
+
variables.set(resultVar, result);
|
|
459
|
+
return result;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private async executeVectorize(
|
|
463
|
+
step: any,
|
|
464
|
+
variables: Map<string, any>,
|
|
465
|
+
options: YamlAgentInterpreterOptions
|
|
466
|
+
): Promise<any> {
|
|
467
|
+
const action = step.action?.action || 'embedSingle';
|
|
468
|
+
const resultVar = step.action?.result || 'vectorizeResult';
|
|
469
|
+
|
|
470
|
+
let result;
|
|
471
|
+
if (action === 'embedSingle') {
|
|
472
|
+
const text = this.resolveInputValue(step.action?.text, options.input, variables);
|
|
473
|
+
result = await executeVectorizeHandler(
|
|
474
|
+
{ action: 'embedSingle', text },
|
|
475
|
+
options.props,
|
|
476
|
+
options.context
|
|
477
|
+
);
|
|
478
|
+
} else if (action === 'embedBatch') {
|
|
479
|
+
const texts = this.resolveInputValue(step.action?.texts, options.input, variables);
|
|
480
|
+
result = await executeVectorizeHandler(
|
|
481
|
+
{ action: 'embedBatch', texts },
|
|
482
|
+
options.props,
|
|
483
|
+
options.context
|
|
484
|
+
);
|
|
485
|
+
} else {
|
|
486
|
+
throw new Error(`Unknown vectorize action: ${action}`);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
variables.set(resultVar, result);
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private async executeChromaDB(
|
|
494
|
+
step: any,
|
|
495
|
+
variables: Map<string, any>,
|
|
496
|
+
options: YamlAgentInterpreterOptions
|
|
497
|
+
): Promise<any> {
|
|
498
|
+
const action = this.resolveInputValue(step.action?.action, options.input, variables);
|
|
499
|
+
const collectionName = this.resolveInputValue(
|
|
500
|
+
step.action?.collection_name,
|
|
501
|
+
options.input,
|
|
502
|
+
variables
|
|
457
503
|
);
|
|
458
|
-
|
|
504
|
+
const resultVar = step.action?.result || 'chromaResult';
|
|
505
|
+
|
|
506
|
+
let params: any = { action, collection_name: collectionName };
|
|
507
|
+
|
|
508
|
+
if (action === 'hasData') {
|
|
509
|
+
params.min_count =
|
|
510
|
+
this.resolveInputValue(step.action?.min_count, options.input, variables) || 1;
|
|
511
|
+
} else if (action === 'store') {
|
|
512
|
+
params.ids = this.resolveInputValue(step.action?.ids, options.input, variables);
|
|
513
|
+
params.vectors = this.resolveInputValue(step.action?.vectors, options.input, variables);
|
|
514
|
+
params.documents = this.resolveInputValue(step.action?.documents, options.input, variables);
|
|
515
|
+
params.metadatas = this.resolveInputValue(step.action?.metadatas, options.input, variables);
|
|
516
|
+
} else if (action === 'search') {
|
|
517
|
+
params.query_vector = this.resolveInputValue(
|
|
518
|
+
step.action?.query_vector,
|
|
519
|
+
options.input,
|
|
520
|
+
variables
|
|
521
|
+
);
|
|
522
|
+
params.limit =
|
|
523
|
+
this.resolveInputValue(step.action?.limit, options.input, variables) || 5;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const result = await executeChromaDBHandler(params, options.props, options.context);
|
|
527
|
+
|
|
528
|
+
variables.set(resultVar, result);
|
|
529
|
+
return result;
|
|
459
530
|
}
|
|
460
531
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
532
|
+
private async executeGitMcp(
|
|
533
|
+
step: any,
|
|
534
|
+
variables: Map<string, any>,
|
|
535
|
+
options: YamlAgentInterpreterOptions
|
|
536
|
+
): Promise<any> {
|
|
537
|
+
const gitmcpUrl = this.resolveInputValue(step.action?.gitmcp_url, options.input, variables);
|
|
538
|
+
const resultVar = step.action?.result || 'gitmcpResult';
|
|
539
|
+
|
|
540
|
+
const result = await executeGitMcpHandler(
|
|
541
|
+
{ gitmcp_url: gitmcpUrl },
|
|
542
|
+
options.props,
|
|
543
|
+
options.context
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
variables.set(resultVar, result);
|
|
547
|
+
return result;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
private async executeRag(
|
|
551
|
+
step: any,
|
|
552
|
+
variables: Map<string, any>,
|
|
553
|
+
options: YamlAgentInterpreterOptions
|
|
554
|
+
): Promise<any> {
|
|
555
|
+
const query = this.resolveInputValue(step.action?.query, options.input, variables);
|
|
556
|
+
const context =
|
|
557
|
+
this.resolveInputValue(step.action?.context, options.input, variables) ||
|
|
558
|
+
this.resolveInputValue(step.action?.documents, options.input, variables);
|
|
559
|
+
const history = this.resolveInputValue(step.action?.history, options.input, variables);
|
|
560
|
+
const resultVar = step.action?.result || 'ragResult';
|
|
561
|
+
|
|
562
|
+
const result = await executeRagHandler(
|
|
563
|
+
{ query, context, documents: context, history },
|
|
564
|
+
options.props,
|
|
565
|
+
options.context
|
|
566
|
+
);
|
|
567
|
+
|
|
568
|
+
variables.set(resultVar, result);
|
|
569
|
+
return result;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
private async executeChat(
|
|
573
|
+
step: any,
|
|
574
|
+
variables: Map<string, any>,
|
|
575
|
+
options: YamlAgentInterpreterOptions
|
|
576
|
+
): Promise<any> {
|
|
577
|
+
const messages = this.resolveInputValue(step.action?.messages, options.input, variables);
|
|
578
|
+
const query = this.resolveInputValue(step.action?.query, options.input, variables);
|
|
579
|
+
const resultVar = step.action?.result || 'chatResult';
|
|
580
|
+
|
|
581
|
+
const result = await executeChatHandler(
|
|
582
|
+
{ messages, query },
|
|
583
|
+
options.props,
|
|
584
|
+
options.context
|
|
585
|
+
);
|
|
586
|
+
|
|
587
|
+
variables.set(resultVar, result);
|
|
588
|
+
return result;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// ============================================================================
|
|
592
|
+
// Custom Action & Builtin Agent (orchestration logic)
|
|
593
|
+
// ============================================================================
|
|
594
|
+
|
|
464
595
|
private async executeCustomAction(
|
|
465
596
|
step: any,
|
|
466
597
|
variables: Map<string, any>,
|
|
@@ -473,16 +604,14 @@ export class DeclarativeAgentInterpreter {
|
|
|
473
604
|
|
|
474
605
|
options.context.log(`Custom action: Looking up function '${functionName}'`);
|
|
475
606
|
|
|
476
|
-
// Retrieve Code Implementation
|
|
477
607
|
const customFunc = agentRegistry.getCustomFunction(functionName);
|
|
478
608
|
if (!customFunc) {
|
|
479
609
|
throw new Error(
|
|
480
610
|
`Custom function '${functionName}' not found in registry. ` +
|
|
481
|
-
|
|
611
|
+
`Make sure the corresponding .ts file is in the /agents directory.`
|
|
482
612
|
);
|
|
483
613
|
}
|
|
484
614
|
|
|
485
|
-
// Prepare Arguments
|
|
486
615
|
const args = {
|
|
487
616
|
input: options.input,
|
|
488
617
|
variables: Object.fromEntries(variables),
|
|
@@ -492,16 +621,11 @@ export class DeclarativeAgentInterpreter {
|
|
|
492
621
|
|
|
493
622
|
options.context.log(`Custom action: Executing function '${functionName}'`);
|
|
494
623
|
|
|
495
|
-
// Execute Code
|
|
496
624
|
try {
|
|
497
625
|
const result = await customFunc(args);
|
|
498
626
|
|
|
499
|
-
// Save Result
|
|
500
627
|
if (step.action.result) {
|
|
501
628
|
variables.set(step.action.result, result);
|
|
502
|
-
options.context.log(
|
|
503
|
-
`Custom action: Saved result to variable '${step.action.result}'`
|
|
504
|
-
);
|
|
505
629
|
}
|
|
506
630
|
|
|
507
631
|
return result;
|
|
@@ -513,77 +637,129 @@ export class DeclarativeAgentInterpreter {
|
|
|
513
637
|
}
|
|
514
638
|
}
|
|
515
639
|
|
|
640
|
+
private async executeBuiltinAgent(
|
|
641
|
+
step: any,
|
|
642
|
+
variables: Map<string, any>,
|
|
643
|
+
options: YamlAgentInterpreterOptions
|
|
644
|
+
): Promise<any> {
|
|
645
|
+
const agentName = step.action?.agent;
|
|
646
|
+
const agentInput =
|
|
647
|
+
this.resolveInputValue(step.action?.input, options.input, variables) || options.input;
|
|
648
|
+
const agentProps = step.action?.props || options.props;
|
|
649
|
+
const resultVar = step.action?.result || 'builtinResult';
|
|
650
|
+
|
|
651
|
+
if (!agentName) {
|
|
652
|
+
throw new Error('Missing required builtin-agent input: agent');
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
options.context.log(`[Builtin Agent] Invoking agent: ${agentName}`);
|
|
656
|
+
|
|
657
|
+
try {
|
|
658
|
+
const result = await agentRegistry.executeAgent(
|
|
659
|
+
agentName,
|
|
660
|
+
agentInput,
|
|
661
|
+
agentProps,
|
|
662
|
+
options.context
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
variables.set(resultVar, result);
|
|
666
|
+
return result;
|
|
667
|
+
} catch (error: unknown) {
|
|
668
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
669
|
+
options.context.log(`[Builtin Agent] Error: ${message}`);
|
|
670
|
+
throw new Error(`Builtin agent '${agentName}' execution failed: ${message}`);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// ============================================================================
|
|
675
|
+
// Helper Methods
|
|
676
|
+
// ============================================================================
|
|
677
|
+
|
|
678
|
+
private resolveInputValue(
|
|
679
|
+
value: any,
|
|
680
|
+
input: Record<string, any>,
|
|
681
|
+
variables: Map<string, any>
|
|
682
|
+
): any {
|
|
683
|
+
if (value === undefined || value === null) return undefined;
|
|
684
|
+
|
|
685
|
+
if (typeof value === 'string' && value.startsWith('$')) {
|
|
686
|
+
const ref = value.substring(1);
|
|
687
|
+
if (ref.startsWith('input.')) {
|
|
688
|
+
const inputKey = ref.substring(6);
|
|
689
|
+
return this.getNestedValue(input, inputKey);
|
|
690
|
+
}
|
|
691
|
+
return this.resolveReference(ref, variables);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (typeof value === 'string' && input[value] !== undefined) {
|
|
695
|
+
return input[value];
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return value;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
private getNestedValue(obj: any, path: string): any {
|
|
702
|
+
const parts = path.split('.');
|
|
703
|
+
let current = obj;
|
|
704
|
+
for (const part of parts) {
|
|
705
|
+
if (current == null) return undefined;
|
|
706
|
+
current = current[part];
|
|
707
|
+
}
|
|
708
|
+
return current;
|
|
709
|
+
}
|
|
516
710
|
|
|
517
|
-
/**
|
|
518
|
-
* Evaluate value expression
|
|
519
|
-
*/
|
|
520
711
|
private evaluateValue(expr: string, variables: Map<string, any>): any {
|
|
521
|
-
// Handle string literals
|
|
522
712
|
if (expr.startsWith('"') && expr.endsWith('"')) {
|
|
523
713
|
if (expr.length - 2 > this.MAX_VARIABLE_SIZE) {
|
|
524
|
-
throw new Error(
|
|
714
|
+
throw new Error('Variable initialization exceeds maximum size');
|
|
525
715
|
}
|
|
526
716
|
return expr.slice(1, -1);
|
|
527
717
|
}
|
|
528
718
|
|
|
529
719
|
if (expr.startsWith("'") && expr.endsWith("'")) {
|
|
530
720
|
if (expr.length - 2 > this.MAX_VARIABLE_SIZE) {
|
|
531
|
-
throw new Error(
|
|
721
|
+
throw new Error('Variable initialization exceeds maximum size');
|
|
532
722
|
}
|
|
533
723
|
return expr.slice(1, -1);
|
|
534
724
|
}
|
|
535
725
|
|
|
536
726
|
if (expr.length > this.MAX_VARIABLE_SIZE) {
|
|
537
|
-
throw new Error(
|
|
727
|
+
throw new Error('Variable initialization exceeds maximum size');
|
|
538
728
|
}
|
|
539
729
|
|
|
540
|
-
|
|
541
|
-
if (expr ===
|
|
542
|
-
if (expr ===
|
|
543
|
-
|
|
544
|
-
// Handle null literal
|
|
545
|
-
if (expr === "null") return null;
|
|
730
|
+
if (expr === 'true') return true;
|
|
731
|
+
if (expr === 'false') return false;
|
|
732
|
+
if (expr === 'null') return null;
|
|
546
733
|
|
|
547
|
-
|
|
548
|
-
if (expr.startsWith("$")) {
|
|
734
|
+
if (expr.startsWith('$')) {
|
|
549
735
|
return this.resolveReference(expr.substring(1), variables);
|
|
550
736
|
}
|
|
551
737
|
|
|
552
|
-
// Handle numbers
|
|
553
738
|
if (/^-?\d+$/.test(expr)) return parseInt(expr, 10);
|
|
554
739
|
if (/^-?\d+\.\d+$/.test(expr)) return parseFloat(expr);
|
|
555
740
|
|
|
556
741
|
throw new Error(`Unsupported value expression: ${expr}`);
|
|
557
742
|
}
|
|
558
743
|
|
|
559
|
-
/**
|
|
560
|
-
* Validate variable declaration
|
|
561
|
-
*/
|
|
562
744
|
private validateVariable(variable: any): void {
|
|
563
745
|
if (!variable.name || !variable.type) {
|
|
564
|
-
throw new Error(
|
|
746
|
+
throw new Error('Invalid variable declaration: missing name or type');
|
|
565
747
|
}
|
|
566
748
|
|
|
567
|
-
if (![
|
|
749
|
+
if (!['string', 'number', 'boolean', 'object'].includes(variable.type)) {
|
|
568
750
|
throw new Error(`Unsupported variable type: ${variable.type}`);
|
|
569
751
|
}
|
|
570
752
|
}
|
|
571
753
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
*/
|
|
575
|
-
private resolveReference(
|
|
576
|
-
reference: string,
|
|
577
|
-
variables: Map<string, any>
|
|
578
|
-
): any {
|
|
579
|
-
const [varName, ...pathSegments] = reference.split(".");
|
|
754
|
+
private resolveReference(reference: string, variables: Map<string, any>): any {
|
|
755
|
+
const [varName, ...pathSegments] = reference.split('.');
|
|
580
756
|
let value = variables.get(varName);
|
|
581
757
|
if (value === undefined) {
|
|
582
758
|
throw new Error(`Undefined variable referenced: ${varName}`);
|
|
583
759
|
}
|
|
584
760
|
|
|
585
761
|
for (const segment of pathSegments) {
|
|
586
|
-
if (value == null || typeof value !==
|
|
762
|
+
if (value == null || typeof value !== 'object') {
|
|
587
763
|
throw new Error(
|
|
588
764
|
`Cannot resolve path '${reference}': segment '${segment}' is invalid`
|
|
589
765
|
);
|
|
@@ -593,299 +769,6 @@ export class DeclarativeAgentInterpreter {
|
|
|
593
769
|
|
|
594
770
|
return value;
|
|
595
771
|
}
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Ensure we have a Gemini API key before calling Genkit helpers
|
|
599
|
-
*/
|
|
600
|
-
private ensureGeminiApiKey(props: Record<string, string>): string {
|
|
601
|
-
const apiKey = props?.gemini_api_key?.trim();
|
|
602
|
-
if (!apiKey) {
|
|
603
|
-
throw new Error(
|
|
604
|
-
"Missing required prop: gemini_api_key. Configure a valid Gemini API key before calling this agent."
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
return apiKey;
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
private createGeminiModel(props: Record<string, string>) {
|
|
611
|
-
const apiKey = this.ensureGeminiApiKey(props);
|
|
612
|
-
const google = createGoogleGenerativeAI({ apiKey });
|
|
613
|
-
const model = google(this.GEMINI_MODEL);
|
|
614
|
-
return model;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
private createGeminiImageModel(props: Record<string, string>) {
|
|
618
|
-
const apiKey = this.ensureGeminiApiKey(props);
|
|
619
|
-
const google = createGoogleGenerativeAI({ apiKey });
|
|
620
|
-
return google.image(this.GEMINI_IMAGE_MODEL);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
private async callGeminiFlashText(
|
|
624
|
-
params: {
|
|
625
|
-
prompt: string;
|
|
626
|
-
temperature?: number;
|
|
627
|
-
maxTokens?: number;
|
|
628
|
-
},
|
|
629
|
-
props: Record<string, string>,
|
|
630
|
-
context: ExecutionContext
|
|
631
|
-
): Promise<{
|
|
632
|
-
texto: string;
|
|
633
|
-
metadados: {
|
|
634
|
-
modelo_utilizado: string;
|
|
635
|
-
tempo_processamento: number;
|
|
636
|
-
temperature: number;
|
|
637
|
-
max_tokens: number | null;
|
|
638
|
-
prompt_utilizado: string;
|
|
639
|
-
};
|
|
640
|
-
}> {
|
|
641
|
-
const prompt = params.prompt?.trim();
|
|
642
|
-
if (!prompt) {
|
|
643
|
-
throw new Error("Gemini Flash text helper requires a prompt");
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
const temperature =
|
|
647
|
-
typeof params.temperature === "number" ? params.temperature : 0.7;
|
|
648
|
-
const maxTokens =
|
|
649
|
-
typeof params.maxTokens === "number" ? params.maxTokens : undefined;
|
|
650
|
-
|
|
651
|
-
const model = this.createGeminiModel(props);
|
|
652
|
-
const startTime = Date.now();
|
|
653
|
-
|
|
654
|
-
try {
|
|
655
|
-
context.log(
|
|
656
|
-
`Invoking Gemini Flash text helper (temperature=${temperature}, maxTokens=${typeof maxTokens === "number" ? maxTokens : "provider-default"
|
|
657
|
-
})`
|
|
658
|
-
);
|
|
659
|
-
const generationOptions: Record<string, any> = {
|
|
660
|
-
model,
|
|
661
|
-
prompt,
|
|
662
|
-
temperature,
|
|
663
|
-
};
|
|
664
|
-
if (typeof maxTokens === "number") {
|
|
665
|
-
generationOptions.maxOutputTokens = maxTokens;
|
|
666
|
-
}
|
|
667
|
-
const { text, content } = await generateText(
|
|
668
|
-
generationOptions as any
|
|
669
|
-
);
|
|
670
|
-
const contentText = content
|
|
671
|
-
? content
|
|
672
|
-
.map((part: any) =>
|
|
673
|
-
typeof part?.text === "string" ? part.text : ""
|
|
674
|
-
)
|
|
675
|
-
.join(" ")
|
|
676
|
-
.trim()
|
|
677
|
-
: "";
|
|
678
|
-
const finalText = (text || "").trim() || contentText || "";
|
|
679
|
-
if (!finalText) {
|
|
680
|
-
throw new Error("Gemini Flash text helper returned empty response");
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
return {
|
|
684
|
-
texto: finalText,
|
|
685
|
-
metadados: {
|
|
686
|
-
modelo_utilizado: this.GEMINI_MODEL,
|
|
687
|
-
tempo_processamento: Date.now() - startTime,
|
|
688
|
-
temperature,
|
|
689
|
-
max_tokens: typeof maxTokens === "number" ? maxTokens : null,
|
|
690
|
-
prompt_utilizado: prompt,
|
|
691
|
-
},
|
|
692
|
-
};
|
|
693
|
-
} catch (error) {
|
|
694
|
-
const message =
|
|
695
|
-
error instanceof Error ? error.message : "Unknown Gemini Flash error";
|
|
696
|
-
context.log(`Gemini Flash text helper failed: ${message}`);
|
|
697
|
-
context.setError(message);
|
|
698
|
-
throw error;
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
private async callGeminiFlashTranslation(
|
|
703
|
-
params: {
|
|
704
|
-
texto?: string;
|
|
705
|
-
idioma_origem?: string;
|
|
706
|
-
idioma_destino?: string;
|
|
707
|
-
promptTemplate?: string;
|
|
708
|
-
},
|
|
709
|
-
props: Record<string, string>,
|
|
710
|
-
context: ExecutionContext
|
|
711
|
-
): Promise<{
|
|
712
|
-
texto_traduzido: string;
|
|
713
|
-
metadados: {
|
|
714
|
-
modelo_utilizado: string;
|
|
715
|
-
tempo_processamento: number;
|
|
716
|
-
confianca: number;
|
|
717
|
-
idiomas_suportados: string[];
|
|
718
|
-
idiomas_solicitados: {
|
|
719
|
-
origem: string;
|
|
720
|
-
destino: string;
|
|
721
|
-
};
|
|
722
|
-
prompt_utilizado: string;
|
|
723
|
-
};
|
|
724
|
-
}> {
|
|
725
|
-
const texto = params.texto?.trim();
|
|
726
|
-
const idiomaOrigem = params.idioma_origem?.trim().toLowerCase();
|
|
727
|
-
const idiomaDestino = params.idioma_destino?.trim().toLowerCase();
|
|
728
|
-
|
|
729
|
-
if (!texto || !idiomaOrigem || !idiomaDestino) {
|
|
730
|
-
throw new Error(
|
|
731
|
-
"Missing required translation parameters: texto, idioma_origem, idioma_destino"
|
|
732
|
-
);
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
if (idiomaOrigem === idiomaDestino) {
|
|
736
|
-
return {
|
|
737
|
-
texto_traduzido: texto,
|
|
738
|
-
metadados: {
|
|
739
|
-
modelo_utilizado: this.GEMINI_MODEL,
|
|
740
|
-
tempo_processamento: 0,
|
|
741
|
-
confianca: 1,
|
|
742
|
-
idiomas_suportados: this.SUPPORTED_TRANSLATION_LANGUAGES,
|
|
743
|
-
idiomas_solicitados: {
|
|
744
|
-
origem: idiomaOrigem,
|
|
745
|
-
destino: idiomaDestino,
|
|
746
|
-
},
|
|
747
|
-
prompt_utilizado: "Bypass: idiomas de origem e destino são iguais",
|
|
748
|
-
},
|
|
749
|
-
};
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
const template =
|
|
753
|
-
params.promptTemplate && params.promptTemplate.trim().length > 0
|
|
754
|
-
? params.promptTemplate
|
|
755
|
-
: `Traduza o texto abaixo de {{idioma_origem}} para {{idioma_destino}}.
|
|
756
|
-
Responda somente com o texto traduzido sem comentários adicionais.
|
|
757
|
-
|
|
758
|
-
Texto:
|
|
759
|
-
{{texto}}`;
|
|
760
|
-
|
|
761
|
-
const prompt = template
|
|
762
|
-
.replace(/{{texto}}/g, texto)
|
|
763
|
-
.replace(/{{idioma_origem}}/g, idiomaOrigem)
|
|
764
|
-
.replace(/{{idioma_destino}}/g, idiomaDestino)
|
|
765
|
-
.trim();
|
|
766
|
-
|
|
767
|
-
const model = this.createGeminiModel(props);
|
|
768
|
-
const startTime = Date.now();
|
|
769
|
-
|
|
770
|
-
try {
|
|
771
|
-
context.log(
|
|
772
|
-
`Invoking Gemini Flash translation helper (${idiomaOrigem}->${idiomaDestino})`
|
|
773
|
-
);
|
|
774
|
-
const { text, content } = await generateText({
|
|
775
|
-
model,
|
|
776
|
-
prompt,
|
|
777
|
-
temperature: 0.2,
|
|
778
|
-
});
|
|
779
|
-
|
|
780
|
-
const contentText = content
|
|
781
|
-
? content
|
|
782
|
-
.map((part: any) =>
|
|
783
|
-
typeof part?.text === "string" ? part.text : ""
|
|
784
|
-
)
|
|
785
|
-
.join(" ")
|
|
786
|
-
.trim()
|
|
787
|
-
: "";
|
|
788
|
-
const translatedText = (text || "").trim() || contentText || "";
|
|
789
|
-
if (!translatedText) {
|
|
790
|
-
throw new Error("Gemini Flash translation returned empty response");
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
return {
|
|
794
|
-
texto_traduzido: translatedText,
|
|
795
|
-
metadados: {
|
|
796
|
-
modelo_utilizado: this.GEMINI_MODEL,
|
|
797
|
-
tempo_processamento: Date.now() - startTime,
|
|
798
|
-
confianca: 0.85,
|
|
799
|
-
idiomas_suportados: this.SUPPORTED_TRANSLATION_LANGUAGES,
|
|
800
|
-
idiomas_solicitados: {
|
|
801
|
-
origem: idiomaOrigem,
|
|
802
|
-
destino: idiomaDestino,
|
|
803
|
-
},
|
|
804
|
-
prompt_utilizado: prompt,
|
|
805
|
-
},
|
|
806
|
-
};
|
|
807
|
-
} catch (error) {
|
|
808
|
-
const message =
|
|
809
|
-
error instanceof Error ? error.message : "Unknown translation error";
|
|
810
|
-
context.log(`Gemini Flash translation failed: ${message}`);
|
|
811
|
-
context.setError(message);
|
|
812
|
-
throw error;
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
private async callGeminiFlashImage(
|
|
817
|
-
params: { prompt: string; estilo: string; resolucao: string },
|
|
818
|
-
props: Record<string, string>,
|
|
819
|
-
context: ExecutionContext
|
|
820
|
-
): Promise<{
|
|
821
|
-
image_url: string;
|
|
822
|
-
image_base64: string;
|
|
823
|
-
media_type: string;
|
|
824
|
-
prompt_utilizado: string;
|
|
825
|
-
metadados: {
|
|
826
|
-
modelo_utilizado: string;
|
|
827
|
-
tempo_processamento: number;
|
|
828
|
-
estilo: string;
|
|
829
|
-
resolucao: string;
|
|
830
|
-
};
|
|
831
|
-
}> {
|
|
832
|
-
const prompt = params.prompt?.trim();
|
|
833
|
-
const estilo = params.estilo?.trim();
|
|
834
|
-
const resolucao = params.resolucao?.trim();
|
|
835
|
-
|
|
836
|
-
if (!prompt) {
|
|
837
|
-
throw new Error("Gemini Flash image helper requires a prompt");
|
|
838
|
-
}
|
|
839
|
-
if (!estilo) {
|
|
840
|
-
throw new Error("Gemini Flash image helper requires an estilo value");
|
|
841
|
-
}
|
|
842
|
-
if (!resolucao || !/^\d+x\d+$/.test(resolucao)) {
|
|
843
|
-
throw new Error(
|
|
844
|
-
"Gemini Flash image helper requires uma resolução no formato LARGURAxALTURA (ex: 1024x1024)"
|
|
845
|
-
);
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
const model = this.createGeminiImageModel(props);
|
|
849
|
-
const startTime = Date.now();
|
|
850
|
-
|
|
851
|
-
try {
|
|
852
|
-
context.log(
|
|
853
|
-
`Invoking Gemini Flash image helper (estilo=${estilo}, resolucao=${resolucao})`
|
|
854
|
-
);
|
|
855
|
-
const result = await experimental_generateImage({
|
|
856
|
-
model,
|
|
857
|
-
prompt,
|
|
858
|
-
size: resolucao as `${number}x${number}`,
|
|
859
|
-
});
|
|
860
|
-
|
|
861
|
-
const image = result.image;
|
|
862
|
-
if (!image?.base64 || !image.mediaType) {
|
|
863
|
-
throw new Error("Gemini Flash image helper returned an invalid file");
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
const normalizedBase64 = image.base64.replace(/\s+/g, "");
|
|
867
|
-
const imageUrl = `data:${image.mediaType};base64,${normalizedBase64}`;
|
|
868
|
-
|
|
869
|
-
return {
|
|
870
|
-
image_url: imageUrl,
|
|
871
|
-
image_base64: normalizedBase64,
|
|
872
|
-
media_type: image.mediaType,
|
|
873
|
-
prompt_utilizado: prompt,
|
|
874
|
-
metadados: {
|
|
875
|
-
modelo_utilizado: this.GEMINI_IMAGE_MODEL,
|
|
876
|
-
tempo_processamento: Date.now() - startTime,
|
|
877
|
-
estilo,
|
|
878
|
-
resolucao,
|
|
879
|
-
},
|
|
880
|
-
};
|
|
881
|
-
} catch (error) {
|
|
882
|
-
const message =
|
|
883
|
-
error instanceof Error ? error.message : "Unknown image error";
|
|
884
|
-
context.log(`Gemini Flash image helper failed: ${message}`);
|
|
885
|
-
context.setError(message);
|
|
886
|
-
throw error;
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
772
|
}
|
|
890
773
|
|
|
891
774
|
// Singleton instance
|