@mastra/code-sdk 0.0.0-agent-tags-alpha-20261125185303
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +32 -0
- package/README.md +284 -0
- package/dist/acp/agent.d.ts +48 -0
- package/dist/acp/agent.js +414 -0
- package/dist/acp/errors.d.ts +3 -0
- package/dist/acp/errors.js +11 -0
- package/dist/acp/event-mapper.d.ts +28 -0
- package/dist/acp/event-mapper.js +274 -0
- package/dist/acp/index.d.ts +9 -0
- package/dist/acp/index.js +21 -0
- package/dist/acp/runtime.d.ts +5 -0
- package/dist/acp/runtime.js +91 -0
- package/dist/acp/server.d.ts +4 -0
- package/dist/acp/server.js +35 -0
- package/dist/acp/skills.d.ts +6 -0
- package/dist/acp/skills.js +30 -0
- package/dist/acp.d.ts +2 -0
- package/dist/acp.js +1 -0
- package/dist/agent-connections/messaging-processor.d.ts +17 -0
- package/dist/agent-connections/messaging-processor.js +131 -0
- package/dist/agent-connections/ownership.d.ts +24 -0
- package/dist/agent-connections/ownership.js +96 -0
- package/dist/agent-connections/registry.d.ts +20 -0
- package/dist/agent-connections/registry.js +167 -0
- package/dist/agent-connections/session-advertisement.d.ts +27 -0
- package/dist/agent-connections/session-advertisement.js +86 -0
- package/dist/agent-connections/signal-provider.d.ts +141 -0
- package/dist/agent-connections/signal-provider.js +35 -0
- package/dist/agent-connections/state-processor.d.ts +23 -0
- package/dist/agent-connections/state-processor.js +270 -0
- package/dist/agent-connections/thread-state.d.ts +41 -0
- package/dist/agent-connections/thread-state.js +157 -0
- package/dist/agent-connections/tools.d.ts +133 -0
- package/dist/agent-connections/tools.js +616 -0
- package/dist/agent-connections/types.d.ts +99 -0
- package/dist/agent-connections/types.js +4 -0
- package/dist/agent-connections/untrusted-text.d.ts +5 -0
- package/dist/agent-connections/untrusted-text.js +19 -0
- package/dist/agents/background-completion-events.d.ts +16 -0
- package/dist/agents/background-completion-events.js +16 -0
- package/dist/agents/background-completion.d.ts +22 -0
- package/dist/agents/background-completion.js +73 -0
- package/dist/agents/context-audit.d.ts +84 -0
- package/dist/agents/context-audit.js +115 -0
- package/dist/agents/credential-resolver.d.ts +74 -0
- package/dist/agents/credential-resolver.js +142 -0
- package/dist/agents/custom-provider-source.d.ts +35 -0
- package/dist/agents/custom-provider-source.js +22 -0
- package/dist/agents/fastembed-subprocess.d.ts +52 -0
- package/dist/agents/fastembed-subprocess.js +282 -0
- package/dist/agents/instructions.d.ts +42 -0
- package/dist/agents/instructions.js +69 -0
- package/dist/agents/mastracode-gateway.d.ts +85 -0
- package/dist/agents/mastracode-gateway.js +500 -0
- package/dist/agents/memory.d.ts +36 -0
- package/dist/agents/memory.js +248 -0
- package/dist/agents/model.d.ts +158 -0
- package/dist/agents/model.js +382 -0
- package/dist/agents/modes/build.d.ts +12 -0
- package/dist/agents/modes/build.js +33 -0
- package/dist/agents/modes/explore.d.ts +10 -0
- package/dist/agents/modes/explore.js +25 -0
- package/dist/agents/modes/plan.d.ts +8 -0
- package/dist/agents/modes/plan.js +43 -0
- package/dist/agents/prompts/agent-instructions.d.ts +122 -0
- package/dist/agents/prompts/agent-instructions.js +617 -0
- package/dist/agents/prompts/build.d.ts +12 -0
- package/dist/agents/prompts/build.js +73 -0
- package/dist/agents/prompts/fast.d.ts +5 -0
- package/dist/agents/prompts/fast.js +23 -0
- package/dist/agents/prompts/index.d.ts +61 -0
- package/dist/agents/prompts/index.js +141 -0
- package/dist/agents/prompts/model.d.ts +5 -0
- package/dist/agents/prompts/model.js +20 -0
- package/dist/agents/prompts/plan.d.ts +11 -0
- package/dist/agents/prompts/plan.js +132 -0
- package/dist/agents/prompts/tool-guidance.d.ts +18 -0
- package/dist/agents/prompts/tool-guidance.js +158 -0
- package/dist/agents/sandbox-filesystem.d.ts +232 -0
- package/dist/agents/sandbox-filesystem.js +851 -0
- package/dist/agents/subagents/execute.d.ts +3 -0
- package/dist/agents/subagents/execute.js +24 -0
- package/dist/agents/subagents/explore.d.ts +3 -0
- package/dist/agents/subagents/explore.js +15 -0
- package/dist/agents/subagents/index.d.ts +6 -0
- package/dist/agents/subagents/index.js +15 -0
- package/dist/agents/subagents/plan.d.ts +3 -0
- package/dist/agents/subagents/plan.js +15 -0
- package/dist/agents/thread-caveman-state.d.ts +17 -0
- package/dist/agents/thread-caveman-state.js +104 -0
- package/dist/agents/tool-availability.d.ts +17 -0
- package/dist/agents/tool-availability.js +134 -0
- package/dist/agents/tools.d.ts +37 -0
- package/dist/agents/tools.js +154 -0
- package/dist/agents/workflow-builder-agent.d.ts +2 -0
- package/dist/agents/workflow-builder-agent.js +79 -0
- package/dist/agents/workspace.d.ts +35 -0
- package/dist/agents/workspace.js +207 -0
- package/dist/analytics.d.ts +18 -0
- package/dist/analytics.js +159 -0
- package/dist/auth/account-rotation-processor.d.ts +210 -0
- package/dist/auth/account-rotation-processor.js +662 -0
- package/dist/auth/account-routing-context.d.ts +17 -0
- package/dist/auth/account-routing-context.js +38 -0
- package/dist/auth/authorization-input.d.ts +16 -0
- package/dist/auth/authorization-input.js +39 -0
- package/dist/auth/device-code.d.ts +93 -0
- package/dist/auth/device-code.js +126 -0
- package/dist/auth/index.d.ts +13 -0
- package/dist/auth/index.js +9 -0
- package/dist/auth/pkce.d.ts +13 -0
- package/dist/auth/pkce.js +29 -0
- package/dist/auth/provider-auth-error.d.ts +24 -0
- package/dist/auth/provider-auth-error.js +44 -0
- package/dist/auth/providers/anthropic.d.ts +40 -0
- package/dist/auth/providers/anthropic.js +117 -0
- package/dist/auth/providers/github-copilot.d.ts +154 -0
- package/dist/auth/providers/github-copilot.js +359 -0
- package/dist/auth/providers/kimi-coding.d.ts +38 -0
- package/dist/auth/providers/kimi-coding.js +225 -0
- package/dist/auth/providers/openai-codex.d.ts +141 -0
- package/dist/auth/providers/openai-codex.js +531 -0
- package/dist/auth/providers/xai.d.ts +64 -0
- package/dist/auth/providers/xai.js +223 -0
- package/dist/auth/storage.d.ts +222 -0
- package/dist/auth/storage.js +805 -0
- package/dist/auth/types.d.ts +164 -0
- package/dist/auth/types.js +1 -0
- package/dist/clipboard/index.d.ts +26 -0
- package/dist/clipboard/index.js +224 -0
- package/dist/constants.d.ts +29 -0
- package/dist/constants.js +34 -0
- package/dist/error-classification.d.ts +10 -0
- package/dist/error-classification.js +19 -0
- package/dist/evals/context-builder.d.ts +41 -0
- package/dist/evals/context-builder.js +156 -0
- package/dist/evals/index.d.ts +4 -0
- package/dist/evals/index.js +5 -0
- package/dist/evals/scorers/classify-command.d.ts +30 -0
- package/dist/evals/scorers/classify-command.js +61 -0
- package/dist/evals/scorers/efficiency.d.ts +44 -0
- package/dist/evals/scorers/efficiency.js +256 -0
- package/dist/evals/scorers/extract-tools.d.ts +25 -0
- package/dist/evals/scorers/extract-tools.js +57 -0
- package/dist/evals/scorers/index.d.ts +3 -0
- package/dist/evals/scorers/index.js +3 -0
- package/dist/evals/scorers/outcome.d.ts +49 -0
- package/dist/evals/scorers/outcome.js +360 -0
- package/dist/experimental-agent.d.ts +19 -0
- package/dist/experimental-agent.js +45 -0
- package/dist/factory/client.d.ts +44 -0
- package/dist/factory/client.js +110 -0
- package/dist/factory/remote-controller.d.ts +34 -0
- package/dist/factory/remote-controller.js +40 -0
- package/dist/factory/remote-session.d.ts +210 -0
- package/dist/factory/remote-session.js +415 -0
- package/dist/goal-manager.d.ts +114 -0
- package/dist/goal-manager.js +351 -0
- package/dist/goal-signal.d.ts +14 -0
- package/dist/goal-signal.js +14 -0
- package/dist/headless/cli.d.ts +55 -0
- package/dist/headless/cli.js +261 -0
- package/dist/headless/flags.d.ts +33 -0
- package/dist/headless/flags.js +207 -0
- package/dist/headless/format.d.ts +41 -0
- package/dist/headless/format.js +94 -0
- package/dist/headless/index.d.ts +25 -0
- package/dist/headless/index.js +6 -0
- package/dist/headless/policy.d.ts +17 -0
- package/dist/headless/policy.js +39 -0
- package/dist/headless/run-mc.d.ts +7 -0
- package/dist/headless/run-mc.js +438 -0
- package/dist/headless/types.d.ts +175 -0
- package/dist/headless/types.js +16 -0
- package/dist/hooks/config.d.ts +11 -0
- package/dist/hooks/config.js +77 -0
- package/dist/hooks/executor.d.ts +13 -0
- package/dist/hooks/executor.js +128 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/manager.d.ts +75 -0
- package/dist/hooks/manager.js +289 -0
- package/dist/hooks/types.d.ts +180 -0
- package/dist/hooks/types.js +5 -0
- package/dist/index.d.ts +537 -0
- package/dist/index.js +1399 -0
- package/dist/ipc/ipc-reporter.d.ts +14 -0
- package/dist/ipc/ipc-reporter.js +18 -0
- package/dist/knowledge-inspector.d.ts +139 -0
- package/dist/knowledge-inspector.js +533 -0
- package/dist/knowledge-scope-id.d.ts +7 -0
- package/dist/knowledge-scope-id.js +10 -0
- package/dist/knowledge-scope.d.ts +41 -0
- package/dist/knowledge-scope.js +85 -0
- package/dist/lsp/client.d.ts +56 -0
- package/dist/lsp/client.js +248 -0
- package/dist/lsp/index.d.ts +4 -0
- package/dist/lsp/index.js +4 -0
- package/dist/lsp/language.d.ts +9 -0
- package/dist/lsp/language.js +41 -0
- package/dist/lsp/manager.d.ts +34 -0
- package/dist/lsp/manager.js +79 -0
- package/dist/lsp/server.d.ts +27 -0
- package/dist/lsp/server.js +158 -0
- package/dist/lsp/workspace.d.ts +9 -0
- package/dist/lsp/workspace.js +52 -0
- package/dist/mcp/config.d.ts +77 -0
- package/dist/mcp/config.js +313 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/manager.d.ts +104 -0
- package/dist/mcp/manager.js +693 -0
- package/dist/mcp/state.d.ts +35 -0
- package/dist/mcp/state.js +94 -0
- package/dist/mcp/types.d.ts +139 -0
- package/dist/mcp/types.js +1 -0
- package/dist/onboarding/custom-packs.d.ts +7 -0
- package/dist/onboarding/custom-packs.js +17 -0
- package/dist/onboarding/custom-providers.d.ts +4 -0
- package/dist/onboarding/custom-providers.js +27 -0
- package/dist/onboarding/index.d.ts +5 -0
- package/dist/onboarding/index.js +3 -0
- package/dist/onboarding/om-settings.d.ts +47 -0
- package/dist/onboarding/om-settings.js +80 -0
- package/dist/onboarding/pack-activation.d.ts +43 -0
- package/dist/onboarding/pack-activation.js +105 -0
- package/dist/onboarding/packs.d.ts +106 -0
- package/dist/onboarding/packs.js +283 -0
- package/dist/onboarding/settings.d.ts +563 -0
- package/dist/onboarding/settings.js +1110 -0
- package/dist/permissions.d.ts +55 -0
- package/dist/permissions.js +139 -0
- package/dist/platform/index.d.ts +74 -0
- package/dist/platform/index.js +199 -0
- package/dist/plugin.d.ts +156 -0
- package/dist/plugin.js +27 -0
- package/dist/plugins/dependencies.d.ts +9 -0
- package/dist/plugins/dependencies.js +84 -0
- package/dist/plugins/install.d.ts +21 -0
- package/dist/plugins/install.js +187 -0
- package/dist/plugins/loader.d.ts +25 -0
- package/dist/plugins/loader.js +264 -0
- package/dist/plugins/manager.d.ts +115 -0
- package/dist/plugins/manager.js +507 -0
- package/dist/plugins/manifest.d.ts +14 -0
- package/dist/plugins/manifest.js +47 -0
- package/dist/plugins/package-link.d.ts +3 -0
- package/dist/plugins/package-link.js +58 -0
- package/dist/plugins/paths.d.ts +10 -0
- package/dist/plugins/paths.js +22 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +109 -0
- package/dist/plugins/scaffold.d.ts +10 -0
- package/dist/plugins/scaffold.js +115 -0
- package/dist/plugins/signal-lane.d.ts +59 -0
- package/dist/plugins/signal-lane.js +163 -0
- package/dist/plugins/types.d.ts +95 -0
- package/dist/plugins/types.js +1 -0
- package/dist/process-memory-diagnostics.d.ts +136 -0
- package/dist/process-memory-diagnostics.js +497 -0
- package/dist/processors/plan-rejection-abort.d.ts +16 -0
- package/dist/processors/plan-rejection-abort.js +62 -0
- package/dist/providers/amazon-bedrock-gateway.d.ts +47 -0
- package/dist/providers/amazon-bedrock-gateway.js +142 -0
- package/dist/providers/amazon-bedrock.d.ts +28 -0
- package/dist/providers/amazon-bedrock.js +99 -0
- package/dist/providers/anthropic-prompt-cache.d.ts +19 -0
- package/dist/providers/anthropic-prompt-cache.js +8 -0
- package/dist/providers/anthropic-thinking.d.ts +5 -0
- package/dist/providers/anthropic-thinking.js +22 -0
- package/dist/providers/claude-max.d.ts +67 -0
- package/dist/providers/claude-max.js +232 -0
- package/dist/providers/github-copilot-gateway.d.ts +35 -0
- package/dist/providers/github-copilot-gateway.js +58 -0
- package/dist/providers/github-copilot.d.ts +60 -0
- package/dist/providers/github-copilot.js +320 -0
- package/dist/providers/google-thinking.d.ts +31 -0
- package/dist/providers/google-thinking.js +64 -0
- package/dist/providers/google-vertex-gateway.d.ts +23 -0
- package/dist/providers/google-vertex-gateway.js +49 -0
- package/dist/providers/google-vertex.d.ts +6 -0
- package/dist/providers/google-vertex.js +51 -0
- package/dist/providers/kimi-coding.d.ts +14 -0
- package/dist/providers/kimi-coding.js +80 -0
- package/dist/providers/model-ids.d.ts +53 -0
- package/dist/providers/model-ids.js +132 -0
- package/dist/providers/openai-codex.d.ts +114 -0
- package/dist/providers/openai-codex.js +427 -0
- package/dist/providers/provider-thinking.d.ts +23 -0
- package/dist/providers/provider-thinking.js +61 -0
- package/dist/providers/thinking-request.d.ts +14 -0
- package/dist/providers/thinking-request.js +87 -0
- package/dist/providers/xai.d.ts +33 -0
- package/dist/providers/xai.js +72 -0
- package/dist/schedules/index.d.ts +10 -0
- package/dist/schedules/index.js +6 -0
- package/dist/schedules/interval.d.ts +49 -0
- package/dist/schedules/interval.js +212 -0
- package/dist/schedules/prompt.d.ts +36 -0
- package/dist/schedules/prompt.js +36 -0
- package/dist/schedules/run-script.d.ts +8 -0
- package/dist/schedules/run-script.js +80 -0
- package/dist/schedules/scheduler.d.ts +101 -0
- package/dist/schedules/scheduler.js +167 -0
- package/dist/schedules/tools.d.ts +57 -0
- package/dist/schedules/tools.js +217 -0
- package/dist/schema.d.ts +499 -0
- package/dist/schema.js +131 -0
- package/dist/theme-palette.d.ts +17 -0
- package/dist/theme-palette.js +18 -0
- package/dist/thinking-catalog.d.ts +4 -0
- package/dist/thinking-catalog.js +13 -0
- package/dist/thinking.d.ts +37 -0
- package/dist/thinking.js +134 -0
- package/dist/tool-names.d.ts +77 -0
- package/dist/tool-names.js +53 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/request-sandbox-access.d.ts +6 -0
- package/dist/tools/request-sandbox-access.js +80 -0
- package/dist/tools/utils.d.ts +20 -0
- package/dist/tools/utils.js +31 -0
- package/dist/tools/web-search.d.ts +116 -0
- package/dist/tools/web-search.js +158 -0
- package/dist/tools/workflows/create-workflow.d.ts +7 -0
- package/dist/tools/workflows/create-workflow.js +82 -0
- package/dist/tools/workflows/delete-workflow.d.ts +7 -0
- package/dist/tools/workflows/delete-workflow.js +18 -0
- package/dist/tools/workflows/ephemeral-memory.d.ts +28 -0
- package/dist/tools/workflows/ephemeral-memory.js +41 -0
- package/dist/tools/workflows/extract-json-schema.d.ts +12 -0
- package/dist/tools/workflows/extract-json-schema.js +19 -0
- package/dist/tools/workflows/get-workflow.d.ts +11 -0
- package/dist/tools/workflows/get-workflow.js +31 -0
- package/dist/tools/workflows/list-available-agents.d.ts +8 -0
- package/dist/tools/workflows/list-available-agents.js +29 -0
- package/dist/tools/workflows/list-available-classifiers.d.ts +19 -0
- package/dist/tools/workflows/list-available-classifiers.js +53 -0
- package/dist/tools/workflows/list-available-tools.d.ts +9 -0
- package/dist/tools/workflows/list-available-tools.js +37 -0
- package/dist/tools/workflows/list-available-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-available-workflows.js +29 -0
- package/dist/tools/workflows/list-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-workflows.js +30 -0
- package/dist/tools/workflows/run-workflow.d.ts +9 -0
- package/dist/tools/workflows/run-workflow.js +49 -0
- package/dist/tools/workflows/save-workflow.d.ts +1125 -0
- package/dist/tools/workflows/save-workflow.js +25 -0
- package/dist/tools/workflows/tool-ids.d.ts +16 -0
- package/dist/tools/workflows/tool-ids.js +17 -0
- package/dist/utils/binaries.d.ts +7 -0
- package/dist/utils/binaries.js +97 -0
- package/dist/utils/debug-log.d.ts +12 -0
- package/dist/utils/debug-log.js +56 -0
- package/dist/utils/discarding-scores-storage.d.ts +33 -0
- package/dist/utils/discarding-scores-storage.js +51 -0
- package/dist/utils/errors.d.ts +36 -0
- package/dist/utils/errors.js +215 -0
- package/dist/utils/gateway-sync.d.ts +26 -0
- package/dist/utils/gateway-sync.js +58 -0
- package/dist/utils/local-observability.d.ts +19 -0
- package/dist/utils/local-observability.js +54 -0
- package/dist/utils/maintenance-lock.d.ts +42 -0
- package/dist/utils/maintenance-lock.js +196 -0
- package/dist/utils/notification-dispatch.d.ts +74 -0
- package/dist/utils/notification-dispatch.js +128 -0
- package/dist/utils/open-url.d.ts +10 -0
- package/dist/utils/open-url.js +27 -0
- package/dist/utils/path-security.d.ts +2 -0
- package/dist/utils/path-security.js +7 -0
- package/dist/utils/plan-diff.d.ts +25 -0
- package/dist/utils/plan-diff.js +59 -0
- package/dist/utils/plans.d.ts +61 -0
- package/dist/utils/plans.js +120 -0
- package/dist/utils/project.d.ts +160 -0
- package/dist/utils/project.js +361 -0
- package/dist/utils/prune-cli.d.ts +24 -0
- package/dist/utils/prune-cli.js +124 -0
- package/dist/utils/signals-pubsub.d.ts +103 -0
- package/dist/utils/signals-pubsub.js +409 -0
- package/dist/utils/slash-command-loader.d.ts +52 -0
- package/dist/utils/slash-command-loader.js +139 -0
- package/dist/utils/slash-command-processor.d.ts +14 -0
- package/dist/utils/slash-command-processor.js +106 -0
- package/dist/utils/stdin-pipe.d.ts +33 -0
- package/dist/utils/stdin-pipe.js +93 -0
- package/dist/utils/storage-factory.d.ts +50 -0
- package/dist/utils/storage-factory.js +118 -0
- package/dist/utils/storage-maintenance.d.ts +121 -0
- package/dist/utils/storage-maintenance.js +352 -0
- package/dist/utils/thread-lock.d.ts +26 -0
- package/dist/utils/thread-lock.js +109 -0
- package/dist/utils/token-estimator.d.ts +3 -0
- package/dist/utils/token-estimator.js +19 -0
- package/dist/utils/update-check.d.ts +151 -0
- package/dist/utils/update-check.js +430 -0
- package/dist/utils/wake-session.d.ts +44 -0
- package/dist/utils/wake-session.js +83 -0
- package/dist/voice/stt-registry.d.ts +67 -0
- package/dist/voice/stt-registry.js +144 -0
- package/dist/voice/transcribe.d.ts +10 -0
- package/dist/voice/transcribe.js +75 -0
- package/dist/workflows/register-primitives.d.ts +52 -0
- package/dist/workflows/register-primitives.js +40 -0
- package/dist/workflows/service.d.ts +64 -0
- package/dist/workflows/service.js +50 -0
- package/package.json +117 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ProviderAuthRequiredError, rejectUnauthorizedResponse } from "../auth/provider-auth-error.js";
|
|
2
|
+
import { AuthStorage } from "../auth/storage.js";
|
|
3
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
4
|
+
// src/providers/xai.ts
|
|
5
|
+
/**
|
|
6
|
+
* xAI (Grok) OAuth Provider
|
|
7
|
+
*
|
|
8
|
+
* Uses OAuth tokens from AuthStorage to authenticate with the xAI API.
|
|
9
|
+
* The xAI API speaks an OpenAI-compatible chat format, and the OAuth access
|
|
10
|
+
* token is accepted as a bearer API key, so we plug `@ai-sdk/openai-compatible`
|
|
11
|
+
* into `https://api.x.ai/v1` with a custom fetch that injects the (auto-refreshed)
|
|
12
|
+
* access token.
|
|
13
|
+
*/
|
|
14
|
+
const XAI_PROVIDER_ID = "xai";
|
|
15
|
+
const XAI_PROVIDER_NAME = "xAI";
|
|
16
|
+
const XAI_BASE_URL = "https://api.x.ai/v1";
|
|
17
|
+
let authStorageInstance = null;
|
|
18
|
+
/** Get or create the shared AuthStorage instance. */
|
|
19
|
+
function getAuthStorage() {
|
|
20
|
+
if (!authStorageInstance) authStorageInstance = new AuthStorage();
|
|
21
|
+
return authStorageInstance;
|
|
22
|
+
}
|
|
23
|
+
/** Set a custom AuthStorage instance (useful for tests / TUI integration). */
|
|
24
|
+
function setAuthStorage(storage) {
|
|
25
|
+
authStorageInstance = storage ?? null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Build a fetch wrapper that authenticates with xAI OAuth.
|
|
29
|
+
* Injects the access token (auto-refreshed by AuthStorage) as a bearer token,
|
|
30
|
+
* preserving non-auth headers from the caller.
|
|
31
|
+
*/
|
|
32
|
+
function buildXAIOAuthFetch(opts = {}) {
|
|
33
|
+
return (async (url, init) => {
|
|
34
|
+
const storage = opts.authStorage ?? getAuthStorage();
|
|
35
|
+
storage.reload();
|
|
36
|
+
const cred = storage.get(XAI_PROVIDER_ID);
|
|
37
|
+
if (!cred || cred.type !== "oauth") throw ProviderAuthRequiredError.notSignedIn(XAI_PROVIDER_NAME);
|
|
38
|
+
const accessToken = await storage.getApiKey(XAI_PROVIDER_ID);
|
|
39
|
+
if (!accessToken) throw ProviderAuthRequiredError.signInExpired(XAI_PROVIDER_NAME);
|
|
40
|
+
const headers = new Headers(url instanceof Request ? url.headers : void 0);
|
|
41
|
+
if (init?.headers) new Headers(init.headers).forEach((value, key) => headers.set(key, value));
|
|
42
|
+
headers.delete("authorization");
|
|
43
|
+
headers.delete("x-api-key");
|
|
44
|
+
headers.set("Authorization", `Bearer ${accessToken}`);
|
|
45
|
+
try {
|
|
46
|
+
return await rejectUnauthorizedResponse(await fetch(url, {
|
|
47
|
+
...init,
|
|
48
|
+
headers
|
|
49
|
+
}), {
|
|
50
|
+
providerName: XAI_PROVIDER_NAME,
|
|
51
|
+
requestUrl: url instanceof URL ? url.toString() : typeof url === "string" ? url : url.url
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error && typeof error === "object") Object.assign(error, { requestUrl: url instanceof URL ? url.toString() : typeof url === "string" ? url : url.url });
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates an xAI model using OAuth authentication.
|
|
61
|
+
* Uses OAuth tokens from AuthStorage (auto-refreshes when needed).
|
|
62
|
+
*/
|
|
63
|
+
function xaiProvider(modelId, options) {
|
|
64
|
+
return createOpenAICompatible({
|
|
65
|
+
name: XAI_PROVIDER_ID,
|
|
66
|
+
baseURL: XAI_BASE_URL,
|
|
67
|
+
apiKey: "oauth-placeholder",
|
|
68
|
+
headers: options?.headers,
|
|
69
|
+
fetch: buildXAIOAuthFetch({ authStorage: options?.authStorage })
|
|
70
|
+
}).chatModel(modelId);
|
|
71
|
+
}
|
|
72
|
+
export { buildXAIOAuthFetch, getAuthStorage, setAuthStorage, xaiProvider };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { parseInterval, validateInterval, nextFireTime } from './interval.js';
|
|
2
|
+
export type { ParsedInterval, IntervalError, IntervalCheck } from './interval.js';
|
|
3
|
+
export { assembleSchedulePrompt, formatScriptOutput, SCRIPT_TIMEOUT_MS } from './prompt.js';
|
|
4
|
+
export type { RunScript, ScriptResult, AssemblePromptOptions, AssembledPrompt } from './prompt.js';
|
|
5
|
+
export { ThreadScheduler, shortScheduleId, describeScheduleSource, scheduleSignalAttributes, SCHEDULE_SIGNAL_SOURCE, LATE_FIRE_GRACE_MS, } from './scheduler.js';
|
|
6
|
+
export type { ThreadSchedule, ThreadScheduleSpec, ThreadScheduleSource, ScheduleTrigger, ScheduleFile, ThreadSchedulerOptions, ScheduleRunResult, } from './scheduler.js';
|
|
7
|
+
export { runScript, resolveScriptCommand, isExecutableFile } from './run-script.js';
|
|
8
|
+
export { createScheduleTools, SCHEDULE_TOOL_IDS } from './tools.js';
|
|
9
|
+
export type { ScheduleToolsOptions, ScheduleFileOptions } from './tools.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { nextFireTime, parseInterval, validateInterval } from "./interval.js";
|
|
2
|
+
import { SCRIPT_TIMEOUT_MS, assembleSchedulePrompt, formatScriptOutput } from "./prompt.js";
|
|
3
|
+
import { LATE_FIRE_GRACE_MS, SCHEDULE_SIGNAL_SOURCE, ThreadScheduler, describeScheduleSource, scheduleSignalAttributes, shortScheduleId } from "./scheduler.js";
|
|
4
|
+
import { isExecutableFile, resolveScriptCommand, runScript } from "./run-script.js";
|
|
5
|
+
import { SCHEDULE_TOOL_IDS, createScheduleTools } from "./tools.js";
|
|
6
|
+
export { LATE_FIRE_GRACE_MS, SCHEDULE_SIGNAL_SOURCE, SCHEDULE_TOOL_IDS, SCRIPT_TIMEOUT_MS, ThreadScheduler, assembleSchedulePrompt, createScheduleTools, describeScheduleSource, formatScriptOutput, isExecutableFile, nextFireTime, parseInterval, resolveScriptCommand, runScript, scheduleSignalAttributes, shortScheduleId, validateInterval };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interval parsing and fire-time math for `/schedules`.
|
|
3
|
+
*
|
|
4
|
+
* Users type `5m`, `2h`, `1 day`. Schedules fire on local wall-clock
|
|
5
|
+
* boundaries (5m fires at :00, :05, ...), so only intervals that divide the
|
|
6
|
+
* hour (minutes) or the day (hours) give a stable cadence; anything else is
|
|
7
|
+
* rejected with the nearest valid suggestion instead of silently drifting.
|
|
8
|
+
*/
|
|
9
|
+
export type ParsedInterval = {
|
|
10
|
+
ms: number;
|
|
11
|
+
label: string;
|
|
12
|
+
};
|
|
13
|
+
export type IntervalError = {
|
|
14
|
+
error: string;
|
|
15
|
+
};
|
|
16
|
+
/** Parse `5m`, `2h`, `1 day`, `90 minutes` into milliseconds plus a normalized label. */
|
|
17
|
+
export declare function parseInterval(text: string): ParsedInterval | IntervalError;
|
|
18
|
+
export type IntervalCheck = {
|
|
19
|
+
ok: true;
|
|
20
|
+
} | {
|
|
21
|
+
error: string;
|
|
22
|
+
suggestion?: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Check that an interval fires on stable clock boundaries: whole minutes that
|
|
26
|
+
* divide the hour, whole hours that divide the day, or exactly one day.
|
|
27
|
+
*/
|
|
28
|
+
export declare function validateInterval({ ms }: {
|
|
29
|
+
ms: number;
|
|
30
|
+
}): IntervalCheck;
|
|
31
|
+
/**
|
|
32
|
+
* The first boundary strictly after `after`, on the local wall clock
|
|
33
|
+
* (5m → :00, :05, ...; 2h → 00:00, 02:00, ...; 1d → midnight). `intervalMs`
|
|
34
|
+
* must pass {@link validateInterval}.
|
|
35
|
+
*
|
|
36
|
+
* Around daylight-saving changes this behaves like cron:
|
|
37
|
+
* - Sub-hour cadences follow the minute hand, so they keep firing every N real
|
|
38
|
+
* minutes through both the skipped and the repeated hour. Across a
|
|
39
|
+
* half-hour change, the gap between fires never exceeds the interval.
|
|
40
|
+
* - Hourly and daily cadences fire once per wall-clock slot. A boundary that
|
|
41
|
+
* falls in a skipped hour fires at the first minute after the jump (02:00 →
|
|
42
|
+
* 03:00), and a repeated hour does not fire a second time.
|
|
43
|
+
*
|
|
44
|
+
* Computing from `after` rather than from the previous fire means a late
|
|
45
|
+
* timer (sleep, busy event loop) skips missed boundaries instead of bursting
|
|
46
|
+
* to catch up.
|
|
47
|
+
*/
|
|
48
|
+
export declare function nextFireTime(intervalMs: number, after: number): number;
|
|
49
|
+
//# sourceMappingURL=interval.d.ts.map
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// src/schedules/interval.ts
|
|
2
|
+
/**
|
|
3
|
+
* Interval parsing and fire-time math for `/schedules`.
|
|
4
|
+
*
|
|
5
|
+
* Users type `5m`, `2h`, `1 day`. Schedules fire on local wall-clock
|
|
6
|
+
* boundaries (5m fires at :00, :05, ...), so only intervals that divide the
|
|
7
|
+
* hour (minutes) or the day (hours) give a stable cadence; anything else is
|
|
8
|
+
* rejected with the nearest valid suggestion instead of silently drifting.
|
|
9
|
+
*/
|
|
10
|
+
const SECOND_MS = 1e3;
|
|
11
|
+
const MINUTE_MS = 60 * SECOND_MS;
|
|
12
|
+
const HOUR_MS = 60 * MINUTE_MS;
|
|
13
|
+
const DAY_MS = 24 * HOUR_MS;
|
|
14
|
+
const UNIT_MS = {
|
|
15
|
+
s: {
|
|
16
|
+
ms: SECOND_MS,
|
|
17
|
+
short: "s"
|
|
18
|
+
},
|
|
19
|
+
sec: {
|
|
20
|
+
ms: SECOND_MS,
|
|
21
|
+
short: "s"
|
|
22
|
+
},
|
|
23
|
+
secs: {
|
|
24
|
+
ms: SECOND_MS,
|
|
25
|
+
short: "s"
|
|
26
|
+
},
|
|
27
|
+
second: {
|
|
28
|
+
ms: SECOND_MS,
|
|
29
|
+
short: "s"
|
|
30
|
+
},
|
|
31
|
+
seconds: {
|
|
32
|
+
ms: SECOND_MS,
|
|
33
|
+
short: "s"
|
|
34
|
+
},
|
|
35
|
+
m: {
|
|
36
|
+
ms: MINUTE_MS,
|
|
37
|
+
short: "m"
|
|
38
|
+
},
|
|
39
|
+
min: {
|
|
40
|
+
ms: MINUTE_MS,
|
|
41
|
+
short: "m"
|
|
42
|
+
},
|
|
43
|
+
mins: {
|
|
44
|
+
ms: MINUTE_MS,
|
|
45
|
+
short: "m"
|
|
46
|
+
},
|
|
47
|
+
minute: {
|
|
48
|
+
ms: MINUTE_MS,
|
|
49
|
+
short: "m"
|
|
50
|
+
},
|
|
51
|
+
minutes: {
|
|
52
|
+
ms: MINUTE_MS,
|
|
53
|
+
short: "m"
|
|
54
|
+
},
|
|
55
|
+
h: {
|
|
56
|
+
ms: HOUR_MS,
|
|
57
|
+
short: "h"
|
|
58
|
+
},
|
|
59
|
+
hr: {
|
|
60
|
+
ms: HOUR_MS,
|
|
61
|
+
short: "h"
|
|
62
|
+
},
|
|
63
|
+
hrs: {
|
|
64
|
+
ms: HOUR_MS,
|
|
65
|
+
short: "h"
|
|
66
|
+
},
|
|
67
|
+
hour: {
|
|
68
|
+
ms: HOUR_MS,
|
|
69
|
+
short: "h"
|
|
70
|
+
},
|
|
71
|
+
hours: {
|
|
72
|
+
ms: HOUR_MS,
|
|
73
|
+
short: "h"
|
|
74
|
+
},
|
|
75
|
+
d: {
|
|
76
|
+
ms: DAY_MS,
|
|
77
|
+
short: "d"
|
|
78
|
+
},
|
|
79
|
+
day: {
|
|
80
|
+
ms: DAY_MS,
|
|
81
|
+
short: "d"
|
|
82
|
+
},
|
|
83
|
+
days: {
|
|
84
|
+
ms: DAY_MS,
|
|
85
|
+
short: "d"
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const INTERVAL_RE = /^(\d+)\s*([a-z]+)$/i;
|
|
89
|
+
/** Parse `5m`, `2h`, `1 day`, `90 minutes` into milliseconds plus a normalized label. */
|
|
90
|
+
function parseInterval(text) {
|
|
91
|
+
const match = INTERVAL_RE.exec(text.trim());
|
|
92
|
+
if (!match) return { error: `Invalid interval "${text}". Use a number followed by a unit, e.g. 5m, 2h, 1d.` };
|
|
93
|
+
const value = Number(match[1]);
|
|
94
|
+
const unit = UNIT_MS[match[2].toLowerCase()];
|
|
95
|
+
if (!unit) return { error: `Unknown interval unit "${match[2]}". Use s, m, h, or d.` };
|
|
96
|
+
if (value <= 0) return { error: "Interval must be greater than zero." };
|
|
97
|
+
return {
|
|
98
|
+
ms: value * unit.ms,
|
|
99
|
+
label: `${value}${unit.short}`
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const MINUTE_DIVISORS = [
|
|
103
|
+
1,
|
|
104
|
+
2,
|
|
105
|
+
3,
|
|
106
|
+
4,
|
|
107
|
+
5,
|
|
108
|
+
6,
|
|
109
|
+
10,
|
|
110
|
+
12,
|
|
111
|
+
15,
|
|
112
|
+
20,
|
|
113
|
+
30,
|
|
114
|
+
60
|
|
115
|
+
];
|
|
116
|
+
const HOUR_DIVISORS = [
|
|
117
|
+
1,
|
|
118
|
+
2,
|
|
119
|
+
3,
|
|
120
|
+
4,
|
|
121
|
+
6,
|
|
122
|
+
8,
|
|
123
|
+
12,
|
|
124
|
+
24
|
|
125
|
+
];
|
|
126
|
+
function nearest(candidates, target) {
|
|
127
|
+
return candidates.reduce((best, candidate) => Math.abs(candidate - target) < Math.abs(best - target) ? candidate : best);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Check that an interval fires on stable clock boundaries: whole minutes that
|
|
131
|
+
* divide the hour, whole hours that divide the day, or exactly one day.
|
|
132
|
+
*/
|
|
133
|
+
function validateInterval({ ms }) {
|
|
134
|
+
if (ms < MINUTE_MS) return {
|
|
135
|
+
error: "Schedules fire at most once per minute.",
|
|
136
|
+
suggestion: "1m"
|
|
137
|
+
};
|
|
138
|
+
if (ms % MINUTE_MS !== 0) {
|
|
139
|
+
const minutes = Math.round(ms / MINUTE_MS);
|
|
140
|
+
return {
|
|
141
|
+
error: "Interval must be a whole number of minutes.",
|
|
142
|
+
suggestion: `${Math.max(1, nearest(MINUTE_DIVISORS, minutes))}m`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const minutes = ms / MINUTE_MS;
|
|
146
|
+
if (minutes < 60) {
|
|
147
|
+
if (60 % minutes === 0) return { ok: true };
|
|
148
|
+
return {
|
|
149
|
+
error: `${minutes}m does not divide an hour evenly.`,
|
|
150
|
+
suggestion: `${nearest(MINUTE_DIVISORS, minutes)}m`
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (minutes % 60 !== 0) {
|
|
154
|
+
const hours = Math.round(minutes / 60);
|
|
155
|
+
return {
|
|
156
|
+
error: `${minutes}m is not a whole number of hours.`,
|
|
157
|
+
suggestion: hours < 24 ? `${nearest(HOUR_DIVISORS, hours)}h` : "1d"
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const hours = minutes / 60;
|
|
161
|
+
if (hours < 24) {
|
|
162
|
+
if (24 % hours === 0) return { ok: true };
|
|
163
|
+
return {
|
|
164
|
+
error: `${hours}h does not divide a day evenly.`,
|
|
165
|
+
suggestion: `${nearest(HOUR_DIVISORS, hours)}h`
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (hours === 24) return { ok: true };
|
|
169
|
+
return {
|
|
170
|
+
error: "Intervals longer than one day are not supported.",
|
|
171
|
+
suggestion: "1d"
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** Hour-and-longer cadences: which local wall-clock slot a minute belongs to. */
|
|
175
|
+
function hourSlotKey(date, stepHours) {
|
|
176
|
+
return ((date.getFullYear() * 12 + date.getMonth()) * 31 + date.getDate()) * 24 + Math.floor(date.getHours() / stepHours) * stepHours;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The first boundary strictly after `after`, on the local wall clock
|
|
180
|
+
* (5m → :00, :05, ...; 2h → 00:00, 02:00, ...; 1d → midnight). `intervalMs`
|
|
181
|
+
* must pass {@link validateInterval}.
|
|
182
|
+
*
|
|
183
|
+
* Around daylight-saving changes this behaves like cron:
|
|
184
|
+
* - Sub-hour cadences follow the minute hand, so they keep firing every N real
|
|
185
|
+
* minutes through both the skipped and the repeated hour. Across a
|
|
186
|
+
* half-hour change, the gap between fires never exceeds the interval.
|
|
187
|
+
* - Hourly and daily cadences fire once per wall-clock slot. A boundary that
|
|
188
|
+
* falls in a skipped hour fires at the first minute after the jump (02:00 →
|
|
189
|
+
* 03:00), and a repeated hour does not fire a second time.
|
|
190
|
+
*
|
|
191
|
+
* Computing from `after` rather than from the previous fire means a late
|
|
192
|
+
* timer (sleep, busy event loop) skips missed boundaries instead of bursting
|
|
193
|
+
* to catch up.
|
|
194
|
+
*/
|
|
195
|
+
function nextFireTime(intervalMs, after) {
|
|
196
|
+
const cursor = /* @__PURE__ */ new Date(Math.floor(after / MINUTE_MS) * MINUTE_MS);
|
|
197
|
+
const stepMinutes = intervalMs / MINUTE_MS;
|
|
198
|
+
if (stepMinutes < 60) {
|
|
199
|
+
const latest = cursor.getTime() + intervalMs;
|
|
200
|
+
do
|
|
201
|
+
cursor.setTime(cursor.getTime() + MINUTE_MS);
|
|
202
|
+
while (cursor.getMinutes() % stepMinutes !== 0 && cursor.getTime() < latest);
|
|
203
|
+
return cursor.getTime();
|
|
204
|
+
}
|
|
205
|
+
const stepHours = stepMinutes / 60;
|
|
206
|
+
const startSlot = hourSlotKey(new Date(after), stepHours);
|
|
207
|
+
do
|
|
208
|
+
cursor.setTime(cursor.getTime() + MINUTE_MS);
|
|
209
|
+
while (hourSlotKey(cursor, stepHours) <= startSlot);
|
|
210
|
+
return cursor.getTime();
|
|
211
|
+
}
|
|
212
|
+
export { nextFireTime, parseInterval, validateInterval };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fire-time prompt assembly for Mastra Code schedules.
|
|
3
|
+
*
|
|
4
|
+
* File-backed schedules only hold a descriptor; each fire runs the script or
|
|
5
|
+
* re-reads the file so the model always sees current output. Failures become
|
|
6
|
+
* the prompt text rather than a skipped fire — a schedule that silently stops
|
|
7
|
+
* delivering is worse than one that reports "the script broke".
|
|
8
|
+
*/
|
|
9
|
+
import type { ThreadSchedule } from './scheduler.js';
|
|
10
|
+
export declare const SCRIPT_TIMEOUT_MS = 60000;
|
|
11
|
+
export type ScriptResult = {
|
|
12
|
+
stdout: string;
|
|
13
|
+
stderr: string;
|
|
14
|
+
exitCode: number | null;
|
|
15
|
+
};
|
|
16
|
+
export type RunScript = (absPath: string, options: {
|
|
17
|
+
cwd: string;
|
|
18
|
+
timeoutMs: number;
|
|
19
|
+
}) => Promise<ScriptResult>;
|
|
20
|
+
/**
|
|
21
|
+
* One fire's prompt. `outcome` summarizes a file-backed fire for the
|
|
22
|
+
* transcript header: `exit 0`, `exit 3`, `killed` (timeout/signal), or
|
|
23
|
+
* `failed` (the file could not be run or read).
|
|
24
|
+
*/
|
|
25
|
+
export type AssembledPrompt = {
|
|
26
|
+
prompt: string;
|
|
27
|
+
outcome?: string;
|
|
28
|
+
};
|
|
29
|
+
export type AssemblePromptOptions = {
|
|
30
|
+
cwd: string;
|
|
31
|
+
runScript: RunScript;
|
|
32
|
+
readFile: (absPath: string) => Promise<string>;
|
|
33
|
+
};
|
|
34
|
+
export declare function formatScriptOutput(displayPath: string, result: ScriptResult): string;
|
|
35
|
+
export declare function assembleSchedulePrompt(spec: Pick<ThreadSchedule, 'prompt' | 'file' | 'extraPrompt'>, options: AssemblePromptOptions): Promise<AssembledPrompt>;
|
|
36
|
+
//# sourceMappingURL=prompt.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/schedules/prompt.ts
|
|
2
|
+
const SCRIPT_TIMEOUT_MS = 6e4;
|
|
3
|
+
function appendExtra(text, extra) {
|
|
4
|
+
return extra ? `${text}\n\n${extra}` : text;
|
|
5
|
+
}
|
|
6
|
+
function formatScriptOutput(displayPath, result) {
|
|
7
|
+
const body = [result.stdout, result.stderr].map((part) => part.trimEnd()).filter(Boolean).join("\n");
|
|
8
|
+
return `Output of ${displayPath} (exit ${result.exitCode ?? "null"}):\n${body || "(no output)"}`;
|
|
9
|
+
}
|
|
10
|
+
function errorMessage(error) {
|
|
11
|
+
return error instanceof Error ? error.message : String(error);
|
|
12
|
+
}
|
|
13
|
+
async function assembleSchedulePrompt(spec, options) {
|
|
14
|
+
const { file } = spec;
|
|
15
|
+
if (!file) return { prompt: spec.prompt ?? "" };
|
|
16
|
+
let text;
|
|
17
|
+
let outcome;
|
|
18
|
+
try {
|
|
19
|
+
if (file.mode === "script") {
|
|
20
|
+
const result = await options.runScript(file.path, {
|
|
21
|
+
cwd: options.cwd,
|
|
22
|
+
timeoutMs: SCRIPT_TIMEOUT_MS
|
|
23
|
+
});
|
|
24
|
+
text = formatScriptOutput(file.displayPath, result);
|
|
25
|
+
outcome = result.exitCode === null ? "killed" : `exit ${result.exitCode}`;
|
|
26
|
+
} else text = (await options.readFile(file.path)).trim();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
text = `Schedule ${file.displayPath} failed: ${errorMessage(error)}`;
|
|
29
|
+
outcome = "failed";
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
prompt: appendExtra(text, spec.extraPrompt),
|
|
33
|
+
...outcome ? { outcome } : {}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export { SCRIPT_TIMEOUT_MS, assembleSchedulePrompt, formatScriptOutput };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RunScript } from './prompt.js';
|
|
2
|
+
export declare function isExecutableFile(absPath: string): boolean;
|
|
3
|
+
export declare function resolveScriptCommand(absPath: string): {
|
|
4
|
+
command: string;
|
|
5
|
+
args: string[];
|
|
6
|
+
};
|
|
7
|
+
export declare const runScript: RunScript;
|
|
8
|
+
//# sourceMappingURL=run-script.d.ts.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as path$1 from "path";
|
|
2
|
+
import * as fs$1 from "fs";
|
|
3
|
+
import { execFile } from "child_process";
|
|
4
|
+
// src/schedules/run-script.ts
|
|
5
|
+
/**
|
|
6
|
+
* Runs a schedule's script file at fire time.
|
|
7
|
+
*
|
|
8
|
+
* The file path is always passed as the literal command (or as the single
|
|
9
|
+
* argument to a fixed interpreter) — never through a shell string — so the
|
|
10
|
+
* user's extra prompt can never be interpolated into a command line.
|
|
11
|
+
*/
|
|
12
|
+
/** Interpreter for scripts that lack an executable bit, keyed by extension. */
|
|
13
|
+
const INTERPRETERS = {
|
|
14
|
+
".sh": () => "/bin/sh",
|
|
15
|
+
".js": () => process.execPath,
|
|
16
|
+
".mjs": () => process.execPath,
|
|
17
|
+
".cjs": () => process.execPath,
|
|
18
|
+
".ts": () => process.execPath,
|
|
19
|
+
".py": () => "python3"
|
|
20
|
+
};
|
|
21
|
+
const MAX_OUTPUT_BYTES = 1048576;
|
|
22
|
+
function isExecutableFile(absPath) {
|
|
23
|
+
try {
|
|
24
|
+
fs$1.accessSync(absPath, fs$1.constants.X_OK);
|
|
25
|
+
return fs$1.statSync(absPath).isFile();
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function resolveScriptCommand(absPath) {
|
|
31
|
+
if (isExecutableFile(absPath)) return {
|
|
32
|
+
command: absPath,
|
|
33
|
+
args: []
|
|
34
|
+
};
|
|
35
|
+
const interpreter = INTERPRETERS[path$1.extname(absPath).toLowerCase()];
|
|
36
|
+
if (!interpreter) throw new Error(`${absPath} is not executable and has no known interpreter`);
|
|
37
|
+
return {
|
|
38
|
+
command: interpreter(),
|
|
39
|
+
args: [absPath]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const runScript = async (absPath, { cwd, timeoutMs }) => {
|
|
43
|
+
const { command, args } = resolveScriptCommand(absPath);
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
execFile(command, args, {
|
|
46
|
+
cwd,
|
|
47
|
+
timeout: timeoutMs,
|
|
48
|
+
maxBuffer: MAX_OUTPUT_BYTES,
|
|
49
|
+
windowsHide: true
|
|
50
|
+
}, (error, stdout, stderr) => {
|
|
51
|
+
if (!error) {
|
|
52
|
+
resolve({
|
|
53
|
+
stdout,
|
|
54
|
+
stderr,
|
|
55
|
+
exitCode: 0
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const spawnError = error;
|
|
60
|
+
if (typeof spawnError.code === "number") {
|
|
61
|
+
resolve({
|
|
62
|
+
stdout,
|
|
63
|
+
stderr,
|
|
64
|
+
exitCode: spawnError.code
|
|
65
|
+
});
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (spawnError.killed) {
|
|
69
|
+
resolve({
|
|
70
|
+
stdout,
|
|
71
|
+
stderr: `${stderr}\n[timed out after ${timeoutMs}ms]`.trim(),
|
|
72
|
+
exitCode: null
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
reject(error);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
export { isExecutableFile, resolveScriptCommand, runScript };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { ParsedInterval } from './interval.js';
|
|
2
|
+
import type { AssembledPrompt } from './prompt.js';
|
|
3
|
+
/** `source` attribute on fired signals; the TUI labels these turns `schedule`. */
|
|
4
|
+
export declare const SCHEDULE_SIGNAL_SOURCE = "schedule";
|
|
5
|
+
/** When a schedule fires. Only recurring `every` triggers exist today. */
|
|
6
|
+
export type ScheduleTrigger = {
|
|
7
|
+
kind: 'every';
|
|
8
|
+
interval: ParsedInterval;
|
|
9
|
+
};
|
|
10
|
+
/** A file a schedule runs as a script, or re-reads and sends as the prompt, on every fire. */
|
|
11
|
+
export type ScheduleFile = {
|
|
12
|
+
path: string;
|
|
13
|
+
displayPath: string;
|
|
14
|
+
mode: 'script' | 'prompt';
|
|
15
|
+
};
|
|
16
|
+
/** What to send on each fire: `prompt` text, or a `file` (plus `extraPrompt` appended after it). */
|
|
17
|
+
export type ThreadScheduleSource = {
|
|
18
|
+
prompt: string;
|
|
19
|
+
} | {
|
|
20
|
+
file: ScheduleFile;
|
|
21
|
+
extraPrompt?: string;
|
|
22
|
+
};
|
|
23
|
+
export type ThreadScheduleSpec = ThreadScheduleSource & {
|
|
24
|
+
trigger: ScheduleTrigger;
|
|
25
|
+
/** Who is creating it: the user via `/schedules`, or the agent via its schedule tools. Defaults to `user`. */
|
|
26
|
+
createdBy?: 'user' | 'agent';
|
|
27
|
+
};
|
|
28
|
+
export type ThreadSchedule = {
|
|
29
|
+
id: string;
|
|
30
|
+
threadId: string;
|
|
31
|
+
resourceId: string;
|
|
32
|
+
trigger: ScheduleTrigger;
|
|
33
|
+
prompt?: string;
|
|
34
|
+
file?: ScheduleFile;
|
|
35
|
+
extraPrompt?: string;
|
|
36
|
+
createdBy: 'user' | 'agent';
|
|
37
|
+
status: 'active' | 'paused';
|
|
38
|
+
/** Next boundary this schedule fires at; only meaningful while active. */
|
|
39
|
+
nextFireAt: number;
|
|
40
|
+
createdAt: number;
|
|
41
|
+
};
|
|
42
|
+
export type ThreadSchedulerOptions = {
|
|
43
|
+
/** Build the prompt for one fire (runs scripts / reads files). */
|
|
44
|
+
assemblePrompt: (schedule: ThreadSchedule) => Promise<AssembledPrompt>;
|
|
45
|
+
/** Hand the prompt to the agent for the schedule's thread. */
|
|
46
|
+
deliver: (schedule: ThreadSchedule, assembled: AssembledPrompt) => Promise<void>;
|
|
47
|
+
/** A fire failed before reaching the agent. */
|
|
48
|
+
onError?: (error: unknown, schedule: ThreadSchedule) => void;
|
|
49
|
+
now?: () => number;
|
|
50
|
+
};
|
|
51
|
+
/** How late a timer may run before its fire counts as missed (sleep, stalled event loop). */
|
|
52
|
+
export declare const LATE_FIRE_GRACE_MS = 60000;
|
|
53
|
+
/**
|
|
54
|
+
* Outcome of a manual `run()`. `failed` means nothing reached the agent: the
|
|
55
|
+
* prompt could not be built or the agent refused it.
|
|
56
|
+
*/
|
|
57
|
+
export type ScheduleRunResult = {
|
|
58
|
+
status: 'fired';
|
|
59
|
+
} | {
|
|
60
|
+
status: 'failed';
|
|
61
|
+
error: unknown;
|
|
62
|
+
} | {
|
|
63
|
+
status: 'busy';
|
|
64
|
+
} | {
|
|
65
|
+
status: 'not-found';
|
|
66
|
+
};
|
|
67
|
+
export declare class ThreadScheduler {
|
|
68
|
+
#private;
|
|
69
|
+
constructor(options: ThreadSchedulerOptions);
|
|
70
|
+
/** Throws when the interval is not a supported wall-clock cadence (see `validateInterval`) or the prompt is empty. */
|
|
71
|
+
create(spec: ThreadScheduleSpec, target: {
|
|
72
|
+
threadId: string;
|
|
73
|
+
resourceId: string;
|
|
74
|
+
}): ThreadSchedule;
|
|
75
|
+
list(filter?: {
|
|
76
|
+
threadId?: string;
|
|
77
|
+
}): ThreadSchedule[];
|
|
78
|
+
delete(id: string): boolean;
|
|
79
|
+
pause(id: string): boolean;
|
|
80
|
+
resume(id: string): boolean;
|
|
81
|
+
/** Whether a fire of this schedule (timer or manual) is still assembling or delivering. */
|
|
82
|
+
isFiring(id: string): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Fire once now, outside the cadence. Returns `busy` without firing when a
|
|
85
|
+
* fire of this schedule is still running (a slow script, say), so the same
|
|
86
|
+
* script never runs twice at once. Resolves once the fire finished; a fire
|
|
87
|
+
* that never reached the agent resolves `failed` instead of calling `onError`.
|
|
88
|
+
*/
|
|
89
|
+
run(id: string): Promise<ScheduleRunResult>;
|
|
90
|
+
/** Drop every schedule and timer (process shutdown). */
|
|
91
|
+
stop(): void;
|
|
92
|
+
}
|
|
93
|
+
export declare function shortScheduleId(id: string): string;
|
|
94
|
+
/** One-line human summary of what a schedule sends: `run ./check.sh + "extra"`, `read notes.md`, `"prompt"`. */
|
|
95
|
+
export declare function describeScheduleSource(schedule: Pick<ThreadSchedule, 'prompt' | 'file' | 'extraPrompt'>): string;
|
|
96
|
+
/**
|
|
97
|
+
* Attributes on a fired schedule signal. They carry everything the transcript
|
|
98
|
+
* header needs, so reloaded history renders without the process-local entry.
|
|
99
|
+
*/
|
|
100
|
+
export declare function scheduleSignalAttributes(schedule: ThreadSchedule, assembled?: Pick<AssembledPrompt, 'outcome'>): Record<string, string>;
|
|
101
|
+
//# sourceMappingURL=scheduler.d.ts.map
|