@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,14 @@
|
|
|
1
|
+
export type IPCMessageType = 'shell-output' | 'token-limits' | 'agent-event' | 'tool-call' | 'tool-result';
|
|
2
|
+
export interface IPCMessage {
|
|
3
|
+
type: IPCMessageType;
|
|
4
|
+
data: unknown;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
}
|
|
7
|
+
declare class IPCReporter {
|
|
8
|
+
private enabled;
|
|
9
|
+
constructor();
|
|
10
|
+
send(type: IPCMessageType, data: unknown): void;
|
|
11
|
+
}
|
|
12
|
+
export declare const ipcReporter: IPCReporter;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=ipc-reporter.d.ts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/ipc/ipc-reporter.ts
|
|
2
|
+
var IPCReporter = class {
|
|
3
|
+
enabled;
|
|
4
|
+
constructor() {
|
|
5
|
+
this.enabled = false;
|
|
6
|
+
}
|
|
7
|
+
send(type, data) {
|
|
8
|
+
if (!this.enabled) return;
|
|
9
|
+
const message = {
|
|
10
|
+
type,
|
|
11
|
+
data,
|
|
12
|
+
timestamp: Date.now()
|
|
13
|
+
};
|
|
14
|
+
if (process.send) process.send(message);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const ipcReporter = new IPCReporter();
|
|
18
|
+
export { ipcReporter };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { Session } from '@mastra/core/agent-controller';
|
|
2
|
+
import type { Knowledge } from '@mastra/core/knowledge';
|
|
3
|
+
import type { Mastra } from '@mastra/core/mastra';
|
|
4
|
+
import type { KnowledgePageActivityAction } from '@mastra/core/storage';
|
|
5
|
+
import type { MastraCodeState } from './schema.js';
|
|
6
|
+
export type KnowledgeInspectorScopeLevel = 'org' | 'resource' | 'thread';
|
|
7
|
+
export type KnowledgeInspectorRecordType = 'node';
|
|
8
|
+
/** `name`: every readable page, by name. `connected`: the first pages by name, most linked first. */
|
|
9
|
+
export type KnowledgeInspectorNodeSort = 'name' | 'connected';
|
|
10
|
+
export interface KnowledgeInspectorScopeRoot {
|
|
11
|
+
level: KnowledgeInspectorScopeLevel;
|
|
12
|
+
id?: string;
|
|
13
|
+
available: boolean;
|
|
14
|
+
reason?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface KnowledgeInspectorScopeTree {
|
|
17
|
+
identityKey: string;
|
|
18
|
+
defaultLevel: 'resource';
|
|
19
|
+
roots: KnowledgeInspectorScopeRoot[];
|
|
20
|
+
}
|
|
21
|
+
export interface KnowledgeInspectorScopeBadge {
|
|
22
|
+
/** The scope's type, e.g. `org`, `project`, `resource`, `thread`. */
|
|
23
|
+
level: string;
|
|
24
|
+
id: string;
|
|
25
|
+
}
|
|
26
|
+
export interface KnowledgeInspectorRelationshipCounts {
|
|
27
|
+
records: number;
|
|
28
|
+
outgoing: number;
|
|
29
|
+
incoming: number;
|
|
30
|
+
sampled: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** A page. */
|
|
33
|
+
export interface KnowledgeInspectorNodeSummary {
|
|
34
|
+
/** Opaque, freshly minted per result and bound to the session; use it to open the page. */
|
|
35
|
+
handle: string;
|
|
36
|
+
/** The page's stable id: the same page always has the same id, and two pages never share one. */
|
|
37
|
+
id: string;
|
|
38
|
+
type: KnowledgeInspectorRecordType;
|
|
39
|
+
name: string;
|
|
40
|
+
scope: KnowledgeInspectorScopeBadge;
|
|
41
|
+
version: number;
|
|
42
|
+
updatedAt: string;
|
|
43
|
+
relationshipCounts?: KnowledgeInspectorRelationshipCounts;
|
|
44
|
+
}
|
|
45
|
+
/** A note. */
|
|
46
|
+
export interface KnowledgeInspectorRecordSummary {
|
|
47
|
+
text: string;
|
|
48
|
+
scope: KnowledgeInspectorScopeBadge;
|
|
49
|
+
sourceThreadId?: string;
|
|
50
|
+
capturedAt: string;
|
|
51
|
+
when?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface KnowledgeInspectorNodeList {
|
|
54
|
+
identityKey: string;
|
|
55
|
+
scopeLevel: KnowledgeInspectorScopeLevel;
|
|
56
|
+
nodes: KnowledgeInspectorNodeSummary[];
|
|
57
|
+
nextCursor?: string;
|
|
58
|
+
sort?: KnowledgeInspectorNodeSort;
|
|
59
|
+
coverage?: 'exact' | 'window';
|
|
60
|
+
}
|
|
61
|
+
export interface KnowledgeInspectorRelationshipPreview {
|
|
62
|
+
nodes: KnowledgeInspectorNodeSummary[];
|
|
63
|
+
partial: boolean;
|
|
64
|
+
}
|
|
65
|
+
export interface KnowledgeInspectorNodeDetail {
|
|
66
|
+
identityKey: string;
|
|
67
|
+
scopeLevel: KnowledgeInspectorScopeLevel;
|
|
68
|
+
node: KnowledgeInspectorNodeSummary;
|
|
69
|
+
/** Notes on the page. */
|
|
70
|
+
records: KnowledgeInspectorRecordSummary[];
|
|
71
|
+
recordsNextCursor?: string;
|
|
72
|
+
/** Notes on other pages that mention this page. */
|
|
73
|
+
mentioningRecords: KnowledgeInspectorRecordSummary[];
|
|
74
|
+
mentioningRecordsNextCursor?: string;
|
|
75
|
+
/** Pages this page's notes and body mention. */
|
|
76
|
+
outgoingTargets: KnowledgeInspectorRelationshipPreview;
|
|
77
|
+
/** Pages whose notes mention this page. */
|
|
78
|
+
incomingParents: KnowledgeInspectorRelationshipPreview;
|
|
79
|
+
relationshipCounts: KnowledgeInspectorRelationshipCounts;
|
|
80
|
+
/** The page body. */
|
|
81
|
+
content?: string;
|
|
82
|
+
contentTruncated: boolean;
|
|
83
|
+
links: Array<{
|
|
84
|
+
label: string;
|
|
85
|
+
node?: KnowledgeInspectorNodeSummary;
|
|
86
|
+
}>;
|
|
87
|
+
}
|
|
88
|
+
export interface KnowledgeInspectorActivityEvent {
|
|
89
|
+
action: KnowledgePageActivityAction;
|
|
90
|
+
recordType: 'page' | 'note';
|
|
91
|
+
scope: KnowledgeInspectorScopeBadge;
|
|
92
|
+
sourceThreadId?: string;
|
|
93
|
+
createdAt: string;
|
|
94
|
+
record?: KnowledgeInspectorNodeSummary;
|
|
95
|
+
}
|
|
96
|
+
export interface KnowledgeInspectorActivityList {
|
|
97
|
+
identityKey: string;
|
|
98
|
+
scopeLevel: KnowledgeInspectorScopeLevel;
|
|
99
|
+
events: KnowledgeInspectorActivityEvent[];
|
|
100
|
+
nextCursor?: string;
|
|
101
|
+
}
|
|
102
|
+
export interface KnowledgeInspector {
|
|
103
|
+
getScopeTree(): Promise<KnowledgeInspectorScopeTree>;
|
|
104
|
+
listNodes(input: {
|
|
105
|
+
level: KnowledgeInspectorScopeLevel;
|
|
106
|
+
namePrefix?: string;
|
|
107
|
+
sort?: KnowledgeInspectorNodeSort;
|
|
108
|
+
cursor?: string;
|
|
109
|
+
limit?: number;
|
|
110
|
+
}): Promise<KnowledgeInspectorNodeList>;
|
|
111
|
+
getNode(input: {
|
|
112
|
+
handle: string;
|
|
113
|
+
recordsCursor?: string;
|
|
114
|
+
mentioningRecordsCursor?: string;
|
|
115
|
+
recordLimit?: number;
|
|
116
|
+
}): Promise<KnowledgeInspectorNodeDetail>;
|
|
117
|
+
listActivity(input: {
|
|
118
|
+
level: KnowledgeInspectorScopeLevel;
|
|
119
|
+
cursor?: string;
|
|
120
|
+
limit?: number;
|
|
121
|
+
}): Promise<KnowledgeInspectorActivityList>;
|
|
122
|
+
}
|
|
123
|
+
export declare class KnowledgeInspectorError extends Error {
|
|
124
|
+
readonly code: 'unavailable' | 'invalid-handle' | 'stale-handle' | 'invalid-cursor' | 'not-visible';
|
|
125
|
+
constructor(code: 'unavailable' | 'invalid-handle' | 'stale-handle' | 'invalid-cursor' | 'not-visible', message: string);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The /knowledge inspector for a session, reading through the session's Knowledge runtime (a key
|
|
129
|
+
* resolves through `mastra`). Undefined when there is no Knowledge runtime: access rules live on the
|
|
130
|
+
* runtime, so a bare storage domain can't be inspected.
|
|
131
|
+
*/
|
|
132
|
+
export declare function createKnowledgeInspector(input: {
|
|
133
|
+
knowledge?: Knowledge | string;
|
|
134
|
+
mastra?: Pick<Mastra, 'getKnowledge'>;
|
|
135
|
+
session: Session<MastraCodeState>;
|
|
136
|
+
/** The host's `knowledge.scopes`, so the inspector reads what the code agent writes. */
|
|
137
|
+
hostScopes?: readonly string[];
|
|
138
|
+
}): Promise<KnowledgeInspector | undefined>;
|
|
139
|
+
//# sourceMappingURL=knowledge-inspector.d.ts.map
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { encodeKnowledgeScopeId } from "./knowledge-scope-id.js";
|
|
2
|
+
import { knowledgeSessionTags, resolveKnowledgeSessionScopes } from "./knowledge-scope.js";
|
|
3
|
+
import { parseKnowledgeWikilinks } from "@mastra/core/storage";
|
|
4
|
+
// src/knowledge-inspector.ts
|
|
5
|
+
var KnowledgeInspectorError = class extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.name = "KnowledgeInspectorError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const HANDLE_TTL_MS = 3e5;
|
|
14
|
+
const MAX_OPAQUE_ENTRIES = 1e3;
|
|
15
|
+
const DEFAULT_PAGE_LIMIT = 50;
|
|
16
|
+
const MAX_PAGE_LIMIT = 50;
|
|
17
|
+
const DEFAULT_NOTE_LIMIT = 25;
|
|
18
|
+
const MAX_NOTE_LIMIT = 100;
|
|
19
|
+
const DEFAULT_ACTIVITY_LIMIT = 20;
|
|
20
|
+
const MAX_ACTIVITY_LIMIT = 100;
|
|
21
|
+
const MAX_RELATED_PAGES = 25;
|
|
22
|
+
const MAX_RANK_CANDIDATES = 50;
|
|
23
|
+
const MAX_RANK_NOTES = 100;
|
|
24
|
+
const MAX_BODY_BYTES = 32768;
|
|
25
|
+
function opaqueToken() {
|
|
26
|
+
return Buffer.from(globalThis.crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(24))).toString("base64url");
|
|
27
|
+
}
|
|
28
|
+
function boundedLimit(value, fallback, maximum) {
|
|
29
|
+
if (value === void 0) return fallback;
|
|
30
|
+
if (!Number.isInteger(value) || value < 1) return fallback;
|
|
31
|
+
return Math.min(value, maximum);
|
|
32
|
+
}
|
|
33
|
+
function badgeOf(address) {
|
|
34
|
+
if (!address) return {
|
|
35
|
+
level: "unknown",
|
|
36
|
+
id: ""
|
|
37
|
+
};
|
|
38
|
+
const separator = address.indexOf(":");
|
|
39
|
+
return {
|
|
40
|
+
level: address.slice(0, separator),
|
|
41
|
+
id: address.slice(separator + 1)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function truncateUtf8(value, maxBytes) {
|
|
45
|
+
const encoded = Buffer.from(value);
|
|
46
|
+
if (encoded.byteLength <= maxBytes) return {
|
|
47
|
+
value,
|
|
48
|
+
truncated: false
|
|
49
|
+
};
|
|
50
|
+
let end = maxBytes;
|
|
51
|
+
let truncated = encoded.subarray(0, end).toString("utf8");
|
|
52
|
+
while (Buffer.byteLength(truncated) > maxBytes) truncated = encoded.subarray(0, --end).toString("utf8");
|
|
53
|
+
return {
|
|
54
|
+
value: truncated,
|
|
55
|
+
truncated: true
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The /knowledge inspector: reads the pages and notes a session's code agent can read, one level at a
|
|
60
|
+
* time (the session's shared tags, then its project resource, then its thread), through the same
|
|
61
|
+
* Knowledge access rules the agent's runs use.
|
|
62
|
+
*/
|
|
63
|
+
var ScopedKnowledgeInspector = class {
|
|
64
|
+
#knowledge;
|
|
65
|
+
#store;
|
|
66
|
+
#session;
|
|
67
|
+
#handles = /* @__PURE__ */ new Map();
|
|
68
|
+
#cursors = /* @__PURE__ */ new Map();
|
|
69
|
+
#scopeAddresses = /* @__PURE__ */ new Map();
|
|
70
|
+
#fingerprint;
|
|
71
|
+
#identityKey = opaqueToken();
|
|
72
|
+
#hostScopes;
|
|
73
|
+
constructor(input) {
|
|
74
|
+
this.#knowledge = input.knowledge;
|
|
75
|
+
this.#store = input.store;
|
|
76
|
+
this.#session = input.session;
|
|
77
|
+
this.#hostScopes = input.hostScopes ?? [];
|
|
78
|
+
this.#session.subscribe((event) => {
|
|
79
|
+
if (event.type === "thread_changed" || event.type === "thread_created" || event.type === "thread_deleted") this.#invalidate();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async getScopeTree() {
|
|
83
|
+
const binding = await this.#binding();
|
|
84
|
+
return {
|
|
85
|
+
identityKey: binding.identityKey,
|
|
86
|
+
defaultLevel: "resource",
|
|
87
|
+
roots: [
|
|
88
|
+
{
|
|
89
|
+
level: "org",
|
|
90
|
+
id: binding.sessionTags.join(" + "),
|
|
91
|
+
available: true
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
level: "resource",
|
|
95
|
+
id: binding.resourceId,
|
|
96
|
+
available: true
|
|
97
|
+
},
|
|
98
|
+
binding.threadId ? {
|
|
99
|
+
level: "thread",
|
|
100
|
+
id: binding.threadId,
|
|
101
|
+
available: true
|
|
102
|
+
} : {
|
|
103
|
+
level: "thread",
|
|
104
|
+
available: false,
|
|
105
|
+
reason: "No active thread belongs to this project."
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async listNodes(input) {
|
|
111
|
+
const binding = await this.#binding();
|
|
112
|
+
const view = await this.#view(binding, input.level);
|
|
113
|
+
const limit = boundedLimit(input.limit, DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT);
|
|
114
|
+
const sort = input.sort ?? "name";
|
|
115
|
+
const filters = {
|
|
116
|
+
namePrefix: input.namePrefix,
|
|
117
|
+
sort
|
|
118
|
+
};
|
|
119
|
+
if (sort === "name") {
|
|
120
|
+
const cursor = this.#consumeCursor(input.cursor, binding, input.level, "node", filters);
|
|
121
|
+
const result = await this.#store.listPages({
|
|
122
|
+
read: view.read,
|
|
123
|
+
namePrefix: input.namePrefix,
|
|
124
|
+
cursor,
|
|
125
|
+
limit
|
|
126
|
+
});
|
|
127
|
+
const nodes = await Promise.all(result.pages.map(async (page) => ({
|
|
128
|
+
...await this.#summary(page, binding, view),
|
|
129
|
+
relationshipCounts: await this.#counts(page, view)
|
|
130
|
+
})));
|
|
131
|
+
await this.#assertStable(binding);
|
|
132
|
+
return {
|
|
133
|
+
identityKey: binding.identityKey,
|
|
134
|
+
scopeLevel: input.level,
|
|
135
|
+
nodes,
|
|
136
|
+
nextCursor: result.nextCursor ? this.#mintCursor(binding, input.level, "node", result.nextCursor, filters) : void 0,
|
|
137
|
+
sort,
|
|
138
|
+
coverage: "exact"
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const encoded = this.#consumeCursor(input.cursor, binding, input.level, "ranked-node", filters);
|
|
142
|
+
const snapshot = encoded ? JSON.parse(encoded) : await this.#ranked(view, input.namePrefix);
|
|
143
|
+
const nodes = [];
|
|
144
|
+
for (const entry of snapshot.entries.slice(snapshot.offset, snapshot.offset + limit)) {
|
|
145
|
+
const page = await this.#store.getPage({
|
|
146
|
+
id: entry.id,
|
|
147
|
+
read: view.read
|
|
148
|
+
});
|
|
149
|
+
if (!page || page.deletedAt) continue;
|
|
150
|
+
nodes.push({
|
|
151
|
+
...await this.#summary(page, binding, view),
|
|
152
|
+
relationshipCounts: entry.counts
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const nextOffset = snapshot.offset + limit;
|
|
156
|
+
await this.#assertStable(binding);
|
|
157
|
+
return {
|
|
158
|
+
identityKey: binding.identityKey,
|
|
159
|
+
scopeLevel: input.level,
|
|
160
|
+
nodes,
|
|
161
|
+
nextCursor: nextOffset < snapshot.entries.length ? this.#mintCursor(binding, input.level, "ranked-node", JSON.stringify({
|
|
162
|
+
...snapshot,
|
|
163
|
+
offset: nextOffset
|
|
164
|
+
}), filters) : void 0,
|
|
165
|
+
sort,
|
|
166
|
+
coverage: "window"
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async getNode(input) {
|
|
170
|
+
const binding = await this.#binding();
|
|
171
|
+
const handle = this.#readHandle(input.handle, binding, "node");
|
|
172
|
+
const view = await this.#view(binding, handle.level);
|
|
173
|
+
const page = await this.#store.getPage({
|
|
174
|
+
id: handle.recordId,
|
|
175
|
+
read: view.read
|
|
176
|
+
});
|
|
177
|
+
if (!page || page.deletedAt) throw new KnowledgeInspectorError("not-visible", "Knowledge page is not visible in the selected scope.");
|
|
178
|
+
const limit = boundedLimit(input.recordLimit, DEFAULT_NOTE_LIMIT, MAX_NOTE_LIMIT);
|
|
179
|
+
const notesAfter = this.#consumeCursor(input.recordsCursor, binding, handle.level, "records");
|
|
180
|
+
const mentioningAfter = this.#consumeCursor(input.mentioningRecordsCursor, binding, handle.level, "mentioning-records");
|
|
181
|
+
const [notes, mentioning] = await Promise.all([this.#store.listNotes({
|
|
182
|
+
pageId: page.id,
|
|
183
|
+
read: view.read,
|
|
184
|
+
cursor: notesAfter,
|
|
185
|
+
limit
|
|
186
|
+
}), this.#store.listNotesMentioning({
|
|
187
|
+
pageId: page.id,
|
|
188
|
+
read: view.read,
|
|
189
|
+
cursor: mentioningAfter,
|
|
190
|
+
limit
|
|
191
|
+
})]);
|
|
192
|
+
const body = truncateUtf8(page.body ?? "", MAX_BODY_BYTES);
|
|
193
|
+
const [outgoing, incoming, counts] = await Promise.all([
|
|
194
|
+
this.#outgoing(page, notes.notes, view),
|
|
195
|
+
this.#incoming(mentioning.notes, view),
|
|
196
|
+
this.#counts(page, view)
|
|
197
|
+
]);
|
|
198
|
+
const links = await Promise.all(parseKnowledgeWikilinks(body.value).slice(0, MAX_RELATED_PAGES).map(async (label) => {
|
|
199
|
+
const [target] = await this.#store.findPagesByName({
|
|
200
|
+
name: label,
|
|
201
|
+
read: view.read,
|
|
202
|
+
limit: 1
|
|
203
|
+
});
|
|
204
|
+
return {
|
|
205
|
+
label,
|
|
206
|
+
node: target ? await this.#summary(target, binding, view) : void 0
|
|
207
|
+
};
|
|
208
|
+
}));
|
|
209
|
+
const summaries = (pages) => Promise.all(pages.map((related) => this.#summary(related, binding, view)));
|
|
210
|
+
const detail = {
|
|
211
|
+
identityKey: binding.identityKey,
|
|
212
|
+
scopeLevel: handle.level,
|
|
213
|
+
node: {
|
|
214
|
+
...await this.#summary(page, binding, view),
|
|
215
|
+
relationshipCounts: counts
|
|
216
|
+
},
|
|
217
|
+
records: await Promise.all(notes.notes.map((note) => this.#noteSummary(note))),
|
|
218
|
+
recordsNextCursor: notes.nextCursor ? this.#mintCursor(binding, handle.level, "records", notes.nextCursor) : void 0,
|
|
219
|
+
mentioningRecords: await Promise.all(mentioning.notes.map((note) => this.#noteSummary(note))),
|
|
220
|
+
mentioningRecordsNextCursor: mentioning.nextCursor ? this.#mintCursor(binding, handle.level, "mentioning-records", mentioning.nextCursor) : void 0,
|
|
221
|
+
outgoingTargets: {
|
|
222
|
+
nodes: await summaries(outgoing.pages),
|
|
223
|
+
partial: outgoing.truncated || Boolean(notes.nextCursor)
|
|
224
|
+
},
|
|
225
|
+
incomingParents: {
|
|
226
|
+
nodes: await summaries(incoming.pages),
|
|
227
|
+
partial: incoming.truncated || Boolean(mentioning.nextCursor)
|
|
228
|
+
},
|
|
229
|
+
relationshipCounts: counts,
|
|
230
|
+
content: page.body === void 0 ? void 0 : body.value,
|
|
231
|
+
contentTruncated: body.truncated,
|
|
232
|
+
links
|
|
233
|
+
};
|
|
234
|
+
await this.#assertStable(binding);
|
|
235
|
+
return detail;
|
|
236
|
+
}
|
|
237
|
+
async listActivity(input) {
|
|
238
|
+
const binding = await this.#binding();
|
|
239
|
+
const view = await this.#view(binding, input.level);
|
|
240
|
+
const limit = boundedLimit(input.limit, DEFAULT_ACTIVITY_LIMIT, MAX_ACTIVITY_LIMIT);
|
|
241
|
+
const cursor = this.#consumeCursor(input.cursor, binding, input.level, "activity");
|
|
242
|
+
const result = await this.#store.listPageActivity({
|
|
243
|
+
read: view.read,
|
|
244
|
+
cursor,
|
|
245
|
+
limit
|
|
246
|
+
});
|
|
247
|
+
const events = [];
|
|
248
|
+
for (const event of result.events) events.push(await this.#activityEvent(event, binding, view));
|
|
249
|
+
await this.#assertStable(binding);
|
|
250
|
+
return {
|
|
251
|
+
identityKey: binding.identityKey,
|
|
252
|
+
scopeLevel: input.level,
|
|
253
|
+
events,
|
|
254
|
+
nextCursor: result.nextCursor ? this.#mintCursor(binding, input.level, "activity", result.nextCursor) : void 0
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
async #binding() {
|
|
258
|
+
const session = resolveKnowledgeSessionScopes(this.#session.state.get(), this.#hostScopes);
|
|
259
|
+
const sessionResourceId = this.#session.identity.getResourceId();
|
|
260
|
+
if (!session.resolved) {
|
|
261
|
+
this.#invalidate();
|
|
262
|
+
throw new KnowledgeInspectorError("unavailable", `Knowledge inspection unavailable: ${session.reason}`);
|
|
263
|
+
}
|
|
264
|
+
if (!sessionResourceId) throw new KnowledgeInspectorError("unavailable", "Knowledge inspection requires an active project.");
|
|
265
|
+
const sessionTags = knowledgeSessionTags(session.scopes);
|
|
266
|
+
const resourceId = sessionResourceId;
|
|
267
|
+
const activeThreadId = this.#session.thread.getId() ?? void 0;
|
|
268
|
+
const thread = activeThreadId ? await this.#session.thread.getById({ threadId: activeThreadId }) : null;
|
|
269
|
+
const threadId = thread?.resourceId === sessionResourceId ? thread.id : void 0;
|
|
270
|
+
const fingerprint = `${sessionTags.join("")}\0${resourceId}\0${threadId ?? ""}`;
|
|
271
|
+
if (this.#fingerprint !== fingerprint) {
|
|
272
|
+
this.#fingerprint = fingerprint;
|
|
273
|
+
this.#identityKey = opaqueToken();
|
|
274
|
+
this.#handles.clear();
|
|
275
|
+
this.#cursors.clear();
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
sessionTags,
|
|
279
|
+
resourceId,
|
|
280
|
+
threadId,
|
|
281
|
+
fingerprint,
|
|
282
|
+
identityKey: this.#identityKey
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/** The principals a level reads with: the session's shared tags, plus its resource, plus its thread. */
|
|
286
|
+
async #view(binding, level) {
|
|
287
|
+
let principals;
|
|
288
|
+
if (level === "org") {
|
|
289
|
+
if (binding.sessionTags.length === 0) throw new KnowledgeInspectorError("unavailable", "This session holds no shared scope tags.");
|
|
290
|
+
principals = [...binding.sessionTags];
|
|
291
|
+
} else {
|
|
292
|
+
principals = [...binding.sessionTags, `resource:${encodeKnowledgeScopeId(binding.resourceId)}`];
|
|
293
|
+
if (level === "thread") {
|
|
294
|
+
if (!binding.threadId) throw new KnowledgeInspectorError("unavailable", "The active thread does not belong to this project.");
|
|
295
|
+
principals.push(`thread:${encodeKnowledgeScopeId(binding.threadId)}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
return {
|
|
300
|
+
level,
|
|
301
|
+
read: await this.#knowledge.readScopeFor(principals)
|
|
302
|
+
};
|
|
303
|
+
} catch (error) {
|
|
304
|
+
throw new KnowledgeInspectorError("unavailable", `Knowledge inspection unavailable: ${error instanceof Error ? error.message : String(error)}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async #assertStable(binding) {
|
|
308
|
+
const current = await this.#binding();
|
|
309
|
+
if (current.identityKey !== binding.identityKey || current.fingerprint !== binding.fingerprint) throw new KnowledgeInspectorError("stale-handle", "Knowledge scope changed while the request was running.");
|
|
310
|
+
}
|
|
311
|
+
async #scopeAddress(scopeId) {
|
|
312
|
+
if (!this.#scopeAddresses.has(scopeId)) {
|
|
313
|
+
const page = await this.#store.getPage({
|
|
314
|
+
id: scopeId,
|
|
315
|
+
read: "all"
|
|
316
|
+
});
|
|
317
|
+
this.#scopeAddresses.set(scopeId, page?.address);
|
|
318
|
+
}
|
|
319
|
+
return this.#scopeAddresses.get(scopeId);
|
|
320
|
+
}
|
|
321
|
+
async #summary(page, binding, view) {
|
|
322
|
+
return {
|
|
323
|
+
handle: this.#mintHandle(binding, view.level, "node", page.id),
|
|
324
|
+
id: page.id,
|
|
325
|
+
type: "node",
|
|
326
|
+
name: page.name,
|
|
327
|
+
scope: badgeOf(await this.#scopeAddress(page.scopeId)),
|
|
328
|
+
version: page.version,
|
|
329
|
+
updatedAt: page.updatedAt.toISOString()
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
async #noteSummary(note) {
|
|
333
|
+
return {
|
|
334
|
+
text: note.text,
|
|
335
|
+
scope: badgeOf(await this.#scopeAddress(note.stampScopeIds[0])),
|
|
336
|
+
sourceThreadId: note.provenance.sourceThreadId,
|
|
337
|
+
capturedAt: note.createdAt.toISOString(),
|
|
338
|
+
when: note.when?.toISOString()
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/** The first pages by name, most linked first; ties keep name order. */
|
|
342
|
+
async #ranked(view, namePrefix) {
|
|
343
|
+
const { pages } = await this.#store.listPages({
|
|
344
|
+
read: view.read,
|
|
345
|
+
namePrefix,
|
|
346
|
+
limit: MAX_RANK_CANDIDATES
|
|
347
|
+
});
|
|
348
|
+
const ranked = await Promise.all(pages.map(async (page, order) => {
|
|
349
|
+
const counts = await this.#counts(page, view);
|
|
350
|
+
return {
|
|
351
|
+
id: page.id,
|
|
352
|
+
order,
|
|
353
|
+
counts,
|
|
354
|
+
degree: counts.outgoing + counts.incoming
|
|
355
|
+
};
|
|
356
|
+
}));
|
|
357
|
+
ranked.sort((a, b) => b.degree - a.degree || a.order - b.order);
|
|
358
|
+
return {
|
|
359
|
+
offset: 0,
|
|
360
|
+
entries: ranked.map(({ id, counts }) => ({
|
|
361
|
+
id,
|
|
362
|
+
counts
|
|
363
|
+
}))
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
async #counts(page, view) {
|
|
367
|
+
const [notes, mentioning] = await Promise.all([this.#store.listNotes({
|
|
368
|
+
pageId: page.id,
|
|
369
|
+
read: view.read,
|
|
370
|
+
limit: MAX_RANK_NOTES
|
|
371
|
+
}), this.#store.listNotesMentioning({
|
|
372
|
+
pageId: page.id,
|
|
373
|
+
read: view.read,
|
|
374
|
+
limit: MAX_RANK_NOTES
|
|
375
|
+
})]);
|
|
376
|
+
const [outgoing, incoming] = await Promise.all([this.#outgoing(page, notes.notes, view), this.#incoming(mentioning.notes, view)]);
|
|
377
|
+
return {
|
|
378
|
+
records: notes.notes.length + mentioning.notes.length,
|
|
379
|
+
outgoing: outgoing.pages.length,
|
|
380
|
+
incoming: incoming.pages.length,
|
|
381
|
+
sampled: Boolean(notes.nextCursor || mentioning.nextCursor || outgoing.truncated || incoming.truncated)
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/** Readable pages this page's notes mention, then pages its body links to by name. */
|
|
385
|
+
async #outgoing(page, notes, view) {
|
|
386
|
+
const related = /* @__PURE__ */ new Map();
|
|
387
|
+
const add = (target) => {
|
|
388
|
+
if (target && !target.deletedAt && target.id !== page.id) related.set(target.id, target);
|
|
389
|
+
};
|
|
390
|
+
for (const id of new Set(notes.flatMap((note) => note.mentions))) {
|
|
391
|
+
if (related.size >= MAX_RELATED_PAGES) return {
|
|
392
|
+
pages: [...related.values()],
|
|
393
|
+
truncated: true
|
|
394
|
+
};
|
|
395
|
+
add(await this.#store.getPage({
|
|
396
|
+
id,
|
|
397
|
+
read: view.read
|
|
398
|
+
}));
|
|
399
|
+
}
|
|
400
|
+
for (const name of parseKnowledgeWikilinks(page.body ?? "")) {
|
|
401
|
+
if (related.size >= MAX_RELATED_PAGES) return {
|
|
402
|
+
pages: [...related.values()],
|
|
403
|
+
truncated: true
|
|
404
|
+
};
|
|
405
|
+
const [target] = await this.#store.findPagesByName({
|
|
406
|
+
name,
|
|
407
|
+
read: view.read,
|
|
408
|
+
limit: 1
|
|
409
|
+
});
|
|
410
|
+
add(target);
|
|
411
|
+
}
|
|
412
|
+
return {
|
|
413
|
+
pages: [...related.values()],
|
|
414
|
+
truncated: false
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/** Readable pages whose notes mention this page. */
|
|
418
|
+
async #incoming(mentioning, view) {
|
|
419
|
+
const related = /* @__PURE__ */ new Map();
|
|
420
|
+
for (const pageId of new Set(mentioning.map((note) => note.pageId))) {
|
|
421
|
+
if (related.size >= MAX_RELATED_PAGES) return {
|
|
422
|
+
pages: [...related.values()],
|
|
423
|
+
truncated: true
|
|
424
|
+
};
|
|
425
|
+
const source = await this.#store.getPage({
|
|
426
|
+
id: pageId,
|
|
427
|
+
read: view.read
|
|
428
|
+
});
|
|
429
|
+
if (source && !source.deletedAt) related.set(source.id, source);
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
pages: [...related.values()],
|
|
433
|
+
truncated: false
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
async #activityEvent(event, binding, view) {
|
|
437
|
+
const page = await this.#store.getPage({
|
|
438
|
+
id: event.pageId,
|
|
439
|
+
read: view.read
|
|
440
|
+
});
|
|
441
|
+
const live = page && !page.deletedAt ? page : void 0;
|
|
442
|
+
return {
|
|
443
|
+
action: event.action,
|
|
444
|
+
recordType: event.noteId ? "note" : "page",
|
|
445
|
+
scope: badgeOf(await this.#scopeAddress(event.scopeId)),
|
|
446
|
+
sourceThreadId: event.provenance.sourceThreadId,
|
|
447
|
+
createdAt: event.createdAt.toISOString(),
|
|
448
|
+
record: live ? await this.#summary(live, binding, view) : void 0
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
#mintHandle(binding, level, type, recordId) {
|
|
452
|
+
this.#pruneOpaqueEntries();
|
|
453
|
+
const token = opaqueToken();
|
|
454
|
+
this.#handles.set(token, {
|
|
455
|
+
identityKey: binding.identityKey,
|
|
456
|
+
level,
|
|
457
|
+
type,
|
|
458
|
+
recordId,
|
|
459
|
+
expiresAt: Date.now() + HANDLE_TTL_MS
|
|
460
|
+
});
|
|
461
|
+
return token;
|
|
462
|
+
}
|
|
463
|
+
#readHandle(handle, binding, expectedType) {
|
|
464
|
+
const entry = this.#handles.get(handle);
|
|
465
|
+
if (!entry || entry.expiresAt < Date.now() || entry.type !== expectedType) throw new KnowledgeInspectorError("invalid-handle", "Knowledge record handle is invalid or expired.");
|
|
466
|
+
if (entry.identityKey !== binding.identityKey) throw new KnowledgeInspectorError("stale-handle", "Knowledge record handle belongs to a previous scope.");
|
|
467
|
+
return entry;
|
|
468
|
+
}
|
|
469
|
+
#mintCursor(binding, level, kind, value, filters) {
|
|
470
|
+
this.#pruneOpaqueEntries();
|
|
471
|
+
const token = opaqueToken();
|
|
472
|
+
this.#cursors.set(token, {
|
|
473
|
+
identityKey: binding.identityKey,
|
|
474
|
+
level,
|
|
475
|
+
kind,
|
|
476
|
+
value,
|
|
477
|
+
filters,
|
|
478
|
+
expiresAt: Date.now() + HANDLE_TTL_MS
|
|
479
|
+
});
|
|
480
|
+
return token;
|
|
481
|
+
}
|
|
482
|
+
#consumeCursor(cursor, binding, level, kind, filters) {
|
|
483
|
+
if (!cursor) return void 0;
|
|
484
|
+
const entry = this.#cursors.get(cursor);
|
|
485
|
+
if (!entry || entry.expiresAt < Date.now() || entry.identityKey !== binding.identityKey || entry.level !== level || entry.kind !== kind || entry.filters?.namePrefix !== filters?.namePrefix || entry.filters?.sort !== filters?.sort) throw new KnowledgeInspectorError("invalid-cursor", "Knowledge cursor does not match the active scope and filters.");
|
|
486
|
+
return entry.value;
|
|
487
|
+
}
|
|
488
|
+
#pruneOpaqueEntries() {
|
|
489
|
+
const now = Date.now();
|
|
490
|
+
for (const [token, entry] of this.#handles) if (entry.expiresAt < now) this.#handles.delete(token);
|
|
491
|
+
for (const [token, entry] of this.#cursors) if (entry.expiresAt < now) this.#cursors.delete(token);
|
|
492
|
+
while (this.#handles.size + this.#cursors.size >= MAX_OPAQUE_ENTRIES) {
|
|
493
|
+
const handle = this.#handles.keys().next().value;
|
|
494
|
+
if (handle) this.#handles.delete(handle);
|
|
495
|
+
else {
|
|
496
|
+
const cursor = this.#cursors.keys().next().value;
|
|
497
|
+
if (cursor) this.#cursors.delete(cursor);
|
|
498
|
+
else break;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
#invalidate() {
|
|
503
|
+
this.#fingerprint = void 0;
|
|
504
|
+
this.#identityKey = opaqueToken();
|
|
505
|
+
this.#handles.clear();
|
|
506
|
+
this.#cursors.clear();
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
/**
|
|
510
|
+
* The /knowledge inspector for a session, reading through the session's Knowledge runtime (a key
|
|
511
|
+
* resolves through `mastra`). Undefined when there is no Knowledge runtime: access rules live on the
|
|
512
|
+
* runtime, so a bare storage domain can't be inspected.
|
|
513
|
+
*/
|
|
514
|
+
async function createKnowledgeInspector(input) {
|
|
515
|
+
let selected;
|
|
516
|
+
if (typeof input.knowledge === "string") {
|
|
517
|
+
try {
|
|
518
|
+
selected = input.mastra?.getKnowledge(input.knowledge);
|
|
519
|
+
} catch {
|
|
520
|
+
throw new KnowledgeInspectorError("unavailable", "The configured Knowledge runtime is unavailable.");
|
|
521
|
+
}
|
|
522
|
+
if (!selected) throw new KnowledgeInspectorError("unavailable", "The configured Knowledge runtime is unavailable.");
|
|
523
|
+
} else selected = input.knowledge;
|
|
524
|
+
if (!selected) return void 0;
|
|
525
|
+
const store = await selected.getStorage();
|
|
526
|
+
return new ScopedKnowledgeInspector({
|
|
527
|
+
knowledge: selected,
|
|
528
|
+
store,
|
|
529
|
+
session: input.session,
|
|
530
|
+
hostScopes: input.hostScopes
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
export { KnowledgeInspectorError, createKnowledgeInspector };
|