enya-agent 0.1.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/.env.example +20 -0
- package/.github/workflows/ci.yml +70 -0
- package/.github/workflows/publish.yml +250 -0
- package/.gitmodules +3 -0
- package/Cargo.lock +3584 -0
- package/Cargo.toml +97 -0
- package/crates/enact/Cargo.toml +27 -0
- package/crates/enact/src/lib.rs +60 -0
- package/crates/enact-a2a/Cargo.toml +25 -0
- package/crates/enact-a2a/src/lib.rs +411 -0
- package/crates/enact-channels/Cargo.toml +64 -0
- package/crates/enact-channels/examples/README.md +80 -0
- package/crates/enact-channels/examples/channel_bot.rs +169 -0
- package/crates/enact-channels/examples/telegram-echo.rs +34 -0
- package/crates/enact-channels/examples/whatsapp-echo.rs +142 -0
- package/crates/enact-channels/src/config.rs +213 -0
- package/crates/enact-channels/src/lib.rs +25 -0
- package/crates/enact-channels/src/runtime.rs +237 -0
- package/crates/enact-channels/src/security/mod.rs +5 -0
- package/crates/enact-channels/src/security/pairing.rs +205 -0
- package/crates/enact-channels/src/teams.rs +601 -0
- package/crates/enact-channels/src/telegram.rs +2833 -0
- package/crates/enact-channels/src/traits.rs +200 -0
- package/crates/enact-channels/src/webhook.rs +262 -0
- package/crates/enact-channels/src/whatsapp.rs +310 -0
- package/crates/enact-cli/Cargo.toml +40 -0
- package/crates/enact-cli/src/commands/doctor.rs +62 -0
- package/crates/enact-cli/src/commands/mod.rs +3 -0
- package/crates/enact-cli/src/commands/run.rs +69 -0
- package/crates/enact-cli/src/commands/serve.rs +81 -0
- package/crates/enact-cli/src/config.rs +2 -0
- package/crates/enact-cli/src/main.rs +79 -0
- package/crates/enact-config/Cargo.toml +36 -0
- package/crates/enact-config/ENV_VAR_MAPPING.md +135 -0
- package/crates/enact-config/QUICK_REFERENCE.md +92 -0
- package/crates/enact-config/README.md +107 -0
- package/crates/enact-config/TESTING.md +161 -0
- package/crates/enact-config/examples/test-env-vars.rs +100 -0
- package/crates/enact-config/src/config.rs +399 -0
- package/crates/enact-config/src/encrypted_store.rs +211 -0
- package/crates/enact-config/src/lib.rs +298 -0
- package/crates/enact-config/src/secrets.rs +149 -0
- package/crates/enact-config/src/sync.rs +260 -0
- package/crates/enact-config/test-env-vars.sh +34 -0
- package/crates/enact-config/tests/README.md +99 -0
- package/crates/enact-config/tests/config_integration_test.rs +202 -0
- package/crates/enact-config/tests/security_test.rs +140 -0
- package/crates/enact-context/Cargo.toml +41 -0
- package/crates/enact-context/src/budget.rs +314 -0
- package/crates/enact-context/src/calibrator.rs +535 -0
- package/crates/enact-context/src/compactor.rs +392 -0
- package/crates/enact-context/src/condenser.rs +826 -0
- package/crates/enact-context/src/lib.rs +94 -0
- package/crates/enact-context/src/segment.rs +238 -0
- package/crates/enact-context/src/step_context.rs +645 -0
- package/crates/enact-context/src/token_counter.rs +148 -0
- package/crates/enact-context/src/window.rs +372 -0
- package/crates/enact-core/Cargo.toml +42 -0
- package/crates/enact-core/README.md +98 -0
- package/crates/enact-core/src/background/executor.rs +524 -0
- package/crates/enact-core/src/background/mod.rs +48 -0
- package/crates/enact-core/src/background/target_binding.rs +390 -0
- package/crates/enact-core/src/background/trigger.rs +511 -0
- package/crates/enact-core/src/callable/callable.rs +152 -0
- package/crates/enact-core/src/callable/composite.rs +817 -0
- package/crates/enact-core/src/callable/graph.rs +104 -0
- package/crates/enact-core/src/callable/llm.rs +211 -0
- package/crates/enact-core/src/callable/mod.rs +64 -0
- package/crates/enact-core/src/callable/registry.rs +206 -0
- package/crates/enact-core/src/context/execution_context.rs +757 -0
- package/crates/enact-core/src/context/invocation.rs +99 -0
- package/crates/enact-core/src/context/mod.rs +50 -0
- package/crates/enact-core/src/context/tenant.rs +175 -0
- package/crates/enact-core/src/context/trace.rs +127 -0
- package/crates/enact-core/src/flow/conditional.rs +293 -0
- package/crates/enact-core/src/flow/mod.rs +43 -0
- package/crates/enact-core/src/flow/parallel.rs +437 -0
- package/crates/enact-core/src/flow/repeat.rs +534 -0
- package/crates/enact-core/src/flow/sequential.rs +248 -0
- package/crates/enact-core/src/graph/checkpoint.rs +79 -0
- package/crates/enact-core/src/graph/checkpoint_store.rs +76 -0
- package/crates/enact-core/src/graph/compiled.rs +189 -0
- package/crates/enact-core/src/graph/edge.rs +59 -0
- package/crates/enact-core/src/graph/graph_schema.rs +218 -0
- package/crates/enact-core/src/graph/loader.rs +155 -0
- package/crates/enact-core/src/graph/mod.rs +18 -0
- package/crates/enact-core/src/graph/node/function.rs +49 -0
- package/crates/enact-core/src/graph/node/mod.rs +48 -0
- package/crates/enact-core/src/graph/schema.rs +62 -0
- package/crates/enact-core/src/inbox/message.rs +405 -0
- package/crates/enact-core/src/inbox/mod.rs +31 -0
- package/crates/enact-core/src/inbox/store.rs +355 -0
- package/crates/enact-core/src/kernel/artifact/filesystem.rs +546 -0
- package/crates/enact-core/src/kernel/artifact/metadata.rs +283 -0
- package/crates/enact-core/src/kernel/artifact/mod.rs +27 -0
- package/crates/enact-core/src/kernel/artifact/store.rs +427 -0
- package/crates/enact-core/src/kernel/enforcement.rs +1315 -0
- package/crates/enact-core/src/kernel/error.rs +1200 -0
- package/crates/enact-core/src/kernel/event.rs +1394 -0
- package/crates/enact-core/src/kernel/execution_model.rs +831 -0
- package/crates/enact-core/src/kernel/execution_state.rs +189 -0
- package/crates/enact-core/src/kernel/execution_strategy.rs +117 -0
- package/crates/enact-core/src/kernel/ids.rs +2086 -0
- package/crates/enact-core/src/kernel/interrupt.rs +125 -0
- package/crates/enact-core/src/kernel/kernel.rs +1283 -0
- package/crates/enact-core/src/kernel/mod.rs +205 -0
- package/crates/enact-core/src/kernel/persistence/event_store.rs +270 -0
- package/crates/enact-core/src/kernel/persistence/message_store.rs +908 -0
- package/crates/enact-core/src/kernel/persistence/mod.rs +102 -0
- package/crates/enact-core/src/kernel/persistence/state_store.rs +228 -0
- package/crates/enact-core/src/kernel/persistence/vector_store.rs +299 -0
- package/crates/enact-core/src/kernel/reducer.rs +808 -0
- package/crates/enact-core/src/kernel/replay.rs +153 -0
- package/crates/enact-core/src/lib.rs +413 -0
- package/crates/enact-core/src/memory/episodic.rs +0 -0
- package/crates/enact-core/src/memory/mod.rs +6 -0
- package/crates/enact-core/src/memory/semantic.rs +0 -0
- package/crates/enact-core/src/memory/trait.rs +0 -0
- package/crates/enact-core/src/memory/vector_db.rs +0 -0
- package/crates/enact-core/src/memory/working.rs +0 -0
- package/crates/enact-core/src/policy/execution_policy.rs +292 -0
- package/crates/enact-core/src/policy/filters.rs +458 -0
- package/crates/enact-core/src/policy/input_processor.rs +407 -0
- package/crates/enact-core/src/policy/long_running.rs +134 -0
- package/crates/enact-core/src/policy/mod.rs +193 -0
- package/crates/enact-core/src/policy/pii_input.rs +274 -0
- package/crates/enact-core/src/policy/tenant_policy.rs +453 -0
- package/crates/enact-core/src/policy/tool_policy.rs +407 -0
- package/crates/enact-core/src/providers/mod.rs +63 -0
- package/crates/enact-core/src/providers/trait.rs +292 -0
- package/crates/enact-core/src/runner/callbacks.rs +6 -0
- package/crates/enact-core/src/runner/execution_runner.rs +476 -0
- package/crates/enact-core/src/runner/loop.rs +117 -0
- package/crates/enact-core/src/runner/mod.rs +58 -0
- package/crates/enact-core/src/runner/protected_runner.rs +280 -0
- package/crates/enact-core/src/signal/inmemory.rs +231 -0
- package/crates/enact-core/src/signal/mod.rs +108 -0
- package/crates/enact-core/src/streaming/event_logger.rs +195 -0
- package/crates/enact-core/src/streaming/event_stream.rs +1423 -0
- package/crates/enact-core/src/streaming/mod.rs +108 -0
- package/crates/enact-core/src/streaming/pause_cancel.rs +0 -0
- package/crates/enact-core/src/streaming/protected_emitter.rs +173 -0
- package/crates/enact-core/src/streaming/protection/context.rs +136 -0
- package/crates/enact-core/src/streaming/protection/encryption.rs +289 -0
- package/crates/enact-core/src/streaming/protection/mod.rs +43 -0
- package/crates/enact-core/src/streaming/protection/pii_protection.rs +243 -0
- package/crates/enact-core/src/streaming/protection/processor.rs +166 -0
- package/crates/enact-core/src/streaming/sse.rs +0 -0
- package/crates/enact-core/src/telemetry/exporter.rs +0 -0
- package/crates/enact-core/src/telemetry/init.rs +0 -0
- package/crates/enact-core/src/telemetry/mod.rs +49 -0
- package/crates/enact-core/src/telemetry/spans.rs +245 -0
- package/crates/enact-core/src/tool/agent_tool.rs +177 -0
- package/crates/enact-core/src/tool/browser/mod.rs +0 -0
- package/crates/enact-core/src/tool/browser/webdriver.rs +0 -0
- package/crates/enact-core/src/tool/cost.rs +247 -0
- package/crates/enact-core/src/tool/discovery.rs +0 -0
- package/crates/enact-core/src/tool/dispatcher.rs +347 -0
- package/crates/enact-core/src/tool/filesystem.rs +231 -0
- package/crates/enact-core/src/tool/function.rs +99 -0
- package/crates/enact-core/src/tool/git.rs +162 -0
- package/crates/enact-core/src/tool/http.rs +214 -0
- package/crates/enact-core/src/tool/mcp/client.rs +0 -0
- package/crates/enact-core/src/tool/mcp/mod.rs +0 -0
- package/crates/enact-core/src/tool/mod.rs +51 -0
- package/crates/enact-core/src/tool/reasoning/debugging.rs +0 -0
- package/crates/enact-core/src/tool/reasoning/mcts.rs +0 -0
- package/crates/enact-core/src/tool/reasoning/mod.rs +0 -0
- package/crates/enact-core/src/tool/reasoning/sequential.rs +0 -0
- package/crates/enact-core/src/tool/sandbox/dagger.rs +0 -0
- package/crates/enact-core/src/tool/sandbox/mod.rs +0 -0
- package/crates/enact-core/src/tool/shell.rs +147 -0
- package/crates/enact-core/src/tool/trait.rs +33 -0
- package/crates/enact-core/src/tool/web_search.rs +277 -0
- package/crates/enact-core/src/util/config.rs +0 -0
- package/crates/enact-core/src/util/errors.rs +0 -0
- package/crates/enact-core/src/util/mod.rs +6 -0
- package/crates/enact-core/tests/airgapped_e2e_test.rs +291 -0
- package/crates/enact-core/tests/e2e_agentic_loop.rs +119 -0
- package/crates/enact-core/tests/e2e_test.rs +259 -0
- package/crates/enact-core/tests/graph_test.rs +130 -0
- package/crates/enact-core/tests/stream_event_id_validation.rs +435 -0
- package/crates/enact-cron/Cargo.toml +28 -0
- package/crates/enact-cron/src/lib.rs +44 -0
- package/crates/enact-cron/src/schedule.rs +156 -0
- package/crates/enact-cron/src/store.rs +589 -0
- package/crates/enact-cron/src/types.rs +148 -0
- package/crates/enact-gateway/Cargo.toml +31 -0
- package/crates/enact-gateway/README.md +30 -0
- package/crates/enact-gateway/examples/whatsapp-gateway-runner-mock.rs +59 -0
- package/crates/enact-gateway/examples/whatsapp-gateway.rs +42 -0
- package/crates/enact-gateway/src/lib.rs +582 -0
- package/crates/enact-mcp/Cargo.toml +24 -0
- package/crates/enact-mcp/src/lib.rs +178 -0
- package/crates/enact-memory/Cargo.toml +25 -0
- package/crates/enact-memory/src/backend.rs +20 -0
- package/crates/enact-memory/src/chunker.rs +230 -0
- package/crates/enact-memory/src/embeddings.rs +221 -0
- package/crates/enact-memory/src/lib.rs +67 -0
- package/crates/enact-memory/src/markdown.rs +127 -0
- package/crates/enact-memory/src/none.rs +61 -0
- package/crates/enact-memory/src/sqlite.rs +276 -0
- package/crates/enact-memory/src/traits.rs +65 -0
- package/crates/enact-memory/src/vector.rs +198 -0
- package/crates/enact-oauth/Cargo.toml +27 -0
- package/crates/enact-oauth/src/lib.rs +584 -0
- package/crates/enact-observability/Cargo.toml +22 -0
- package/crates/enact-observability/src/lib.rs +197 -0
- package/crates/enact-providers/Cargo.toml +33 -0
- package/crates/enact-providers/examples/hello-agent.rs +33 -0
- package/crates/enact-providers/src/anthropic.rs +182 -0
- package/crates/enact-providers/src/azure.rs +96 -0
- package/crates/enact-providers/src/bridge.rs +221 -0
- package/crates/enact-providers/src/gemini.rs +227 -0
- package/crates/enact-providers/src/http.rs +78 -0
- package/crates/enact-providers/src/lib.rs +53 -0
- package/crates/enact-providers/src/openai_compatible.rs +167 -0
- package/crates/enact-providers/src/openrouter.rs +33 -0
- package/crates/enact-runner/Cargo.toml +24 -0
- package/crates/enact-runner/README.md +76 -0
- package/crates/enact-runner/src/compaction.rs +225 -0
- package/crates/enact-runner/src/config.rs +118 -0
- package/crates/enact-runner/src/lib.rs +63 -0
- package/crates/enact-runner/src/loop_driver.rs +414 -0
- package/crates/enact-runner/src/parser.rs +421 -0
- package/crates/enact-runner/src/retry.rs +262 -0
- package/crates/enact-runner/tests/integration.rs +278 -0
- package/crates/enact-security/Cargo.toml +22 -0
- package/crates/enact-security/src/audit.rs +375 -0
- package/crates/enact-security/src/lib.rs +37 -0
- package/crates/enact-security/src/policy.rs +406 -0
- package/crates/enact-skills/Cargo.toml +25 -0
- package/crates/enact-skills/src/lib.rs +506 -0
- package/crates/enact-tools/Cargo.toml +22 -0
- package/crates/enact-tools/src/file_read.rs +166 -0
- package/crates/enact-tools/src/file_write.rs +216 -0
- package/crates/enact-tools/src/git_operations.rs +513 -0
- package/crates/enact-tools/src/http_request.rs +417 -0
- package/crates/enact-tools/src/lib.rs +104 -0
- package/crates/enact-tools/src/security.rs +227 -0
- package/crates/enact-tools/src/shell.rs +191 -0
- package/crates/enact-tools/src/traits.rs +159 -0
- package/docs/Makefile +74 -0
- package/docs/config.toml +62 -0
- package/docs/content/_index.md +174 -0
- package/docs/content/a2a/_index.md +431 -0
- package/docs/content/api/_index.md +323 -0
- package/docs/content/channels/_index.md +160 -0
- package/docs/content/channels/teams.md +205 -0
- package/docs/content/channels/telegram.md +182 -0
- package/docs/content/channels/webhook.md +423 -0
- package/docs/content/channels/whatsapp.md +240 -0
- package/docs/content/cli/_index.md +261 -0
- package/docs/content/concepts/_index.md +273 -0
- package/docs/content/configuration/_index.md +241 -0
- package/docs/content/cron/_index.md +248 -0
- package/docs/content/developers/_index.md +278 -0
- package/docs/content/getting-started/_index.md +180 -0
- package/docs/content/installation/_index.md +186 -0
- package/docs/content/installation/uninstall.md +101 -0
- package/docs/content/installation/updating.md +120 -0
- package/docs/content/mcp/_index.md +215 -0
- package/docs/content/memory/_index.md +163 -0
- package/docs/content/oauth/_index.md +515 -0
- package/docs/content/providers/_index.md +206 -0
- package/docs/content/roadmap/_index.md +199 -0
- package/docs/content/security/_index.md +219 -0
- package/docs/content/skills/_index.md +228 -0
- package/docs/content/tools/_index.md +485 -0
- package/docs/content/troubleshooting/_index.md +259 -0
- package/docs/content/yaml-schema/_index.md +294 -0
- package/docs/static/giallo-dark.css +91 -0
- package/docs/static/giallo-light.css +91 -0
- package/docs/themes/tanuki/.github/workflows/deploy.yml +44 -0
- package/docs/themes/tanuki/LICENSE +21 -0
- package/docs/themes/tanuki/README.md +166 -0
- package/docs/themes/tanuki/examples/blog/config.toml +58 -0
- package/docs/themes/tanuki/examples/blog/content/_index.md +4 -0
- package/docs/themes/tanuki/examples/blog/content/about.md +33 -0
- package/docs/themes/tanuki/examples/blog/content/blog/_index.md +7 -0
- package/docs/themes/tanuki/examples/blog/content/blog/api-design-best-practices.md +245 -0
- package/docs/themes/tanuki/examples/blog/content/blog/building-accessible-websites.md +147 -0
- package/docs/themes/tanuki/examples/blog/content/blog/css-grid-vs-flexbox.md +165 -0
- package/docs/themes/tanuki/examples/blog/content/blog/customizing-catppuccin-colors.md +137 -0
- package/docs/themes/tanuki/examples/blog/content/blog/dark-mode-best-practices.md +82 -0
- package/docs/themes/tanuki/examples/blog/content/blog/docker-essentials.md +301 -0
- package/docs/themes/tanuki/examples/blog/content/blog/getting-started-with-zola.md +129 -0
- package/docs/themes/tanuki/examples/blog/content/blog/git-workflow-for-content.md +112 -0
- package/docs/themes/tanuki/examples/blog/content/blog/introduction-to-webassembly.md +183 -0
- package/docs/themes/tanuki/examples/blog/content/blog/modern-javascript-features.md +234 -0
- package/docs/themes/tanuki/examples/blog/content/blog/testing-strategies.md +311 -0
- package/docs/themes/tanuki/examples/blog/content/blog/typography-for-developers.md +104 -0
- package/docs/themes/tanuki/examples/blog/content/blog/welcome-to-tanuki.md +67 -0
- package/docs/themes/tanuki/examples/blog/content/blog/why-static-sites.md +85 -0
- package/docs/themes/tanuki/examples/blog/content/projects.md +64 -0
- package/docs/themes/tanuki/examples/book/config.toml +17 -0
- package/docs/themes/tanuki/examples/book/content/_index.md +12 -0
- package/docs/themes/tanuki/examples/book/content/chapter-1.md +90 -0
- package/docs/themes/tanuki/examples/book/content/chapter-2.md +143 -0
- package/docs/themes/tanuki/examples/book/content/chapter-3.md +217 -0
- package/docs/themes/tanuki/examples/book/content/chapter-4.md +224 -0
- package/docs/themes/tanuki/examples/book/content/chapter-5.md +297 -0
- package/docs/themes/tanuki/examples/book/content/print.md +6 -0
- package/docs/themes/tanuki/examples/docs/config.toml +28 -0
- package/docs/themes/tanuki/examples/docs/content/_index.md +20 -0
- package/docs/themes/tanuki/examples/docs/content/components.md +156 -0
- package/docs/themes/tanuki/examples/docs/content/configuration.md +94 -0
- package/docs/themes/tanuki/examples/docs/content/customization.md +202 -0
- package/docs/themes/tanuki/examples/docs/content/deployment.md +204 -0
- package/docs/themes/tanuki/examples/docs/content/installation.md +59 -0
- package/docs/themes/tanuki/examples/docs/content/print.md +6 -0
- package/docs/themes/tanuki/examples/docs/static/img/tanuki-icon.avif +0 -0
- package/docs/themes/tanuki/examples/index.html +2104 -0
- package/docs/themes/tanuki/mise.toml +108 -0
- package/docs/themes/tanuki/sass/base/_catppuccin.scss +164 -0
- package/docs/themes/tanuki/sass/base/_fonts.scss +64 -0
- package/docs/themes/tanuki/sass/base/_reset.scss +152 -0
- package/docs/themes/tanuki/sass/base/_typography.scss +523 -0
- package/docs/themes/tanuki/sass/components/_buttons.scss +209 -0
- package/docs/themes/tanuki/sass/components/_code.scss +457 -0
- package/docs/themes/tanuki/sass/components/_landing.scss +633 -0
- package/docs/themes/tanuki/sass/components/_layout.scss +294 -0
- package/docs/themes/tanuki/sass/components/_navigation.scss +1200 -0
- package/docs/themes/tanuki/sass/components/_print.scss +237 -0
- package/docs/themes/tanuki/sass/components/_search.scss +224 -0
- package/docs/themes/tanuki/sass/components/_sidebar.scss +473 -0
- package/docs/themes/tanuki/sass/components/_theme-toggle.scss +186 -0
- package/docs/themes/tanuki/sass/modes/_blog.scss +366 -0
- package/docs/themes/tanuki/sass/modes/_product.scss +875 -0
- package/docs/themes/tanuki/sass/modes/_raskell.scss +1696 -0
- package/docs/themes/tanuki/sass/patterns/_buttons.scss +183 -0
- package/docs/themes/tanuki/sass/patterns/_cards.scss +144 -0
- package/docs/themes/tanuki/sass/patterns/_index.scss +9 -0
- package/docs/themes/tanuki/sass/patterns/_lists.scss +259 -0
- package/docs/themes/tanuki/sass/patterns/_sections.scss +243 -0
- package/docs/themes/tanuki/sass/style.scss +47 -0
- package/docs/themes/tanuki/sass/tokens/_colors.scss +139 -0
- package/docs/themes/tanuki/sass/tokens/_spacing.scss +100 -0
- package/docs/themes/tanuki/sass/tokens/_typography.scss +186 -0
- package/docs/themes/tanuki/screenshot.png +0 -0
- package/docs/themes/tanuki/sentinel.kdl +59 -0
- package/docs/themes/tanuki/static/elasticlunr.min.js +10 -0
- package/docs/themes/tanuki/static/fonts/GEIST-LICENSE.txt +92 -0
- package/docs/themes/tanuki/static/fonts/Geist-Variable.woff2 +0 -0
- package/docs/themes/tanuki/static/fonts/GeistMono-Variable.woff2 +0 -0
- package/docs/themes/tanuki/static/img/tanuki-icon.avif +0 -0
- package/docs/themes/tanuki/static/img/tanuki-icon.png +0 -0
- package/docs/themes/tanuki/static/js/anchors.js +18 -0
- package/docs/themes/tanuki/static/js/app.js +274 -0
- package/docs/themes/tanuki/static/js/code.js +394 -0
- package/docs/themes/tanuki/static/js/navigation.js +778 -0
- package/docs/themes/tanuki/static/js/scroll-to-top.js +33 -0
- package/docs/themes/tanuki/static/js/search-raskell.js +240 -0
- package/docs/themes/tanuki/static/js/search.js +215 -0
- package/docs/themes/tanuki/static/js/theme.js +169 -0
- package/docs/themes/tanuki/static/syntax-dark.css +151 -0
- package/docs/themes/tanuki/static/syntax-light.css +151 -0
- package/docs/themes/tanuki/static/wasm/sentinel_playground_wasm.js +486 -0
- package/docs/themes/tanuki/static/wasm/sentinel_playground_wasm_bg.wasm +0 -0
- package/docs/themes/tanuki/templates/404.html +52 -0
- package/docs/themes/tanuki/templates/base.html +428 -0
- package/docs/themes/tanuki/templates/blog.html +66 -0
- package/docs/themes/tanuki/templates/home.html +108 -0
- package/docs/themes/tanuki/templates/index.html +178 -0
- package/docs/themes/tanuki/templates/landing.html +168 -0
- package/docs/themes/tanuki/templates/macros/nav.html +128 -0
- package/docs/themes/tanuki/templates/macros/posts.html +101 -0
- package/docs/themes/tanuki/templates/macros/ui.html +159 -0
- package/docs/themes/tanuki/templates/page.html +135 -0
- package/docs/themes/tanuki/templates/partials/footer.html +38 -0
- package/docs/themes/tanuki/templates/partials/header.html +366 -0
- package/docs/themes/tanuki/templates/partials/nav-buttons.html +55 -0
- package/docs/themes/tanuki/templates/partials/nav-overlay.html +81 -0
- package/docs/themes/tanuki/templates/partials/page-toc-panel.html +43 -0
- package/docs/themes/tanuki/templates/partials/search.html +52 -0
- package/docs/themes/tanuki/templates/partials/sidebar.html +107 -0
- package/docs/themes/tanuki/templates/partials/theme-toggle.html +35 -0
- package/docs/themes/tanuki/templates/partials/toc-overlay.html +146 -0
- package/docs/themes/tanuki/templates/partials/version-picker.html +38 -0
- package/docs/themes/tanuki/templates/print.html +244 -0
- package/docs/themes/tanuki/templates/section.html +186 -0
- package/docs/themes/tanuki/templates/taxonomy_list.html +18 -0
- package/docs/themes/tanuki/templates/taxonomy_single.html +31 -0
- package/docs/themes/tanuki/theme.toml +58 -0
- package/examples/hello-agent.rs +55 -0
- package/package.json +36 -0
- package/proto/config.proto +60 -0
- package/proto/events.proto +0 -0
- package/proto/runtime.proto +215 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
//! MCP (Model Context Protocol) Client for Enact
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides a client for the Model Context Protocol,
|
|
4
|
+
//! allowing Enact to connect to MCP servers and use their tools.
|
|
5
|
+
|
|
6
|
+
use anyhow::Result;
|
|
7
|
+
use async_trait::async_trait;
|
|
8
|
+
use serde::{Deserialize, Serialize};
|
|
9
|
+
use std::process::Stdio;
|
|
10
|
+
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
|
11
|
+
use tokio::process::{Child, Command};
|
|
12
|
+
use tracing::debug;
|
|
13
|
+
|
|
14
|
+
/// MCP Tool definition
|
|
15
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
16
|
+
pub struct McpTool {
|
|
17
|
+
pub name: String,
|
|
18
|
+
pub description: String,
|
|
19
|
+
pub parameters: serde_json::Value,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// MCP Client for stdio transport
|
|
23
|
+
pub struct McpStdioClient {
|
|
24
|
+
process: Child,
|
|
25
|
+
stdin: tokio::process::ChildStdin,
|
|
26
|
+
stdout: BufReader<tokio::process::ChildStdout>,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
impl McpStdioClient {
|
|
30
|
+
/// Create a new MCP client connected to a command via stdio
|
|
31
|
+
pub async fn new(command: &str, args: &[&str]) -> Result<Self> {
|
|
32
|
+
let mut process = Command::new(command)
|
|
33
|
+
.args(args)
|
|
34
|
+
.stdin(Stdio::piped())
|
|
35
|
+
.stdout(Stdio::piped())
|
|
36
|
+
.stderr(Stdio::piped())
|
|
37
|
+
.spawn()?;
|
|
38
|
+
|
|
39
|
+
let stdin = process.stdin.take().unwrap();
|
|
40
|
+
let stdout = BufReader::new(process.stdout.take().unwrap());
|
|
41
|
+
|
|
42
|
+
let mut client = Self {
|
|
43
|
+
process,
|
|
44
|
+
stdin,
|
|
45
|
+
stdout,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Initialize connection
|
|
49
|
+
client.initialize().await?;
|
|
50
|
+
|
|
51
|
+
Ok(client)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async fn initialize(&mut self) -> Result<()> {
|
|
55
|
+
let init_request = serde_json::json!({
|
|
56
|
+
"jsonrpc": "2.0",
|
|
57
|
+
"id": 1,
|
|
58
|
+
"method": "initialize",
|
|
59
|
+
"params": {
|
|
60
|
+
"protocolVersion": "2024-11-05",
|
|
61
|
+
"capabilities": {},
|
|
62
|
+
"clientInfo": {
|
|
63
|
+
"name": "enact-mcp",
|
|
64
|
+
"version": "0.1.0"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
self.send_request(&init_request).await?;
|
|
70
|
+
let response = self.read_response().await?;
|
|
71
|
+
debug!("MCP initialized: {:?}", response);
|
|
72
|
+
|
|
73
|
+
Ok(())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async fn send_request(&mut self, request: &serde_json::Value) -> Result<()> {
|
|
77
|
+
let request_str = request.to_string();
|
|
78
|
+
debug!("Sending MCP request: {}", request_str);
|
|
79
|
+
|
|
80
|
+
self.stdin.write_all(request_str.as_bytes()).await?;
|
|
81
|
+
self.stdin.write_all(b"\n").await?;
|
|
82
|
+
self.stdin.flush().await?;
|
|
83
|
+
|
|
84
|
+
Ok(())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async fn read_response(&mut self) -> Result<serde_json::Value> {
|
|
88
|
+
let mut line = String::new();
|
|
89
|
+
self.stdout.read_line(&mut line).await?;
|
|
90
|
+
|
|
91
|
+
debug!("Received MCP response: {}", line);
|
|
92
|
+
let response: serde_json::Value = serde_json::from_str(&line)?;
|
|
93
|
+
Ok(response)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// List available tools from the MCP server
|
|
97
|
+
pub async fn list_tools(&mut self) -> Result<Vec<McpTool>> {
|
|
98
|
+
let request = serde_json::json!({
|
|
99
|
+
"jsonrpc": "2.0",
|
|
100
|
+
"id": 2,
|
|
101
|
+
"method": "tools/list"
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
self.send_request(&request).await?;
|
|
105
|
+
let response = self.read_response().await?;
|
|
106
|
+
|
|
107
|
+
let tools = response
|
|
108
|
+
.get("result")
|
|
109
|
+
.and_then(|r| r.get("tools"))
|
|
110
|
+
.and_then(|t| t.as_array())
|
|
111
|
+
.map(|arr| {
|
|
112
|
+
arr.iter()
|
|
113
|
+
.filter_map(|tool| {
|
|
114
|
+
Some(McpTool {
|
|
115
|
+
name: tool.get("name")?.as_str()?.to_string(),
|
|
116
|
+
description: tool.get("description")?.as_str()?.to_string(),
|
|
117
|
+
parameters: tool.get("parameters")?.clone(),
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
.collect()
|
|
121
|
+
})
|
|
122
|
+
.unwrap_or_default();
|
|
123
|
+
|
|
124
|
+
Ok(tools)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// Call a tool on the MCP server
|
|
128
|
+
pub async fn call_tool(&mut self, name: &str, arguments: serde_json::Value) -> Result<String> {
|
|
129
|
+
let request = serde_json::json!({
|
|
130
|
+
"jsonrpc": "2.0",
|
|
131
|
+
"id": 3,
|
|
132
|
+
"method": "tools/call",
|
|
133
|
+
"params": {
|
|
134
|
+
"name": name,
|
|
135
|
+
"arguments": arguments
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
self.send_request(&request).await?;
|
|
140
|
+
let response = self.read_response().await?;
|
|
141
|
+
|
|
142
|
+
if let Some(error) = response.get("error") {
|
|
143
|
+
anyhow::bail!("MCP tool error: {:?}", error);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let content = response
|
|
147
|
+
.get("result")
|
|
148
|
+
.and_then(|r| r.get("content"))
|
|
149
|
+
.and_then(|c| c.as_array())
|
|
150
|
+
.and_then(|arr| arr.first())
|
|
151
|
+
.and_then(|item| item.get("text"))
|
|
152
|
+
.and_then(|t| t.as_str())
|
|
153
|
+
.unwrap_or("No content returned");
|
|
154
|
+
|
|
155
|
+
Ok(content.to_string())
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
impl Drop for McpStdioClient {
|
|
160
|
+
fn drop(&mut self) {
|
|
161
|
+
let _ = self.process.start_kill();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#[cfg(test)]
|
|
166
|
+
mod tests {
|
|
167
|
+
use super::*;
|
|
168
|
+
|
|
169
|
+
#[test]
|
|
170
|
+
fn test_mcp_tool_creation() {
|
|
171
|
+
let tool = McpTool {
|
|
172
|
+
name: "test".to_string(),
|
|
173
|
+
description: "Test tool".to_string(),
|
|
174
|
+
parameters: serde_json::json!({}),
|
|
175
|
+
};
|
|
176
|
+
assert_eq!(tool.name, "test");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "enact-memory"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
license.workspace = true
|
|
6
|
+
description = "Local-first memory backends for Enact"
|
|
7
|
+
repository.workspace = true
|
|
8
|
+
homepage.workspace = true
|
|
9
|
+
keywords = ["memory", "storage", "sqlite", "local"]
|
|
10
|
+
categories.workspace = true
|
|
11
|
+
|
|
12
|
+
[dependencies]
|
|
13
|
+
anyhow.workspace = true
|
|
14
|
+
async-trait.workspace = true
|
|
15
|
+
chrono.workspace = true
|
|
16
|
+
serde.workspace = true
|
|
17
|
+
serde_json.workspace = true
|
|
18
|
+
tokio.workspace = true
|
|
19
|
+
uuid.workspace = true
|
|
20
|
+
tracing.workspace = true
|
|
21
|
+
rusqlite = { version = "0.32", features = ["bundled"] }
|
|
22
|
+
reqwest.workspace = true
|
|
23
|
+
|
|
24
|
+
[dev-dependencies]
|
|
25
|
+
tempfile.workspace = true
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
2
|
+
pub enum MemoryBackendKind {
|
|
3
|
+
Sqlite,
|
|
4
|
+
Markdown,
|
|
5
|
+
None,
|
|
6
|
+
Unknown,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
pub fn classify_memory_backend(name: &str) -> MemoryBackendKind {
|
|
10
|
+
match name.trim().to_ascii_lowercase().as_str() {
|
|
11
|
+
"sqlite" => MemoryBackendKind::Sqlite,
|
|
12
|
+
"markdown" | "md" => MemoryBackendKind::Markdown,
|
|
13
|
+
"none" | "noop" => MemoryBackendKind::None,
|
|
14
|
+
_ => MemoryBackendKind::Unknown,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pub fn default_memory_backend_key() -> &'static str {
|
|
19
|
+
"sqlite"
|
|
20
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
//! Line-based markdown chunker — splits documents into semantic chunks.
|
|
2
|
+
//!
|
|
3
|
+
//! Splits on markdown headings and paragraph boundaries, respecting
|
|
4
|
+
//! a max token limit per chunk. Preserves heading context.
|
|
5
|
+
|
|
6
|
+
/// A single chunk of text with metadata.
|
|
7
|
+
#[derive(Debug, Clone)]
|
|
8
|
+
pub struct Chunk {
|
|
9
|
+
pub index: usize,
|
|
10
|
+
pub content: String,
|
|
11
|
+
pub heading: Option<String>,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/// Split markdown text into chunks, each under `max_tokens` approximate tokens.
|
|
15
|
+
///
|
|
16
|
+
/// Strategy:
|
|
17
|
+
/// 1. Split on `## ` and `# ` headings (keeps heading with its content)
|
|
18
|
+
/// 2. If a section exceeds `max_tokens`, split on blank lines (paragraphs)
|
|
19
|
+
/// 3. If a paragraph still exceeds, split on line boundaries
|
|
20
|
+
///
|
|
21
|
+
/// Token estimation: ~4 chars per token (rough English average).
|
|
22
|
+
pub fn chunk_markdown(text: &str, max_tokens: usize) -> Vec<Chunk> {
|
|
23
|
+
if text.trim().is_empty() {
|
|
24
|
+
return Vec::new();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let max_chars = max_tokens * 4;
|
|
28
|
+
let sections = split_on_headings(text);
|
|
29
|
+
let mut chunks = Vec::new();
|
|
30
|
+
|
|
31
|
+
for (heading, body) in sections {
|
|
32
|
+
let full = if let Some(ref h) = heading {
|
|
33
|
+
format!("{h}\n{body}")
|
|
34
|
+
} else {
|
|
35
|
+
body.clone()
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
if full.len() <= max_chars {
|
|
39
|
+
chunks.push(Chunk {
|
|
40
|
+
index: chunks.len(),
|
|
41
|
+
content: full.trim().to_string(),
|
|
42
|
+
heading: heading.clone(),
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
// Split on paragraphs (blank lines)
|
|
46
|
+
let paragraphs = split_on_blank_lines(&body);
|
|
47
|
+
let mut current = heading
|
|
48
|
+
.as_ref()
|
|
49
|
+
.map_or_else(String::new, |h| format!("{h}\n"));
|
|
50
|
+
|
|
51
|
+
for para in paragraphs {
|
|
52
|
+
if current.len() + para.len() > max_chars && !current.trim().is_empty() {
|
|
53
|
+
chunks.push(Chunk {
|
|
54
|
+
index: chunks.len(),
|
|
55
|
+
content: current.trim().to_string(),
|
|
56
|
+
heading: heading.clone(),
|
|
57
|
+
});
|
|
58
|
+
current = heading
|
|
59
|
+
.as_ref()
|
|
60
|
+
.map_or_else(String::new, |h| format!("{h}\n"));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if para.len() > max_chars {
|
|
64
|
+
// Paragraph too big — split on lines
|
|
65
|
+
if !current.trim().is_empty() {
|
|
66
|
+
chunks.push(Chunk {
|
|
67
|
+
index: chunks.len(),
|
|
68
|
+
content: current.trim().to_string(),
|
|
69
|
+
heading: heading.clone(),
|
|
70
|
+
});
|
|
71
|
+
current = heading
|
|
72
|
+
.as_ref()
|
|
73
|
+
.map_or_else(String::new, |h| format!("{h}\n"));
|
|
74
|
+
}
|
|
75
|
+
for line_chunk in split_on_lines(¶, max_chars) {
|
|
76
|
+
chunks.push(Chunk {
|
|
77
|
+
index: chunks.len(),
|
|
78
|
+
content: line_chunk.trim().to_string(),
|
|
79
|
+
heading: heading.clone(),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
current.push_str(¶);
|
|
84
|
+
current.push('\n');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if !current.trim().is_empty() {
|
|
89
|
+
chunks.push(Chunk {
|
|
90
|
+
index: chunks.len(),
|
|
91
|
+
content: current.trim().to_string(),
|
|
92
|
+
heading: heading.clone(),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Filter out empty chunks
|
|
99
|
+
chunks.retain(|c| !c.content.is_empty());
|
|
100
|
+
|
|
101
|
+
// Re-index
|
|
102
|
+
for (i, chunk) in chunks.iter_mut().enumerate() {
|
|
103
|
+
chunk.index = i;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
chunks
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Split text into `(heading, body)` sections.
|
|
110
|
+
fn split_on_headings(text: &str) -> Vec<(Option<String>, String)> {
|
|
111
|
+
let mut sections = Vec::new();
|
|
112
|
+
let mut current_heading: Option<String> = None;
|
|
113
|
+
let mut current_body = String::new();
|
|
114
|
+
|
|
115
|
+
for line in text.lines() {
|
|
116
|
+
if line.starts_with("# ") || line.starts_with("## ") || line.starts_with("### ") {
|
|
117
|
+
if !current_body.trim().is_empty() || current_heading.is_some() {
|
|
118
|
+
sections.push((current_heading.take(), current_body.clone()));
|
|
119
|
+
current_body.clear();
|
|
120
|
+
}
|
|
121
|
+
current_heading = Some(line.to_string());
|
|
122
|
+
} else {
|
|
123
|
+
current_body.push_str(line);
|
|
124
|
+
current_body.push('\n');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if !current_body.trim().is_empty() || current_heading.is_some() {
|
|
129
|
+
sections.push((current_heading, current_body));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
sections
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// Split text on blank lines (paragraph boundaries)
|
|
136
|
+
fn split_on_blank_lines(text: &str) -> Vec<String> {
|
|
137
|
+
let mut paragraphs = Vec::new();
|
|
138
|
+
let mut current = String::new();
|
|
139
|
+
|
|
140
|
+
for line in text.lines() {
|
|
141
|
+
if line.trim().is_empty() {
|
|
142
|
+
if !current.trim().is_empty() {
|
|
143
|
+
paragraphs.push(current.clone());
|
|
144
|
+
current.clear();
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
current.push_str(line);
|
|
148
|
+
current.push('\n');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if !current.trim().is_empty() {
|
|
153
|
+
paragraphs.push(current);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
paragraphs
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// Split text on line boundaries to fit within `max_chars`
|
|
160
|
+
fn split_on_lines(text: &str, max_chars: usize) -> Vec<String> {
|
|
161
|
+
let mut chunks = Vec::new();
|
|
162
|
+
let mut current = String::new();
|
|
163
|
+
|
|
164
|
+
for line in text.lines() {
|
|
165
|
+
if current.len() + line.len() + 1 > max_chars && !current.is_empty() {
|
|
166
|
+
chunks.push(current.clone());
|
|
167
|
+
current.clear();
|
|
168
|
+
}
|
|
169
|
+
current.push_str(line);
|
|
170
|
+
current.push('\n');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if !current.is_empty() {
|
|
174
|
+
chunks.push(current);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
chunks
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#[cfg(test)]
|
|
181
|
+
mod tests {
|
|
182
|
+
use super::*;
|
|
183
|
+
|
|
184
|
+
#[test]
|
|
185
|
+
fn empty_text() {
|
|
186
|
+
assert!(chunk_markdown("", 512).is_empty());
|
|
187
|
+
assert!(chunk_markdown(" ", 512).is_empty());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#[test]
|
|
191
|
+
fn single_short_paragraph() {
|
|
192
|
+
let chunks = chunk_markdown("Hello world", 512);
|
|
193
|
+
assert_eq!(chunks.len(), 1);
|
|
194
|
+
assert_eq!(chunks[0].content, "Hello world");
|
|
195
|
+
assert!(chunks[0].heading.is_none());
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#[test]
|
|
199
|
+
fn heading_sections() {
|
|
200
|
+
let text = "# Title\nSome intro.\n\n## Section A\nContent A.\n\n## Section B\nContent B.";
|
|
201
|
+
let chunks = chunk_markdown(text, 512);
|
|
202
|
+
assert!(chunks.len() >= 3);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
#[test]
|
|
206
|
+
fn respects_max_tokens() {
|
|
207
|
+
let long_text: String = (0..200).fold(String::new(), |mut s, i| {
|
|
208
|
+
use std::fmt::Write;
|
|
209
|
+
let _ = writeln!(
|
|
210
|
+
s,
|
|
211
|
+
"This is sentence number {i} with some extra words to fill it up."
|
|
212
|
+
);
|
|
213
|
+
s
|
|
214
|
+
});
|
|
215
|
+
let chunks = chunk_markdown(&long_text, 50);
|
|
216
|
+
assert!(chunks.len() > 1);
|
|
217
|
+
for chunk in &chunks {
|
|
218
|
+
assert!(chunk.content.len() <= 300);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
#[test]
|
|
223
|
+
fn indexes_are_sequential() {
|
|
224
|
+
let text = "# A\nContent A\n\n# B\nContent B\n\n# C\nContent C";
|
|
225
|
+
let chunks = chunk_markdown(text, 512);
|
|
226
|
+
for (i, chunk) in chunks.iter().enumerate() {
|
|
227
|
+
assert_eq!(chunk.index, i);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
//! Embedding providers for semantic memory search
|
|
2
|
+
|
|
3
|
+
use async_trait::async_trait;
|
|
4
|
+
|
|
5
|
+
/// Trait for embedding providers — convert text to vectors
|
|
6
|
+
#[async_trait]
|
|
7
|
+
pub trait EmbeddingProvider: Send + Sync {
|
|
8
|
+
/// Provider name
|
|
9
|
+
fn name(&self) -> &str;
|
|
10
|
+
|
|
11
|
+
/// Embedding dimensions
|
|
12
|
+
fn dimensions(&self) -> usize;
|
|
13
|
+
|
|
14
|
+
/// Embed a batch of texts into vectors
|
|
15
|
+
async fn embed(&self, texts: &[&str]) -> anyhow::Result<Vec<Vec<f32>>>;
|
|
16
|
+
|
|
17
|
+
/// Embed a single text
|
|
18
|
+
async fn embed_one(&self, text: &str) -> anyhow::Result<Vec<f32>> {
|
|
19
|
+
let mut results = self.embed(&[text]).await?;
|
|
20
|
+
results
|
|
21
|
+
.pop()
|
|
22
|
+
.ok_or_else(|| anyhow::anyhow!("Empty embedding result"))
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ── Noop provider (keyword-only fallback) ────────────────────
|
|
27
|
+
|
|
28
|
+
/// No-op embedding provider that returns empty vectors
|
|
29
|
+
pub struct NoopEmbedding;
|
|
30
|
+
|
|
31
|
+
#[async_trait]
|
|
32
|
+
impl EmbeddingProvider for NoopEmbedding {
|
|
33
|
+
fn name(&self) -> &str {
|
|
34
|
+
"none"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn dimensions(&self) -> usize {
|
|
38
|
+
0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async fn embed(&self, _texts: &[&str]) -> anyhow::Result<Vec<Vec<f32>>> {
|
|
42
|
+
Ok(Vec::new())
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ── OpenAI-compatible embedding provider ─────────────────────
|
|
47
|
+
|
|
48
|
+
/// OpenAI-compatible embedding provider
|
|
49
|
+
pub struct OpenAiEmbedding {
|
|
50
|
+
base_url: String,
|
|
51
|
+
api_key: String,
|
|
52
|
+
model: String,
|
|
53
|
+
dims: usize,
|
|
54
|
+
client: reqwest::Client,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
impl OpenAiEmbedding {
|
|
58
|
+
pub fn new(base_url: &str, api_key: &str, model: &str, dims: usize) -> Self {
|
|
59
|
+
Self {
|
|
60
|
+
base_url: base_url.trim_end_matches('/').to_string(),
|
|
61
|
+
api_key: api_key.to_string(),
|
|
62
|
+
model: model.to_string(),
|
|
63
|
+
dims,
|
|
64
|
+
client: reqwest::Client::new(),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn embeddings_url(&self) -> String {
|
|
69
|
+
let url = reqwest::Url::parse(&self.base_url).ok();
|
|
70
|
+
let has_embeddings = url
|
|
71
|
+
.as_ref()
|
|
72
|
+
.map(|u| u.path().trim_end_matches('/').ends_with("/embeddings"))
|
|
73
|
+
.unwrap_or(false);
|
|
74
|
+
|
|
75
|
+
if has_embeddings {
|
|
76
|
+
return self.base_url.clone();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let has_path = url
|
|
80
|
+
.as_ref()
|
|
81
|
+
.map(|u| {
|
|
82
|
+
let path = u.path().trim_end_matches('/');
|
|
83
|
+
!path.is_empty() && path != "/"
|
|
84
|
+
})
|
|
85
|
+
.unwrap_or(false);
|
|
86
|
+
|
|
87
|
+
if has_path {
|
|
88
|
+
format!("{}/embeddings", self.base_url)
|
|
89
|
+
} else {
|
|
90
|
+
format!("{}/v1/embeddings", self.base_url)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#[async_trait]
|
|
96
|
+
impl EmbeddingProvider for OpenAiEmbedding {
|
|
97
|
+
fn name(&self) -> &str {
|
|
98
|
+
"openai"
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fn dimensions(&self) -> usize {
|
|
102
|
+
self.dims
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async fn embed(&self, texts: &[&str]) -> anyhow::Result<Vec<Vec<f32>>> {
|
|
106
|
+
if texts.is_empty() {
|
|
107
|
+
return Ok(Vec::new());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let body = serde_json::json!({
|
|
111
|
+
"model": self.model,
|
|
112
|
+
"input": texts,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
let resp = self
|
|
116
|
+
.client
|
|
117
|
+
.post(self.embeddings_url())
|
|
118
|
+
.header("Authorization", format!("Bearer {}", self.api_key))
|
|
119
|
+
.header("Content-Type", "application/json")
|
|
120
|
+
.json(&body)
|
|
121
|
+
.send()
|
|
122
|
+
.await?;
|
|
123
|
+
|
|
124
|
+
if !resp.status().is_success() {
|
|
125
|
+
let status = resp.status();
|
|
126
|
+
let text = resp.text().await.unwrap_or_default();
|
|
127
|
+
anyhow::bail!("Embedding API error {status}: {text}");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let json: serde_json::Value = resp.json().await?;
|
|
131
|
+
let data = json
|
|
132
|
+
.get("data")
|
|
133
|
+
.and_then(|d| d.as_array())
|
|
134
|
+
.ok_or_else(|| anyhow::anyhow!("Invalid embedding response: missing 'data'"))?;
|
|
135
|
+
|
|
136
|
+
let mut embeddings = Vec::with_capacity(data.len());
|
|
137
|
+
for item in data {
|
|
138
|
+
let embedding = item
|
|
139
|
+
.get("embedding")
|
|
140
|
+
.and_then(|e| e.as_array())
|
|
141
|
+
.ok_or_else(|| anyhow::anyhow!("Invalid embedding item"))?;
|
|
142
|
+
|
|
143
|
+
#[allow(clippy::cast_possible_truncation)]
|
|
144
|
+
let vec: Vec<f32> = embedding
|
|
145
|
+
.iter()
|
|
146
|
+
.filter_map(|v| v.as_f64().map(|f| f as f32))
|
|
147
|
+
.collect();
|
|
148
|
+
|
|
149
|
+
embeddings.push(vec);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
Ok(embeddings)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── Factory ──────────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
/// Create an embedding provider
|
|
159
|
+
pub fn create_embedding_provider(
|
|
160
|
+
provider: &str,
|
|
161
|
+
api_key: Option<&str>,
|
|
162
|
+
model: &str,
|
|
163
|
+
dims: usize,
|
|
164
|
+
) -> Box<dyn EmbeddingProvider> {
|
|
165
|
+
match provider {
|
|
166
|
+
"openai" => {
|
|
167
|
+
let key = api_key.unwrap_or("");
|
|
168
|
+
Box::new(OpenAiEmbedding::new(
|
|
169
|
+
"https://api.openai.com",
|
|
170
|
+
key,
|
|
171
|
+
model,
|
|
172
|
+
dims,
|
|
173
|
+
))
|
|
174
|
+
}
|
|
175
|
+
name if name.starts_with("custom:") => {
|
|
176
|
+
let base_url = name.strip_prefix("custom:").unwrap_or("");
|
|
177
|
+
let key = api_key.unwrap_or("");
|
|
178
|
+
Box::new(OpenAiEmbedding::new(base_url, key, model, dims))
|
|
179
|
+
}
|
|
180
|
+
_ => Box::new(NoopEmbedding),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#[cfg(test)]
|
|
185
|
+
mod tests {
|
|
186
|
+
use super::*;
|
|
187
|
+
|
|
188
|
+
#[test]
|
|
189
|
+
fn noop_name() {
|
|
190
|
+
let p = NoopEmbedding;
|
|
191
|
+
assert_eq!(p.name(), "none");
|
|
192
|
+
assert_eq!(p.dimensions(), 0);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#[tokio::test]
|
|
196
|
+
async fn noop_embed_returns_empty() {
|
|
197
|
+
let p = NoopEmbedding;
|
|
198
|
+
let result = p.embed(&["hello"]).await.unwrap();
|
|
199
|
+
assert!(result.is_empty());
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#[test]
|
|
203
|
+
fn factory_none() {
|
|
204
|
+
let p = create_embedding_provider("none", None, "model", 1536);
|
|
205
|
+
assert_eq!(p.name(), "none");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#[test]
|
|
209
|
+
fn factory_openai() {
|
|
210
|
+
let p = create_embedding_provider("openai", Some("key"), "text-embedding-3-small", 1536);
|
|
211
|
+
assert_eq!(p.name(), "openai");
|
|
212
|
+
assert_eq!(p.dimensions(), 1536);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#[test]
|
|
216
|
+
fn factory_custom_url() {
|
|
217
|
+
let p = create_embedding_provider("custom:http://localhost:1234", None, "model", 768);
|
|
218
|
+
assert_eq!(p.name(), "openai");
|
|
219
|
+
assert_eq!(p.dimensions(), 768);
|
|
220
|
+
}
|
|
221
|
+
}
|