@xopcai/xopcbot 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/README.md +132 -0
- package/README.zh-CN.md +130 -0
- package/dist/__tests__/core.test.d.ts +1 -0
- package/dist/__tests__/core.test.js +72 -0
- package/dist/__tests__/core.test.js.map +1 -0
- package/dist/agent/fallback/__tests__/index.test.d.ts +1 -0
- package/dist/agent/fallback/__tests__/index.test.js +111 -0
- package/dist/agent/fallback/__tests__/index.test.js.map +1 -0
- package/dist/agent/fallback/candidates.d.ts +23 -0
- package/dist/agent/fallback/candidates.js +62 -0
- package/dist/agent/fallback/candidates.js.map +1 -0
- package/dist/agent/fallback/error.d.ts +22 -0
- package/dist/agent/fallback/error.js +27 -0
- package/dist/agent/fallback/error.js.map +1 -0
- package/dist/agent/fallback/index.d.ts +6 -0
- package/dist/agent/fallback/index.js +5 -0
- package/dist/agent/fallback/index.js.map +1 -0
- package/dist/agent/fallback/reason.d.ts +7 -0
- package/dist/agent/fallback/reason.js +78 -0
- package/dist/agent/fallback/reason.js.map +1 -0
- package/dist/agent/fallback/runner.d.ts +22 -0
- package/dist/agent/fallback/runner.js +34 -0
- package/dist/agent/fallback/runner.js.map +1 -0
- package/dist/agent/helpers.d.ts +38 -0
- package/dist/agent/helpers.js +122 -0
- package/dist/agent/helpers.js.map +1 -0
- package/dist/agent/index.d.ts +7 -0
- package/dist/agent/index.js +7 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/memory/compaction.d.ts +63 -0
- package/dist/agent/memory/compaction.js +233 -0
- package/dist/agent/memory/compaction.js.map +1 -0
- package/dist/agent/memory/window.d.ts +29 -0
- package/dist/agent/memory/window.js +64 -0
- package/dist/agent/memory/window.js.map +1 -0
- package/dist/agent/model-manager.d.ts +80 -0
- package/dist/agent/model-manager.js +213 -0
- package/dist/agent/model-manager.js.map +1 -0
- package/dist/agent/prompt/heartbeat.d.ts +97 -0
- package/dist/agent/prompt/heartbeat.js +313 -0
- package/dist/agent/prompt/heartbeat.js.map +1 -0
- package/dist/agent/prompt/index.d.ts +65 -0
- package/dist/agent/prompt/index.js +215 -0
- package/dist/agent/prompt/index.js.map +1 -0
- package/dist/agent/prompt/memory/index.d.ts +37 -0
- package/dist/agent/prompt/memory/index.js +241 -0
- package/dist/agent/prompt/memory/index.js.map +1 -0
- package/dist/agent/prompt/modes.d.ts +44 -0
- package/dist/agent/prompt/modes.js +167 -0
- package/dist/agent/prompt/modes.js.map +1 -0
- package/dist/agent/prompt/safety.d.ts +88 -0
- package/dist/agent/prompt/safety.js +290 -0
- package/dist/agent/prompt/safety.js.map +1 -0
- package/dist/agent/service.d.ts +91 -0
- package/dist/agent/service.js +641 -0
- package/dist/agent/service.js.map +1 -0
- package/dist/agent/session-tracker.d.ts +73 -0
- package/dist/agent/session-tracker.js +137 -0
- package/dist/agent/session-tracker.js.map +1 -0
- package/dist/agent/skills/__tests__/test-framework.test.d.ts +4 -0
- package/dist/agent/skills/__tests__/test-framework.test.js +343 -0
- package/dist/agent/skills/__tests__/test-framework.test.js.map +1 -0
- package/dist/agent/skills/config.d.ts +58 -0
- package/dist/agent/skills/config.js +212 -0
- package/dist/agent/skills/config.js.map +1 -0
- package/dist/agent/skills/index.d.ts +25 -0
- package/dist/agent/skills/index.js +349 -0
- package/dist/agent/skills/index.js.map +1 -0
- package/dist/agent/skills/installer.d.ts +38 -0
- package/dist/agent/skills/installer.js +352 -0
- package/dist/agent/skills/installer.js.map +1 -0
- package/dist/agent/skills/scanner.d.ts +32 -0
- package/dist/agent/skills/scanner.js +265 -0
- package/dist/agent/skills/scanner.js.map +1 -0
- package/dist/agent/skills/test-framework.d.ts +107 -0
- package/dist/agent/skills/test-framework.js +607 -0
- package/dist/agent/skills/test-framework.js.map +1 -0
- package/dist/agent/skills/types.d.ts +181 -0
- package/dist/agent/skills/types.js +7 -0
- package/dist/agent/skills/types.js.map +1 -0
- package/dist/agent/skills/watcher.d.ts +37 -0
- package/dist/agent/skills/watcher.js +124 -0
- package/dist/agent/skills/watcher.js.map +1 -0
- package/dist/agent/tools/communication.d.ts +18 -0
- package/dist/agent/tools/communication.js +56 -0
- package/dist/agent/tools/communication.js.map +1 -0
- package/dist/agent/tools/edit-diff.d.ts +21 -0
- package/dist/agent/tools/edit-diff.js +69 -0
- package/dist/agent/tools/edit-diff.js.map +1 -0
- package/dist/agent/tools/edit.d.ts +13 -0
- package/dist/agent/tools/edit.js +65 -0
- package/dist/agent/tools/edit.js.map +1 -0
- package/dist/agent/tools/find.d.ts +24 -0
- package/dist/agent/tools/find.js +99 -0
- package/dist/agent/tools/find.js.map +1 -0
- package/dist/agent/tools/grep.d.ts +33 -0
- package/dist/agent/tools/grep.js +183 -0
- package/dist/agent/tools/grep.js.map +1 -0
- package/dist/agent/tools/index.d.ts +13 -0
- package/dist/agent/tools/index.js +18 -0
- package/dist/agent/tools/index.js.map +1 -0
- package/dist/agent/tools/list-dir.d.ts +6 -0
- package/dist/agent/tools/list-dir.js +37 -0
- package/dist/agent/tools/list-dir.js.map +1 -0
- package/dist/agent/tools/memory-tool.d.ts +14 -0
- package/dist/agent/tools/memory-tool.js +81 -0
- package/dist/agent/tools/memory-tool.js.map +1 -0
- package/dist/agent/tools/path-utils.d.ts +4 -0
- package/dist/agent/tools/path-utils.js +13 -0
- package/dist/agent/tools/path-utils.js.map +1 -0
- package/dist/agent/tools/read.d.ts +7 -0
- package/dist/agent/tools/read.js +47 -0
- package/dist/agent/tools/read.js.map +1 -0
- package/dist/agent/tools/send-media.d.ts +18 -0
- package/dist/agent/tools/send-media.js +104 -0
- package/dist/agent/tools/send-media.js.map +1 -0
- package/dist/agent/tools/shell.d.ts +13 -0
- package/dist/agent/tools/shell.js +114 -0
- package/dist/agent/tools/shell.js.map +1 -0
- package/dist/agent/tools/truncate.d.ts +36 -0
- package/dist/agent/tools/truncate.js +98 -0
- package/dist/agent/tools/truncate.js.map +1 -0
- package/dist/agent/tools/web.d.ts +14 -0
- package/dist/agent/tools/web.js +109 -0
- package/dist/agent/tools/web.js.map +1 -0
- package/dist/agent/tools/write.d.ts +7 -0
- package/dist/agent/tools/write.js +35 -0
- package/dist/agent/tools/write.js.map +1 -0
- package/dist/agent/types.d.ts +8 -0
- package/dist/agent/types.js +2 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/agent/typing.d.ts +9 -0
- package/dist/agent/typing.js +32 -0
- package/dist/agent/typing.js.map +1 -0
- package/dist/auth/index.d.ts +12 -0
- package/dist/auth/index.js +14 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/oauth/anthropic.d.ts +7 -0
- package/dist/auth/oauth/anthropic.js +89 -0
- package/dist/auth/oauth/anthropic.js.map +1 -0
- package/dist/auth/oauth/github-copilot.d.ts +8 -0
- package/dist/auth/oauth/github-copilot.js +35 -0
- package/dist/auth/oauth/github-copilot.js.map +1 -0
- package/dist/auth/oauth/google-antigravity.d.ts +7 -0
- package/dist/auth/oauth/google-antigravity.js +27 -0
- package/dist/auth/oauth/google-antigravity.js.map +1 -0
- package/dist/auth/oauth/google-gemini-cli.d.ts +7 -0
- package/dist/auth/oauth/google-gemini-cli.js +28 -0
- package/dist/auth/oauth/google-gemini-cli.js.map +1 -0
- package/dist/auth/oauth/index.d.ts +14 -0
- package/dist/auth/oauth/index.js +15 -0
- package/dist/auth/oauth/index.js.map +1 -0
- package/dist/auth/oauth/kimi.d.ts +8 -0
- package/dist/auth/oauth/kimi.js +104 -0
- package/dist/auth/oauth/kimi.js.map +1 -0
- package/dist/auth/oauth/minimax.d.ts +8 -0
- package/dist/auth/oauth/minimax.js +109 -0
- package/dist/auth/oauth/minimax.js.map +1 -0
- package/dist/auth/oauth/openai-codex.d.ts +7 -0
- package/dist/auth/oauth/openai-codex.js +33 -0
- package/dist/auth/oauth/openai-codex.js.map +1 -0
- package/dist/auth/oauth/pkce.d.ts +8 -0
- package/dist/auth/oauth/pkce.js +22 -0
- package/dist/auth/oauth/pkce.js.map +1 -0
- package/dist/auth/oauth/qwen.d.ts +7 -0
- package/dist/auth/oauth/qwen.js +114 -0
- package/dist/auth/oauth/qwen.js.map +1 -0
- package/dist/auth/oauth/types.d.ts +46 -0
- package/dist/auth/oauth/types.js +7 -0
- package/dist/auth/oauth/types.js.map +1 -0
- package/dist/auth/profiles/index.d.ts +16 -0
- package/dist/auth/profiles/index.js +17 -0
- package/dist/auth/profiles/index.js.map +1 -0
- package/dist/auth/profiles/oauth.d.ts +12 -0
- package/dist/auth/profiles/oauth.js +106 -0
- package/dist/auth/profiles/oauth.js.map +1 -0
- package/dist/auth/profiles/order.d.ts +19 -0
- package/dist/auth/profiles/order.js +63 -0
- package/dist/auth/profiles/order.js.map +1 -0
- package/dist/auth/profiles/profiles.d.ts +27 -0
- package/dist/auth/profiles/profiles.js +110 -0
- package/dist/auth/profiles/profiles.js.map +1 -0
- package/dist/auth/profiles/store.d.ts +17 -0
- package/dist/auth/profiles/store.js +234 -0
- package/dist/auth/profiles/store.js.map +1 -0
- package/dist/auth/profiles/types.d.ts +64 -0
- package/dist/auth/profiles/types.js +7 -0
- package/dist/auth/profiles/types.js.map +1 -0
- package/dist/auth/profiles/usage.d.ts +24 -0
- package/dist/auth/profiles/usage.js +99 -0
- package/dist/auth/profiles/usage.js.map +1 -0
- package/dist/auth/storage.d.ts +99 -0
- package/dist/auth/storage.js +315 -0
- package/dist/auth/storage.js.map +1 -0
- package/dist/bus/index.d.ts +3 -0
- package/dist/bus/index.js +2 -0
- package/dist/bus/index.js.map +1 -0
- package/dist/bus/queue.d.ts +20 -0
- package/dist/bus/queue.js +57 -0
- package/dist/bus/queue.js.map +1 -0
- package/dist/channels/__tests__/access-control.test.d.ts +4 -0
- package/dist/channels/__tests__/access-control.test.js +310 -0
- package/dist/channels/__tests__/access-control.test.js.map +1 -0
- package/dist/channels/__tests__/draft-stream.test.d.ts +4 -0
- package/dist/channels/__tests__/draft-stream.test.js +243 -0
- package/dist/channels/__tests__/draft-stream.test.js.map +1 -0
- package/dist/channels/__tests__/format.test.d.ts +4 -0
- package/dist/channels/__tests__/format.test.js +146 -0
- package/dist/channels/__tests__/format.test.js.map +1 -0
- package/dist/channels/__tests__/manager.test.d.ts +6 -0
- package/dist/channels/__tests__/manager.test.js +34 -0
- package/dist/channels/__tests__/manager.test.js.map +1 -0
- package/dist/channels/__tests__/typing-controller.test.d.ts +4 -0
- package/dist/channels/__tests__/typing-controller.test.js +109 -0
- package/dist/channels/__tests__/typing-controller.test.js.map +1 -0
- package/dist/channels/__tests__/update-offset-store.test.d.ts +7 -0
- package/dist/channels/__tests__/update-offset-store.test.js +35 -0
- package/dist/channels/__tests__/update-offset-store.test.js.map +1 -0
- package/dist/channels/access-control.d.ts +93 -0
- package/dist/channels/access-control.js +201 -0
- package/dist/channels/access-control.js.map +1 -0
- package/dist/channels/draft-stream.d.ts +55 -0
- package/dist/channels/draft-stream.js +211 -0
- package/dist/channels/draft-stream.js.map +1 -0
- package/dist/channels/format.d.ts +38 -0
- package/dist/channels/format.js +187 -0
- package/dist/channels/format.js.map +1 -0
- package/dist/channels/index.d.ts +22 -0
- package/dist/channels/index.js +23 -0
- package/dist/channels/index.js.map +1 -0
- package/dist/channels/manager.d.ts +30 -0
- package/dist/channels/manager.js +125 -0
- package/dist/channels/manager.js.map +1 -0
- package/dist/channels/telegram/__tests__/index.test.d.ts +4 -0
- package/dist/channels/telegram/__tests__/index.test.js +153 -0
- package/dist/channels/telegram/__tests__/index.test.js.map +1 -0
- package/dist/channels/telegram/__tests__/inline-keyboards.test.d.ts +4 -0
- package/dist/channels/telegram/__tests__/inline-keyboards.test.js +99 -0
- package/dist/channels/telegram/__tests__/inline-keyboards.test.js.map +1 -0
- package/dist/channels/telegram/client.d.ts +48 -0
- package/dist/channels/telegram/client.js +177 -0
- package/dist/channels/telegram/client.js.map +1 -0
- package/dist/channels/telegram/command-handler.d.ts +28 -0
- package/dist/channels/telegram/command-handler.js +209 -0
- package/dist/channels/telegram/command-handler.js.map +1 -0
- package/dist/channels/telegram/index.d.ts +8 -0
- package/dist/channels/telegram/index.js +9 -0
- package/dist/channels/telegram/index.js.map +1 -0
- package/dist/channels/telegram/inline-keyboards.d.ts +20 -0
- package/dist/channels/telegram/inline-keyboards.js +38 -0
- package/dist/channels/telegram/inline-keyboards.js.map +1 -0
- package/dist/channels/telegram/plugin.d.ts +33 -0
- package/dist/channels/telegram/plugin.js +640 -0
- package/dist/channels/telegram/plugin.js.map +1 -0
- package/dist/channels/telegram/webhook.d.ts +31 -0
- package/dist/channels/telegram/webhook.js +129 -0
- package/dist/channels/telegram/webhook.js.map +1 -0
- package/dist/channels/types.d.ts +186 -0
- package/dist/channels/types.js +7 -0
- package/dist/channels/types.js.map +1 -0
- package/dist/channels/typing-controller.d.ts +47 -0
- package/dist/channels/typing-controller.js +117 -0
- package/dist/channels/typing-controller.js.map +1 -0
- package/dist/channels/update-offset-store.d.ts +37 -0
- package/dist/channels/update-offset-store.js +165 -0
- package/dist/channels/update-offset-store.js.map +1 -0
- package/dist/channels/whatsapp/index.d.ts +4 -0
- package/dist/channels/whatsapp/index.js +5 -0
- package/dist/channels/whatsapp/index.js.map +1 -0
- package/dist/channels/whatsapp/plugin.d.ts +27 -0
- package/dist/channels/whatsapp/plugin.js +66 -0
- package/dist/channels/whatsapp/plugin.js.map +1 -0
- package/dist/cli/__tests__/registry.test.d.ts +1 -0
- package/dist/cli/__tests__/registry.test.js +366 -0
- package/dist/cli/__tests__/registry.test.js.map +1 -0
- package/dist/cli/commands/agent.d.ts +1 -0
- package/dist/cli/commands/agent.js +216 -0
- package/dist/cli/commands/agent.js.map +1 -0
- package/dist/cli/commands/auth.d.ts +6 -0
- package/dist/cli/commands/auth.js +440 -0
- package/dist/cli/commands/auth.js.map +1 -0
- package/dist/cli/commands/config.d.ts +1 -0
- package/dist/cli/commands/config.js +135 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/configure.d.ts +1 -0
- package/dist/cli/commands/configure.js +173 -0
- package/dist/cli/commands/configure.js.map +1 -0
- package/dist/cli/commands/cron.d.ts +1 -0
- package/dist/cli/commands/cron.js +81 -0
- package/dist/cli/commands/cron.js.map +1 -0
- package/dist/cli/commands/gateway.d.ts +1 -0
- package/dist/cli/commands/gateway.js +91 -0
- package/dist/cli/commands/gateway.js.map +1 -0
- package/dist/cli/commands/index.d.ts +0 -0
- package/dist/cli/commands/index.js +2 -0
- package/dist/cli/commands/index.js.map +1 -0
- package/dist/cli/commands/models.d.ts +1 -0
- package/dist/cli/commands/models.js +79 -0
- package/dist/cli/commands/models.js.map +1 -0
- package/dist/cli/commands/onboard.d.ts +1 -0
- package/dist/cli/commands/onboard.js +621 -0
- package/dist/cli/commands/onboard.js.map +1 -0
- package/dist/cli/commands/plugin.d.ts +1 -0
- package/dist/cli/commands/plugin.js +341 -0
- package/dist/cli/commands/plugin.js.map +1 -0
- package/dist/cli/commands/session.d.ts +1 -0
- package/dist/cli/commands/session.js +450 -0
- package/dist/cli/commands/session.js.map +1 -0
- package/dist/cli/commands/skills-test.d.ts +9 -0
- package/dist/cli/commands/skills-test.js +195 -0
- package/dist/cli/commands/skills-test.js.map +1 -0
- package/dist/cli/commands/skills.d.ts +9 -0
- package/dist/cli/commands/skills.js +385 -0
- package/dist/cli/commands/skills.js.map +1 -0
- package/dist/cli/index.d.ts +19 -0
- package/dist/cli/index.js +40 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/registry.d.ts +43 -0
- package/dist/cli/registry.js +82 -0
- package/dist/cli/registry.js.map +1 -0
- package/dist/cli/templates.d.ts +19 -0
- package/dist/cli/templates.js +191 -0
- package/dist/cli/templates.js.map +1 -0
- package/dist/cli/utils/colors.d.ts +13 -0
- package/dist/cli/utils/colors.js +16 -0
- package/dist/cli/utils/colors.js.map +1 -0
- package/dist/config/__tests__/diff.test.d.ts +1 -0
- package/dist/config/__tests__/diff.test.js +192 -0
- package/dist/config/__tests__/diff.test.js.map +1 -0
- package/dist/config/__tests__/loader.test.d.ts +1 -0
- package/dist/config/__tests__/loader.test.js +356 -0
- package/dist/config/__tests__/loader.test.js.map +1 -0
- package/dist/config/__tests__/paths.test.d.ts +1 -0
- package/dist/config/__tests__/paths.test.js +192 -0
- package/dist/config/__tests__/paths.test.js.map +1 -0
- package/dist/config/__tests__/reload.test.d.ts +1 -0
- package/dist/config/__tests__/reload.test.js +374 -0
- package/dist/config/__tests__/reload.test.js.map +1 -0
- package/dist/config/__tests__/rules.test.d.ts +1 -0
- package/dist/config/__tests__/rules.test.js +204 -0
- package/dist/config/__tests__/rules.test.js.map +1 -0
- package/dist/config/__tests__/schema.test.d.ts +1 -0
- package/dist/config/__tests__/schema.test.js +672 -0
- package/dist/config/__tests__/schema.test.js.map +1 -0
- package/dist/config/diff.d.ts +7 -0
- package/dist/config/diff.js +46 -0
- package/dist/config/diff.js.map +1 -0
- package/dist/config/index.d.ts +6 -0
- package/dist/config/index.js +7 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/loader.d.ts +5 -0
- package/dist/config/loader.js +81 -0
- package/dist/config/loader.js.map +1 -0
- package/dist/config/paths.d.ts +35 -0
- package/dist/config/paths.js +79 -0
- package/dist/config/paths.js.map +1 -0
- package/dist/config/reload.d.ts +78 -0
- package/dist/config/reload.js +190 -0
- package/dist/config/reload.js.map +1 -0
- package/dist/config/rules.d.ts +42 -0
- package/dist/config/rules.js +114 -0
- package/dist/config/rules.js.map +1 -0
- package/dist/config/schema.d.ts +1340 -0
- package/dist/config/schema.js +611 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/cron/executor.d.ts +54 -0
- package/dist/cron/executor.js +364 -0
- package/dist/cron/executor.js.map +1 -0
- package/dist/cron/index.d.ts +5 -0
- package/dist/cron/index.js +6 -0
- package/dist/cron/index.js.map +1 -0
- package/dist/cron/persistence.d.ts +61 -0
- package/dist/cron/persistence.js +188 -0
- package/dist/cron/persistence.js.map +1 -0
- package/dist/cron/service.d.ts +94 -0
- package/dist/cron/service.js +382 -0
- package/dist/cron/service.js.map +1 -0
- package/dist/cron/types.d.ts +119 -0
- package/dist/cron/types.js +3 -0
- package/dist/cron/types.js.map +1 -0
- package/dist/cron/validation.d.ts +144 -0
- package/dist/cron/validation.js +113 -0
- package/dist/cron/validation.js.map +1 -0
- package/dist/errors/__tests__/index.test.d.ts +1 -0
- package/dist/errors/__tests__/index.test.js +301 -0
- package/dist/errors/__tests__/index.test.js.map +1 -0
- package/dist/errors/index.d.ts +114 -0
- package/dist/errors/index.js +278 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/gateway/hono/app.d.ts +7 -0
- package/dist/gateway/hono/app.js +668 -0
- package/dist/gateway/hono/app.js.map +1 -0
- package/dist/gateway/hono/index.d.ts +2 -0
- package/dist/gateway/hono/index.js +3 -0
- package/dist/gateway/hono/index.js.map +1 -0
- package/dist/gateway/hono/middleware/auth.d.ts +18 -0
- package/dist/gateway/hono/middleware/auth.js +79 -0
- package/dist/gateway/hono/middleware/auth.js.map +1 -0
- package/dist/gateway/hono/middleware/logger.d.ts +1 -0
- package/dist/gateway/hono/middleware/logger.js +17 -0
- package/dist/gateway/hono/middleware/logger.js.map +1 -0
- package/dist/gateway/hono/sse.d.ts +55 -0
- package/dist/gateway/hono/sse.js +224 -0
- package/dist/gateway/hono/sse.js.map +1 -0
- package/dist/gateway/index.d.ts +4 -0
- package/dist/gateway/index.js +5 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/gateway/protocol.d.ts +28 -0
- package/dist/gateway/protocol.js +14 -0
- package/dist/gateway/protocol.js.map +1 -0
- package/dist/gateway/server.d.ts +19 -0
- package/dist/gateway/server.js +64 -0
- package/dist/gateway/server.js.map +1 -0
- package/dist/gateway/service.d.ts +265 -0
- package/dist/gateway/service.js +621 -0
- package/dist/gateway/service.js.map +1 -0
- package/dist/gateway/static/root/assets/main-CfIxL-cL.js +2105 -0
- package/dist/gateway/static/root/assets/main-CfIxL-cL.js.map +1 -0
- package/dist/gateway/static/root/assets/main-DndcTCgX.css +1 -0
- package/dist/gateway/static/root/index.html +116 -0
- package/dist/heartbeat/index.d.ts +1 -0
- package/dist/heartbeat/index.js +2 -0
- package/dist/heartbeat/index.js.map +1 -0
- package/dist/heartbeat/service.d.ts +19 -0
- package/dist/heartbeat/service.js +61 -0
- package/dist/heartbeat/service.js.map +1 -0
- package/dist/plugin-sdk/index.d.ts +21 -0
- package/dist/plugin-sdk/index.js +12 -0
- package/dist/plugin-sdk/index.js.map +1 -0
- package/dist/plugins/__tests__/api.test.d.ts +1 -0
- package/dist/plugins/__tests__/api.test.js +164 -0
- package/dist/plugins/__tests__/api.test.js.map +1 -0
- package/dist/plugins/__tests__/hooks.test.d.ts +1 -0
- package/dist/plugins/__tests__/hooks.test.js +159 -0
- package/dist/plugins/__tests__/hooks.test.js.map +1 -0
- package/dist/plugins/__tests__/loader.test.d.ts +1 -0
- package/dist/plugins/__tests__/loader.test.js +120 -0
- package/dist/plugins/__tests__/loader.test.js.map +1 -0
- package/dist/plugins/api.d.ts +41 -0
- package/dist/plugins/api.js +131 -0
- package/dist/plugins/api.js.map +1 -0
- package/dist/plugins/hooks.d.ts +153 -0
- package/dist/plugins/hooks.js +172 -0
- package/dist/plugins/hooks.js.map +1 -0
- package/dist/plugins/index.d.ts +11 -0
- package/dist/plugins/index.js +16 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/install.d.ts +59 -0
- package/dist/plugins/install.js +324 -0
- package/dist/plugins/install.js.map +1 -0
- package/dist/plugins/loader.d.ts +84 -0
- package/dist/plugins/loader.js +545 -0
- package/dist/plugins/loader.js.map +1 -0
- package/dist/plugins/types.d.ts +190 -0
- package/dist/plugins/types.js +7 -0
- package/dist/plugins/types.js.map +1 -0
- package/dist/providers/__tests__/registry.test.d.ts +1 -0
- package/dist/providers/__tests__/registry.test.js +110 -0
- package/dist/providers/__tests__/registry.test.js.map +1 -0
- package/dist/providers/api-strategies.d.ts +15 -0
- package/dist/providers/api-strategies.js +96 -0
- package/dist/providers/api-strategies.js.map +1 -0
- package/dist/providers/auto-discovery.d.ts +84 -0
- package/dist/providers/auto-discovery.js +236 -0
- package/dist/providers/auto-discovery.js.map +1 -0
- package/dist/providers/config.d.ts +25 -0
- package/dist/providers/config.js +42 -0
- package/dist/providers/config.js.map +1 -0
- package/dist/providers/index.d.ts +25 -0
- package/dist/providers/index.js +55 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/model-catalog.d.ts +205 -0
- package/dist/providers/model-catalog.js +1071 -0
- package/dist/providers/model-catalog.js.map +1 -0
- package/dist/providers/models-dev-data.d.ts +11 -0
- package/dist/providers/models-dev-data.js +1035 -0
- package/dist/providers/models-dev-data.js.map +1 -0
- package/dist/providers/models-dev.d.ts +30 -0
- package/dist/providers/models-dev.js +42 -0
- package/dist/providers/models-dev.js.map +1 -0
- package/dist/providers/pi-ai.d.ts +62 -0
- package/dist/providers/pi-ai.js +221 -0
- package/dist/providers/pi-ai.js.map +1 -0
- package/dist/providers/provider-catalog.d.ts +173 -0
- package/dist/providers/provider-catalog.js +834 -0
- package/dist/providers/provider-catalog.js.map +1 -0
- package/dist/providers/registry.d.ts +155 -0
- package/dist/providers/registry.js +524 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/types.d.ts +95 -0
- package/dist/providers/types.js +7 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/session/chat-manager.d.ts +49 -0
- package/dist/session/chat-manager.js +167 -0
- package/dist/session/chat-manager.js.map +1 -0
- package/dist/session/chat-types.d.ts +53 -0
- package/dist/session/chat-types.js +63 -0
- package/dist/session/chat-types.js.map +1 -0
- package/dist/session/index.d.ts +5 -0
- package/dist/session/index.js +5 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/manager.d.ts +103 -0
- package/dist/session/manager.js +200 -0
- package/dist/session/manager.js.map +1 -0
- package/dist/session/store.d.ts +114 -0
- package/dist/session/store.js +687 -0
- package/dist/session/store.js.map +1 -0
- package/dist/session/types.d.ts +88 -0
- package/dist/session/types.js +10 -0
- package/dist/session/types.js.map +1 -0
- package/dist/types/index.d.ts +177 -0
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/providers.d.ts +67 -0
- package/dist/types/providers.js +7 -0
- package/dist/types/providers.js.map +1 -0
- package/dist/utils/frontmatter.d.ts +17 -0
- package/dist/utils/frontmatter.js +104 -0
- package/dist/utils/frontmatter.js.map +1 -0
- package/dist/utils/helpers.d.ts +5 -0
- package/dist/utils/helpers.js +36 -0
- package/dist/utils/helpers.js.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/log-store.d.ts +78 -0
- package/dist/utils/log-store.js +432 -0
- package/dist/utils/log-store.js.map +1 -0
- package/dist/utils/logger.d.ts +127 -0
- package/dist/utils/logger.js +481 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/logger.types.d.ts +152 -0
- package/dist/utils/logger.types.js +30 -0
- package/dist/utils/logger.types.js.map +1 -0
- package/package.json +93 -0
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Service - Main coordinator
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates message processing, model management, and session handling.
|
|
5
|
+
* Refactored to delegate specific concerns to specialized modules.
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync } from 'fs';
|
|
8
|
+
import { Agent } from '@mariozechner/pi-agent-core';
|
|
9
|
+
import { getApiKey as getConfigApiKey } from '../config/schema.js';
|
|
10
|
+
import { SessionStore } from '../session/index.js';
|
|
11
|
+
import { readFileTool, writeFileTool, editFileTool, listDirTool, createShellTool, grepTool, findTool, createWebSearchTool, webFetchTool, createMessageTool, createSendMediaTool, createMemorySearchTool, createMemoryGetTool, } from './tools/index.js';
|
|
12
|
+
import { createSkillLoader } from './skills/index.js';
|
|
13
|
+
import { getBundledSkillsDir } from '../config/paths.js';
|
|
14
|
+
import { createLogger } from '../utils/logger.js';
|
|
15
|
+
import { HookRunner, createHookContext } from '../plugins/index.js';
|
|
16
|
+
import { PromptBuilder } from './prompt/index.js';
|
|
17
|
+
import { createTypingController } from './typing.js';
|
|
18
|
+
import { loadBootstrapFiles, extractTextContent } from './helpers.js';
|
|
19
|
+
import { SessionTracker } from './session-tracker.js';
|
|
20
|
+
import { ModelManager } from './model-manager.js';
|
|
21
|
+
const log = createLogger('AgentService');
|
|
22
|
+
/**
|
|
23
|
+
* AgentService - Main agent orchestrator
|
|
24
|
+
*
|
|
25
|
+
* Delegates to:
|
|
26
|
+
* - SessionTracker: session state and cleanup
|
|
27
|
+
* - ModelManager: model selection and fallback
|
|
28
|
+
* - SessionStore: message persistence
|
|
29
|
+
* - PluginRegistry: hooks and tools
|
|
30
|
+
*/
|
|
31
|
+
export class AgentService {
|
|
32
|
+
bus;
|
|
33
|
+
config;
|
|
34
|
+
agent;
|
|
35
|
+
sessionStore;
|
|
36
|
+
compactor;
|
|
37
|
+
hookRunner;
|
|
38
|
+
unsubscribe;
|
|
39
|
+
running = false;
|
|
40
|
+
currentContext = null;
|
|
41
|
+
agentId;
|
|
42
|
+
skillPrompt = '';
|
|
43
|
+
skills = [];
|
|
44
|
+
skillLoader = createSkillLoader();
|
|
45
|
+
workspaceDir;
|
|
46
|
+
bootstrapFiles = [];
|
|
47
|
+
// Delegated modules
|
|
48
|
+
sessionTracker;
|
|
49
|
+
modelManager;
|
|
50
|
+
constructor(bus, config) {
|
|
51
|
+
this.bus = bus;
|
|
52
|
+
this.config = config;
|
|
53
|
+
this.agentId = `agent-${Date.now()}`;
|
|
54
|
+
this.workspaceDir = config.workspace;
|
|
55
|
+
// Load bootstrap files
|
|
56
|
+
this.bootstrapFiles = loadBootstrapFiles(config.workspace);
|
|
57
|
+
// Initialize delegated modules
|
|
58
|
+
this.sessionTracker = new SessionTracker();
|
|
59
|
+
this.modelManager = new ModelManager({
|
|
60
|
+
defaultModel: config.model,
|
|
61
|
+
config: config.config,
|
|
62
|
+
});
|
|
63
|
+
// Setup session store
|
|
64
|
+
const defaults = config.agentDefaults || config.config?.agents?.defaults;
|
|
65
|
+
const windowConfig = {
|
|
66
|
+
maxMessages: 100,
|
|
67
|
+
keepRecentMessages: defaults?.maxToolIterations || 20,
|
|
68
|
+
preserveSystemMessages: true,
|
|
69
|
+
};
|
|
70
|
+
const compactionConfig = {
|
|
71
|
+
enabled: defaults?.compaction?.enabled ?? true,
|
|
72
|
+
mode: defaults?.compaction?.mode || 'abstractive',
|
|
73
|
+
reserveTokens: defaults?.compaction?.reserveTokens || 8000,
|
|
74
|
+
triggerThreshold: defaults?.compaction?.triggerThreshold || 0.8,
|
|
75
|
+
minMessagesBeforeCompact: defaults?.compaction?.minMessagesBeforeCompact || 10,
|
|
76
|
+
keepRecentMessages: defaults?.compaction?.keepRecentMessages || 10,
|
|
77
|
+
};
|
|
78
|
+
this.sessionStore = new SessionStore(config.workspace, windowConfig, compactionConfig);
|
|
79
|
+
// Setup hook runner
|
|
80
|
+
if (config.pluginRegistry) {
|
|
81
|
+
this.hookRunner = new HookRunner(config.pluginRegistry, {
|
|
82
|
+
catchErrors: true,
|
|
83
|
+
logger: {
|
|
84
|
+
info: (msg) => log.info({ hook: true }, msg),
|
|
85
|
+
warn: (msg) => log.warn({ hook: true }, msg),
|
|
86
|
+
error: (msg) => log.error({ hook: true }, msg),
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// Initialize tools
|
|
91
|
+
const tools = [
|
|
92
|
+
readFileTool,
|
|
93
|
+
writeFileTool,
|
|
94
|
+
editFileTool,
|
|
95
|
+
listDirTool,
|
|
96
|
+
grepTool,
|
|
97
|
+
findTool,
|
|
98
|
+
createShellTool(config.workspace),
|
|
99
|
+
createWebSearchTool(config.braveApiKey),
|
|
100
|
+
webFetchTool,
|
|
101
|
+
createMessageTool(bus, () => this.currentContext),
|
|
102
|
+
createSendMediaTool(bus, () => this.currentContext),
|
|
103
|
+
createMemorySearchTool(config.workspace),
|
|
104
|
+
createMemoryGetTool(config.workspace),
|
|
105
|
+
];
|
|
106
|
+
if (config.pluginRegistry) {
|
|
107
|
+
const pluginTools = this.convertPluginTools(config.pluginRegistry.getAllTools());
|
|
108
|
+
tools.push(...pluginTools);
|
|
109
|
+
log.info({ count: pluginTools.length }, 'Loaded plugin tools');
|
|
110
|
+
}
|
|
111
|
+
// Load skills
|
|
112
|
+
const skillResult = this.skillLoader.init(config.workspace, getBundledSkillsDir());
|
|
113
|
+
this.skillPrompt = skillResult.prompt;
|
|
114
|
+
this.skills = skillResult.skills;
|
|
115
|
+
for (const diag of skillResult.diagnostics) {
|
|
116
|
+
if (diag.type === 'collision') {
|
|
117
|
+
log.warn({ skill: diag.skillName, message: diag.message }, 'Skill collision');
|
|
118
|
+
}
|
|
119
|
+
else if (diag.type === 'warning') {
|
|
120
|
+
log.warn({ skill: diag.skillName, message: diag.message }, 'Skill warning');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
log.info({ count: skillResult.skills.length }, 'Skills loaded');
|
|
124
|
+
// Initialize agent
|
|
125
|
+
const registry = this.modelManager.getRegistry();
|
|
126
|
+
let model;
|
|
127
|
+
if (config.model) {
|
|
128
|
+
const found = registry.findByRef(config.model);
|
|
129
|
+
if (found) {
|
|
130
|
+
model = found;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
log.warn({ model: config.model }, 'Model not found, using default');
|
|
134
|
+
model = registry.find('google', 'gemini-2.5-flash-lite-preview-06-17');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
model = registry.find('google', 'gemini-2.5-flash-lite-preview-06-17');
|
|
139
|
+
}
|
|
140
|
+
this.agent = new Agent({
|
|
141
|
+
initialState: {
|
|
142
|
+
systemPrompt: this.getSystemPrompt(),
|
|
143
|
+
model,
|
|
144
|
+
tools,
|
|
145
|
+
messages: [],
|
|
146
|
+
},
|
|
147
|
+
getApiKey: (provider) => {
|
|
148
|
+
if (config.config) {
|
|
149
|
+
return getConfigApiKey(config.config, provider) ?? undefined;
|
|
150
|
+
}
|
|
151
|
+
return undefined;
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
this.unsubscribe = this.agent.subscribe((event) => this.handleEvent(event));
|
|
155
|
+
// Setup shutdown handlers
|
|
156
|
+
process.on('SIGINT', () => this.dispose());
|
|
157
|
+
process.on('SIGTERM', () => this.dispose());
|
|
158
|
+
}
|
|
159
|
+
// ============================================================================
|
|
160
|
+
// Public API
|
|
161
|
+
// ============================================================================
|
|
162
|
+
setChannelManager(channelManager) {
|
|
163
|
+
this.modelManager.setChannelManager(channelManager);
|
|
164
|
+
}
|
|
165
|
+
async switchModelForSession(sessionKey, modelId) {
|
|
166
|
+
const result = await this.modelManager.switchModelForSession(sessionKey, modelId);
|
|
167
|
+
if (result) {
|
|
168
|
+
this.sessionTracker.touchSession(sessionKey);
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
async start() {
|
|
173
|
+
this.running = true;
|
|
174
|
+
await this.triggerHook('gateway_start', { port: 0, host: 'cli' });
|
|
175
|
+
log.info('Agent service started');
|
|
176
|
+
await this.triggerHook('session_start', { sessionId: this.agentId });
|
|
177
|
+
while (this.running) {
|
|
178
|
+
try {
|
|
179
|
+
const msg = await this.bus.consumeInbound();
|
|
180
|
+
await this.handleInboundMessage(msg);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
log.error({ err: error }, 'Error in agent loop');
|
|
184
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
await this.triggerHook('session_end', {
|
|
188
|
+
sessionId: this.agentId,
|
|
189
|
+
messageCount: this.agent.state.messages.length,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
stop() {
|
|
193
|
+
this.running = false;
|
|
194
|
+
this.agent.abort();
|
|
195
|
+
this.unsubscribe?.();
|
|
196
|
+
this.dispose();
|
|
197
|
+
this.triggerHook('gateway_stop', { reason: 'stopped' });
|
|
198
|
+
log.info('Agent service stopped');
|
|
199
|
+
return Promise.resolve();
|
|
200
|
+
}
|
|
201
|
+
async compactSession(sessionKey, instructions) {
|
|
202
|
+
const messages = await this.sessionStore.load(sessionKey);
|
|
203
|
+
const contextWindow = this.getContextWindow();
|
|
204
|
+
const result = await this.sessionStore.compact(sessionKey, messages, contextWindow, instructions);
|
|
205
|
+
if (result.compacted) {
|
|
206
|
+
await this.sessionStore.save(sessionKey, await this.sessionStore.load(sessionKey));
|
|
207
|
+
}
|
|
208
|
+
log.info({ sessionKey, result }, 'Manual compaction complete');
|
|
209
|
+
}
|
|
210
|
+
getSessionStats(sessionKey, messages) {
|
|
211
|
+
return {
|
|
212
|
+
windowStats: this.sessionStore.getWindowStats(messages),
|
|
213
|
+
compactionStats: this.sessionStore.getCompactionStats(sessionKey),
|
|
214
|
+
tokenEstimate: this.sessionStore.estimateTokenUsage(sessionKey, messages),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
async processDirect(content, sessionKey = 'cli:direct', attachments) {
|
|
218
|
+
const messages = await this.sessionStore.load(sessionKey);
|
|
219
|
+
this.agent.replaceMessages(messages);
|
|
220
|
+
const messageContent = [];
|
|
221
|
+
if (content.trim()) {
|
|
222
|
+
messageContent.push({ type: 'text', text: content });
|
|
223
|
+
}
|
|
224
|
+
if (attachments && attachments.length > 0) {
|
|
225
|
+
for (const att of attachments) {
|
|
226
|
+
if (att.type === 'image' || att.mimeType?.startsWith('image/')) {
|
|
227
|
+
const mimeType = att.mimeType || 'image/png';
|
|
228
|
+
const data = att.data || '';
|
|
229
|
+
messageContent.push({ type: 'image', data, mimeType });
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
const fileInfo = `[File: ${att.name || 'unknown'} (${att.mimeType || 'unknown type'}, ${att.size || 0} bytes)]`;
|
|
233
|
+
messageContent.push({ type: 'text', text: fileInfo });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
await this.agent.prompt({
|
|
238
|
+
role: 'user',
|
|
239
|
+
content: messageContent,
|
|
240
|
+
timestamp: Date.now(),
|
|
241
|
+
});
|
|
242
|
+
await this.agent.waitForIdle();
|
|
243
|
+
const response = this.getLastAssistantContent() || '';
|
|
244
|
+
await this.sessionStore.save(sessionKey, this.agent.state.messages);
|
|
245
|
+
return response;
|
|
246
|
+
}
|
|
247
|
+
// ============================================================================
|
|
248
|
+
// Private Methods
|
|
249
|
+
// ============================================================================
|
|
250
|
+
dispose() {
|
|
251
|
+
this.sessionTracker.dispose();
|
|
252
|
+
}
|
|
253
|
+
async triggerHook(event, eventData) {
|
|
254
|
+
if (!this.hookRunner)
|
|
255
|
+
return;
|
|
256
|
+
const ctx = createHookContext({
|
|
257
|
+
pluginId: undefined,
|
|
258
|
+
sessionKey: this.currentContext?.sessionKey,
|
|
259
|
+
agentId: this.agentId,
|
|
260
|
+
timestamp: new Date(),
|
|
261
|
+
});
|
|
262
|
+
try {
|
|
263
|
+
await this.hookRunner.runHooks(event, eventData, ctx);
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
log.warn({ event, err: error }, 'Hook execution failed');
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async runBeforeToolCallHook(_toolName, _params) {
|
|
270
|
+
if (!this.hookRunner)
|
|
271
|
+
return { allowed: true };
|
|
272
|
+
// ... (simplified, full implementation similar to original)
|
|
273
|
+
return { allowed: true };
|
|
274
|
+
}
|
|
275
|
+
async runMessageSendingHook(to, content) {
|
|
276
|
+
if (!this.hookRunner)
|
|
277
|
+
return { send: true, content };
|
|
278
|
+
const ctx = createHookContext({
|
|
279
|
+
sessionKey: this.currentContext?.sessionKey,
|
|
280
|
+
agentId: this.agentId,
|
|
281
|
+
});
|
|
282
|
+
return this.hookRunner.runMessageSending(to, content, ctx);
|
|
283
|
+
}
|
|
284
|
+
convertPluginTools(pluginTools) {
|
|
285
|
+
return pluginTools.map((tool) => ({
|
|
286
|
+
name: tool.name,
|
|
287
|
+
description: tool.description,
|
|
288
|
+
parameters: tool.parameters,
|
|
289
|
+
label: `🔌 ${tool.name}`,
|
|
290
|
+
async execute(toolCallId, params) {
|
|
291
|
+
try {
|
|
292
|
+
const result = await tool.execute(params);
|
|
293
|
+
return { content: [{ type: 'text', text: result }], details: {} };
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
return {
|
|
297
|
+
content: [{ type: 'text', text: `Error: ${error instanceof Error ? error.message : String(error)}` }],
|
|
298
|
+
details: {},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
async handleInboundMessage(msg) {
|
|
305
|
+
const sessionKey = `${msg.channel}:${msg.chat_id}`;
|
|
306
|
+
this.currentContext = { channel: msg.channel, chatId: msg.chat_id, sessionKey };
|
|
307
|
+
try {
|
|
308
|
+
await this.triggerHook('message_received', {
|
|
309
|
+
channelId: msg.channel,
|
|
310
|
+
from: msg.sender_id,
|
|
311
|
+
content: msg.content,
|
|
312
|
+
timestamp: new Date(),
|
|
313
|
+
});
|
|
314
|
+
if (msg.channel === 'system') {
|
|
315
|
+
await this.handleSystemMessage(msg);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
await this.handleUserMessage(msg);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
this.currentContext = null;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async handleUserMessage(msg) {
|
|
326
|
+
const sessionKey = `${msg.channel}:${msg.chat_id}`;
|
|
327
|
+
log.info({ channel: msg.channel, senderId: msg.sender_id }, 'Processing message');
|
|
328
|
+
// Apply model for session
|
|
329
|
+
await this.modelManager.applyModelForSession(this.agent, sessionKey);
|
|
330
|
+
this.sessionTracker.touchSession(sessionKey);
|
|
331
|
+
const typing = createTypingController({
|
|
332
|
+
onStart: async () => {
|
|
333
|
+
await this.bus.publishOutbound({ channel: msg.channel, chat_id: msg.chat_id, type: 'typing_on' });
|
|
334
|
+
},
|
|
335
|
+
onStop: async () => {
|
|
336
|
+
await this.bus.publishOutbound({ channel: msg.channel, chat_id: msg.chat_id, type: 'typing_off' });
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
try {
|
|
340
|
+
typing.start();
|
|
341
|
+
const command = msg.content.trim();
|
|
342
|
+
// Handle commands
|
|
343
|
+
if (command === '/reset' || command === '/new') {
|
|
344
|
+
// Archive current session before starting a new one
|
|
345
|
+
const messages = await this.sessionStore.load(sessionKey);
|
|
346
|
+
if (messages.length > 0) {
|
|
347
|
+
await this.sessionStore.archive(sessionKey);
|
|
348
|
+
log.info({ sessionKey, messageCount: messages.length }, 'Session archived due to /new command');
|
|
349
|
+
}
|
|
350
|
+
// Clear current session state to start fresh
|
|
351
|
+
await this.sessionStore.deleteSession(sessionKey);
|
|
352
|
+
this.sessionTracker.deleteSession(sessionKey);
|
|
353
|
+
await this.bus.publishOutbound({
|
|
354
|
+
channel: msg.channel,
|
|
355
|
+
chat_id: msg.chat_id,
|
|
356
|
+
content: '✅ New session started. Previous session has been archived.',
|
|
357
|
+
type: 'message',
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (command === '/skills reload') {
|
|
362
|
+
this.reloadSkills();
|
|
363
|
+
await this.bus.publishOutbound({
|
|
364
|
+
channel: msg.channel,
|
|
365
|
+
chat_id: msg.chat_id,
|
|
366
|
+
content: '✅ Skills reloaded successfully',
|
|
367
|
+
type: 'message',
|
|
368
|
+
});
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
// Check plugin commands
|
|
372
|
+
if (msg.content.startsWith('/') && this.config.pluginRegistry) {
|
|
373
|
+
const commandName = command.slice(1).split(/\s+/)[0];
|
|
374
|
+
const pluginCommand = this.config.pluginRegistry.getCommand(commandName);
|
|
375
|
+
if (pluginCommand) {
|
|
376
|
+
const ctx = {
|
|
377
|
+
senderId: msg.sender_id,
|
|
378
|
+
channel: msg.channel,
|
|
379
|
+
isAuthorized: true,
|
|
380
|
+
config: this.config.config,
|
|
381
|
+
};
|
|
382
|
+
const args = command.replace(/^\/\w+\s*/, '');
|
|
383
|
+
const result = await pluginCommand.handler(args, ctx);
|
|
384
|
+
await this.bus.publishOutbound({
|
|
385
|
+
channel: msg.channel,
|
|
386
|
+
chat_id: msg.chat_id,
|
|
387
|
+
content: result.content,
|
|
388
|
+
type: 'message',
|
|
389
|
+
});
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
// Process message
|
|
394
|
+
const expandedContent = this.expandSkillCommand(msg.content);
|
|
395
|
+
await this.triggerHook('before_agent_start', { prompt: expandedContent });
|
|
396
|
+
let messages = await this.sessionStore.load(sessionKey);
|
|
397
|
+
await this.checkAndCompact(sessionKey, messages);
|
|
398
|
+
messages = await this.sessionStore.load(sessionKey);
|
|
399
|
+
this.agent.replaceMessages(messages);
|
|
400
|
+
// Build message content with text and attachments
|
|
401
|
+
const messageContent = [];
|
|
402
|
+
// Add text content
|
|
403
|
+
if (expandedContent.trim()) {
|
|
404
|
+
messageContent.push({ type: 'text', text: expandedContent });
|
|
405
|
+
}
|
|
406
|
+
// Add attachments from inbound message
|
|
407
|
+
const attachments = msg.attachments;
|
|
408
|
+
if (attachments && attachments.length > 0) {
|
|
409
|
+
for (const att of attachments) {
|
|
410
|
+
if (att.type === 'photo' || att.mimeType?.startsWith('image/')) {
|
|
411
|
+
const mimeType = att.mimeType || 'image/jpeg';
|
|
412
|
+
messageContent.push({ type: 'image', data: att.data, mimeType });
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
// For non-image files, add as text description
|
|
416
|
+
const fileInfo = `[File: ${att.name || att.type} (${att.mimeType || 'unknown type'}, ${att.size || 0} bytes)]`;
|
|
417
|
+
messageContent.push({ type: 'text', text: fileInfo });
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const userMessage = {
|
|
422
|
+
role: 'user',
|
|
423
|
+
content: messageContent,
|
|
424
|
+
timestamp: Date.now(),
|
|
425
|
+
};
|
|
426
|
+
// Run with fallback
|
|
427
|
+
const result = await this.modelManager.runWithFallback(this.agent, sessionKey, userMessage, this.modelManager.getCurrentProvider(), this.modelManager.getCurrentModel());
|
|
428
|
+
// Track usage
|
|
429
|
+
if (result.usage) {
|
|
430
|
+
this.sessionTracker.updateUsage(sessionKey, {
|
|
431
|
+
prompt: result.usage.prompt_tokens,
|
|
432
|
+
completion: result.usage.completion_tokens,
|
|
433
|
+
total: result.usage.total_tokens,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
// Send response
|
|
437
|
+
if (result.content) {
|
|
438
|
+
let contentWithUsage = result.content;
|
|
439
|
+
if (result.usage && msg.channel === 'telegram') {
|
|
440
|
+
const modelName = this.modelManager.getCurrentModel().split('/').pop() || 'unknown';
|
|
441
|
+
contentWithUsage += `\n\n📊 *${modelName}* \`+${result.usage.prompt_tokens} → ${result.usage.completion_tokens} = ${result.usage.total_tokens}\``;
|
|
442
|
+
}
|
|
443
|
+
// Run message_sending hook for plugin interception
|
|
444
|
+
const hookResult = await this.runMessageSendingHook(msg.chat_id, contentWithUsage);
|
|
445
|
+
if (!hookResult.send) {
|
|
446
|
+
log.info({ chatId: msg.chat_id, reason: hookResult.reason }, 'Message sending blocked by hook');
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
await this.bus.publishOutbound({
|
|
450
|
+
channel: msg.channel,
|
|
451
|
+
chat_id: msg.chat_id,
|
|
452
|
+
content: hookResult.content || contentWithUsage,
|
|
453
|
+
type: 'message',
|
|
454
|
+
});
|
|
455
|
+
await this.triggerHook('message_sent', { to: msg.chat_id, content: hookResult.content || contentWithUsage, success: true });
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
await this.sessionStore.save(sessionKey, this.agent.state.messages);
|
|
459
|
+
await this.triggerHook('agent_end', { messages: this.agent.state.messages, success: true, durationMs: 0 });
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
typing.stop();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async handleSystemMessage(msg) {
|
|
466
|
+
log.info({ senderId: msg.sender_id }, 'Processing system message');
|
|
467
|
+
if (msg.content === '/usage' && msg.sender_id === 'telegram:usage') {
|
|
468
|
+
const sessionKey = msg.metadata?.sessionKey || `telegram:${msg.chat_id}`;
|
|
469
|
+
const usage = this.sessionTracker.getUsage(sessionKey);
|
|
470
|
+
this.sessionTracker.touchSession(sessionKey);
|
|
471
|
+
if (usage) {
|
|
472
|
+
const modelName = this.modelManager.getCurrentModel().split('/').pop() || 'unknown';
|
|
473
|
+
await this.bus.publishOutbound({
|
|
474
|
+
channel: 'telegram',
|
|
475
|
+
chat_id: msg.chat_id,
|
|
476
|
+
content: '📊 *Session Token Usage*\n\n' +
|
|
477
|
+
`🤖 Model: ${modelName}\n` +
|
|
478
|
+
`📥 Prompt: ${usage.prompt.toLocaleString()} tokens\n` +
|
|
479
|
+
`📤 Completion: ${usage.completion.toLocaleString()} tokens\n` +
|
|
480
|
+
`📊 Total: ${usage.total.toLocaleString()} tokens`,
|
|
481
|
+
type: 'message',
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
await this.bus.publishOutbound({
|
|
486
|
+
channel: 'telegram',
|
|
487
|
+
chat_id: msg.chat_id,
|
|
488
|
+
content: '📊 No usage data available for this session yet.',
|
|
489
|
+
type: 'message',
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
// Parse origin channel
|
|
495
|
+
let originChannel = 'cli';
|
|
496
|
+
let originChatId = msg.chat_id;
|
|
497
|
+
if (msg.chat_id.includes(':')) {
|
|
498
|
+
const [ch, ...rest] = msg.chat_id.split(':');
|
|
499
|
+
originChannel = ch;
|
|
500
|
+
originChatId = rest.join(':');
|
|
501
|
+
}
|
|
502
|
+
const sessionKey = `${originChannel}:${originChatId}`;
|
|
503
|
+
let messages = await this.sessionStore.load(sessionKey);
|
|
504
|
+
await this.checkAndCompact(sessionKey, messages);
|
|
505
|
+
messages = await this.sessionStore.load(sessionKey);
|
|
506
|
+
this.agent.replaceMessages(messages);
|
|
507
|
+
const systemMessage = {
|
|
508
|
+
role: 'user',
|
|
509
|
+
content: [{ type: 'text', text: `[System: ${msg.sender_id}] ${msg.content}` }],
|
|
510
|
+
timestamp: Date.now(),
|
|
511
|
+
};
|
|
512
|
+
await this.agent.prompt(systemMessage);
|
|
513
|
+
await this.agent.waitForIdle();
|
|
514
|
+
const finalContent = this.getLastAssistantContent();
|
|
515
|
+
if (finalContent) {
|
|
516
|
+
// Run message_sending hook for plugin interception
|
|
517
|
+
const hookResult = await this.runMessageSendingHook(originChatId, finalContent);
|
|
518
|
+
if (!hookResult.send) {
|
|
519
|
+
log.info({ chatId: originChatId, reason: hookResult.reason }, 'System message sending blocked by hook');
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
await this.bus.publishOutbound({
|
|
523
|
+
channel: originChannel,
|
|
524
|
+
chat_id: originChatId,
|
|
525
|
+
content: hookResult.content || finalContent,
|
|
526
|
+
type: 'message',
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
await this.sessionStore.save(sessionKey, this.agent.state.messages);
|
|
531
|
+
}
|
|
532
|
+
async checkAndCompact(sessionKey, messages) {
|
|
533
|
+
const contextWindow = this.getContextWindow();
|
|
534
|
+
const prep = this.sessionStore.prepareCompaction(sessionKey, messages, contextWindow);
|
|
535
|
+
if (!prep.needsCompaction)
|
|
536
|
+
return;
|
|
537
|
+
log.info({ sessionKey, reason: prep.stats?.reason, usagePercent: prep.stats?.usagePercent }, 'Session needs compaction');
|
|
538
|
+
try {
|
|
539
|
+
const result = await this.sessionStore.compact(sessionKey, messages, contextWindow);
|
|
540
|
+
await this.triggerHook('after_compaction', {
|
|
541
|
+
messageCount: messages.length,
|
|
542
|
+
tokenCount: result.tokensBefore,
|
|
543
|
+
compactedCount: messages.length - result.firstKeptIndex,
|
|
544
|
+
});
|
|
545
|
+
log.info({ sessionKey, tokensBefore: result.tokensBefore, tokensAfter: result.tokensAfter }, 'Session compacted');
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
log.error({ err: error, sessionKey }, 'Failed to compact session');
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
getContextWindow() {
|
|
552
|
+
const defaults = this.config.agentDefaults || this.config.config?.agents?.defaults;
|
|
553
|
+
return defaults?.maxTokens ? defaults.maxTokens * 4 : 128000;
|
|
554
|
+
}
|
|
555
|
+
handleEvent(event) {
|
|
556
|
+
switch (event.type) {
|
|
557
|
+
case 'agent_start':
|
|
558
|
+
log.debug('Agent turn started');
|
|
559
|
+
break;
|
|
560
|
+
case 'turn_start':
|
|
561
|
+
log.debug('Turn started');
|
|
562
|
+
break;
|
|
563
|
+
case 'message_start':
|
|
564
|
+
if (event.message.role === 'assistant')
|
|
565
|
+
log.debug('Assistant response starting');
|
|
566
|
+
break;
|
|
567
|
+
case 'message_end':
|
|
568
|
+
if (event.message.role === 'assistant') {
|
|
569
|
+
const content = event.message.content;
|
|
570
|
+
const text = Array.isArray(content)
|
|
571
|
+
? extractTextContent(content)
|
|
572
|
+
: String(content);
|
|
573
|
+
log.debug({ contentLength: text.length }, 'Assistant response complete');
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
case 'tool_execution_start':
|
|
577
|
+
log.debug({ tool: event.toolName }, 'Tool execution started');
|
|
578
|
+
break;
|
|
579
|
+
case 'tool_execution_end':
|
|
580
|
+
log.debug({ tool: event.toolName, isError: event.isError }, 'Tool execution complete');
|
|
581
|
+
break;
|
|
582
|
+
case 'turn_end':
|
|
583
|
+
log.debug('Turn complete');
|
|
584
|
+
break;
|
|
585
|
+
case 'agent_end':
|
|
586
|
+
log.debug('Agent turn ended');
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
getLastAssistantContent() {
|
|
591
|
+
const messages = this.agent.state.messages;
|
|
592
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
593
|
+
if (messages[i].role === 'assistant') {
|
|
594
|
+
const content = messages[i].content;
|
|
595
|
+
if (Array.isArray(content)) {
|
|
596
|
+
return extractTextContent(content);
|
|
597
|
+
}
|
|
598
|
+
return String(content);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
getSystemPrompt() {
|
|
604
|
+
const prompt = PromptBuilder.createFullPrompt({ workspaceDir: this.config.workspace }, { heartbeatEnabled: false, contextFiles: this.bootstrapFiles });
|
|
605
|
+
return this.skillPrompt ? prompt + '\n\n' + this.skillPrompt : prompt;
|
|
606
|
+
}
|
|
607
|
+
reloadSkills() {
|
|
608
|
+
const skillResult = this.skillLoader.reload();
|
|
609
|
+
this.skillPrompt = skillResult.prompt;
|
|
610
|
+
this.skills = skillResult.skills;
|
|
611
|
+
for (const diag of skillResult.diagnostics) {
|
|
612
|
+
if (diag.type === 'collision') {
|
|
613
|
+
log.warn({ skill: diag.skillName, message: diag.message }, 'Skill collision');
|
|
614
|
+
}
|
|
615
|
+
else if (diag.type === 'warning') {
|
|
616
|
+
log.warn({ skill: diag.skillName, message: diag.message }, 'Skill warning');
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
log.info({ count: skillResult.skills.length }, 'Skills reloaded');
|
|
620
|
+
}
|
|
621
|
+
expandSkillCommand(text) {
|
|
622
|
+
if (!text.startsWith('/skill:'))
|
|
623
|
+
return text;
|
|
624
|
+
const spaceIndex = text.indexOf(' ');
|
|
625
|
+
const skillName = spaceIndex === -1 ? text.slice(7) : text.slice(7, spaceIndex);
|
|
626
|
+
const args = spaceIndex === -1 ? undefined : text.slice(spaceIndex + 1).trim();
|
|
627
|
+
const skill = this.skills.find((s) => s.name === skillName);
|
|
628
|
+
if (!skill)
|
|
629
|
+
return text;
|
|
630
|
+
try {
|
|
631
|
+
const rawContent = readFileSync(skill.filePath, 'utf-8');
|
|
632
|
+
const body = rawContent.replace(/^---[\s\S]*?---\n*/, '');
|
|
633
|
+
const skillBlock = `<skill name="${skill.name}" location="${skill.filePath}">\nReferences are relative to ${skill.baseDir}.\n\n${body}\n</skill>`;
|
|
634
|
+
return args ? `${skillBlock}\n\n${args}` : skillBlock;
|
|
635
|
+
}
|
|
636
|
+
catch {
|
|
637
|
+
return text;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
//# sourceMappingURL=service.js.map
|