nova-personal-agent-server 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +30 -2
- package/bin/novapersonal-server.mjs +4 -0
- package/package.json +61 -4
- package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
- package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
- package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
- package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
- package/resources/native/codex-sandbox-probe +0 -0
- package/resources/native/project-native/nova_project_native.node +0 -0
- package/resources/native-resources-v1.json +1 -0
- package/runtime/dist/eval/demos.js +156 -0
- package/runtime/dist/eval/fixture-host.js +385 -0
- package/runtime/dist/eval/fixtures.js +319 -0
- package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
- package/runtime/dist/eval/scorecard.js +285 -0
- package/runtime/dist/eval/sim.js +43 -0
- package/runtime/dist/src/cascaded-text-provider.js +31 -0
- package/runtime/dist/src/cli.js +96 -0
- package/runtime/dist/src/composition/assembly.js +352 -0
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
- package/runtime/dist/src/composition/production-composition.js +389 -0
- package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
- package/runtime/dist/src/config/capability-registry.js +315 -0
- package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
- package/runtime/dist/src/config/coding-backends.js +8 -0
- package/runtime/dist/src/config/coding-profiles.js +98 -0
- package/runtime/dist/src/config/config.js +857 -0
- package/runtime/dist/src/config/diagnostics.js +139 -0
- package/runtime/dist/src/config/environment-contract.js +202 -0
- package/runtime/dist/src/config/self-hosted.js +23 -0
- package/runtime/dist/src/connectors/composio/client.js +195 -0
- package/runtime/dist/src/connectors/composio/google.js +208 -0
- package/runtime/dist/src/connectors/composio/index.js +334 -0
- package/runtime/dist/src/connectors/feishu/cli.js +220 -0
- package/runtime/dist/src/connectors/feishu/index.js +980 -0
- package/runtime/dist/src/connectors/macos/calendar.js +35 -0
- package/runtime/dist/src/connectors/macos/mail.js +32 -0
- package/runtime/dist/src/connectors/macos/reader.js +62 -0
- package/runtime/dist/src/conversation-voice-provider.js +41 -0
- package/runtime/dist/src/core/approval-port.js +9 -0
- package/runtime/dist/src/core/approval.js +1328 -0
- package/runtime/dist/src/core/calls.js +35 -0
- package/runtime/dist/src/core/camera-session.js +32 -0
- package/runtime/dist/src/core/causal-runtime.js +650 -0
- package/runtime/dist/src/core/clock.js +139 -0
- package/runtime/dist/src/core/context-view.js +156 -0
- package/runtime/dist/src/core/effects.js +62 -0
- package/runtime/dist/src/core/events.js +217 -0
- package/runtime/dist/src/core/executor-diagnostic.js +9 -0
- package/runtime/dist/src/core/ids.js +32 -0
- package/runtime/dist/src/core/media-store.js +104 -0
- package/runtime/dist/src/core/memory.js +296 -0
- package/runtime/dist/src/core/pending-decision.js +64 -0
- package/runtime/dist/src/core/ports.js +125 -0
- package/runtime/dist/src/core/runtime.js +1459 -0
- package/runtime/dist/src/core/schedule-tools.js +15 -0
- package/runtime/dist/src/core/sims.js +186 -0
- package/runtime/dist/src/core/slots.js +97 -0
- package/runtime/dist/src/core/source-processing-instructions.js +3 -0
- package/runtime/dist/src/core/source-processing-tools.js +12 -0
- package/runtime/dist/src/core/suggestions.js +143 -0
- package/runtime/dist/src/core/task-tools.js +22 -0
- package/runtime/dist/src/core/tool-schema.js +212 -0
- package/runtime/dist/src/core/trace.js +34 -0
- package/runtime/dist/src/core/work-tools.js +94 -0
- package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
- package/runtime/dist/src/desktop/desktop-camera.js +281 -0
- package/runtime/dist/src/desktop/desktop-control.js +140 -0
- package/runtime/dist/src/desktop/desktop-progress.js +162 -0
- package/runtime/dist/src/desktop/desktop-session.js +2106 -0
- package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
- package/runtime/dist/src/desktop/desktop-wire.js +605 -0
- package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
- package/runtime/dist/src/desktop/startup-error.js +18 -0
- package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
- package/runtime/dist/src/desktop-entry.js +106 -0
- package/runtime/dist/src/desktop.js +1092 -0
- package/runtime/dist/src/executors/acp/backends.js +105 -0
- package/runtime/dist/src/executors/acp/mcp.js +81 -0
- package/runtime/dist/src/executors/acp/preflight.js +14 -0
- package/runtime/dist/src/executors/acp/process-owner.js +112 -0
- package/runtime/dist/src/executors/acp/transport.js +760 -0
- package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
- package/runtime/dist/src/executors/agent-controller.js +244 -0
- package/runtime/dist/src/executors/autoglm.js +376 -0
- package/runtime/dist/src/executors/browser-device.js +102 -0
- package/runtime/dist/src/executors/browser.js +61 -0
- package/runtime/dist/src/executors/camera.js +101 -0
- package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
- package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
- package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
- package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
- package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
- package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
- package/runtime/dist/src/executors/codex/common.js +655 -0
- package/runtime/dist/src/executors/codex/contract.js +581 -0
- package/runtime/dist/src/executors/codex/controller.js +141 -0
- package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
- package/runtime/dist/src/executors/codex/factory.js +463 -0
- package/runtime/dist/src/executors/codex/host-config.js +238 -0
- package/runtime/dist/src/executors/codex/host.js +17 -0
- package/runtime/dist/src/executors/codex/index.js +19 -0
- package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
- package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
- package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
- package/runtime/dist/src/executors/codex/process-owner.js +398 -0
- package/runtime/dist/src/executors/codex/production-host.js +887 -0
- package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
- package/runtime/dist/src/executors/codex/protocol.js +581 -0
- package/runtime/dist/src/executors/codex/safe-json.js +87 -0
- package/runtime/dist/src/executors/codex/session-title.js +61 -0
- package/runtime/dist/src/executors/codex/shared-home.js +29 -0
- package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
- package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
- package/runtime/dist/src/executors/codex/version.js +38 -0
- package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
- package/runtime/dist/src/executors/coding/intake-model.js +103 -0
- package/runtime/dist/src/executors/coding/intake.js +824 -0
- package/runtime/dist/src/executors/coding/json-completion.js +14 -0
- package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
- package/runtime/dist/src/executors/coding/work-order.js +50 -0
- package/runtime/dist/src/executors/coding-executor.js +23 -0
- package/runtime/dist/src/executors/computer-backend.js +188 -0
- package/runtime/dist/src/executors/computer-device.js +187 -0
- package/runtime/dist/src/executors/computer-worker.js +125 -0
- package/runtime/dist/src/executors/computer.js +100 -0
- package/runtime/dist/src/executors/device-lock.js +127 -0
- package/runtime/dist/src/executors/frame-source.js +29 -0
- package/runtime/dist/src/executors/gui-evidence.js +30 -0
- package/runtime/dist/src/executors/gui-execution.js +237 -0
- package/runtime/dist/src/executors/gui-runner.js +88 -0
- package/runtime/dist/src/executors/host-executor-capability.js +12 -0
- package/runtime/dist/src/executors/index.js +13 -0
- package/runtime/dist/src/executors/mcp-client.js +198 -0
- package/runtime/dist/src/executors/mcp.js +210 -0
- package/runtime/dist/src/executors/mobile-android.js +180 -0
- package/runtime/dist/src/executors/mobile-ios.js +303 -0
- package/runtime/dist/src/executors/mobile.js +78 -0
- package/runtime/dist/src/executors/search-mcp.js +146 -0
- package/runtime/dist/src/executors/search.js +524 -0
- package/runtime/dist/src/executors/task-resources.js +38 -0
- package/runtime/dist/src/executors/vision/assess.js +296 -0
- package/runtime/dist/src/executors/vision/controller-core.js +373 -0
- package/runtime/dist/src/executors/vision/controller.js +164 -0
- package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
- package/runtime/dist/src/executors/vision.js +5 -0
- package/runtime/dist/src/executors/watcher.js +718 -0
- package/runtime/dist/src/http/bounded-response.js +37 -0
- package/runtime/dist/src/index.js +56 -0
- package/runtime/dist/src/knowledge/assembly.js +60 -0
- package/runtime/dist/src/knowledge/documents.js +520 -0
- package/runtime/dist/src/knowledge/embeddings.js +201 -0
- package/runtime/dist/src/knowledge/mcp.js +218 -0
- package/runtime/dist/src/knowledge/parse-worker.js +122 -0
- package/runtime/dist/src/knowledge/references.js +51 -0
- package/runtime/dist/src/knowledge/service.js +526 -0
- package/runtime/dist/src/knowledge/store-client.js +185 -0
- package/runtime/dist/src/knowledge/store-worker.js +843 -0
- package/runtime/dist/src/knowledge/types.js +2 -0
- package/runtime/dist/src/mcp/http-server.js +177 -0
- package/runtime/dist/src/mem0/resource.js +35 -0
- package/runtime/dist/src/mem0/store-worker.js +330 -0
- package/runtime/dist/src/memory/blackboard-session.js +205 -0
- package/runtime/dist/src/memory/blackboard-store.js +194 -0
- package/runtime/dist/src/memory/blackboard-worker.js +369 -0
- package/runtime/dist/src/memory/entry.js +39 -0
- package/runtime/dist/src/memory/factory.js +20 -0
- package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
- package/runtime/dist/src/memory/personal-memory.js +10 -0
- package/runtime/dist/src/memory/prerecall.js +20 -0
- package/runtime/dist/src/memory/provenance.js +17 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
- package/runtime/dist/src/memory/retrieval.js +235 -0
- package/runtime/dist/src/memory/sensitivity.js +261 -0
- package/runtime/dist/src/memory-ledger/models.js +98 -0
- package/runtime/dist/src/memory-ledger/store-client.js +210 -0
- package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
- package/runtime/dist/src/memory-ledger/store.js +597 -0
- package/runtime/dist/src/memory-substrate/candidates.js +16 -0
- package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
- package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
- package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
- package/runtime/dist/src/memory-substrate/migration.js +87 -0
- package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
- package/runtime/dist/src/memory-substrate/purge.js +398 -0
- package/runtime/dist/src/memory-substrate/resolution.js +32 -0
- package/runtime/dist/src/memory-substrate/resource.js +867 -0
- package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
- package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
- package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
- package/runtime/dist/src/memory-substrate/source-state.js +93 -0
- package/runtime/dist/src/memory-substrate/store.js +534 -0
- package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
- package/runtime/dist/src/memory-substrate/workspace.js +21 -0
- package/runtime/dist/src/model/jev-client.js +25 -0
- package/runtime/dist/src/model/model-adapters.js +64 -0
- package/runtime/dist/src/model/model-gateway.js +399 -0
- package/runtime/dist/src/model/model-lane.js +34 -0
- package/runtime/dist/src/model/model-purpose.js +5 -0
- package/runtime/dist/src/model/personal-writer.js +361 -0
- package/runtime/dist/src/model/proactivity.js +72 -0
- package/runtime/dist/src/model/prompting.js +279 -0
- package/runtime/dist/src/model/task-verifier.js +217 -0
- package/runtime/dist/src/model/vision-capability.js +10 -0
- package/runtime/dist/src/news/feeds.js +114 -0
- package/runtime/dist/src/news/jev-ranking.js +25 -0
- package/runtime/dist/src/news/ranking.js +26 -0
- package/runtime/dist/src/news/service.js +262 -0
- package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
- package/runtime/dist/src/notifications/adapters/nova.js +15 -0
- package/runtime/dist/src/notifications/content.js +15 -0
- package/runtime/dist/src/notifications/contracts.js +9 -0
- package/runtime/dist/src/notifications/feed.js +8 -0
- package/runtime/dist/src/notifications/service.js +412 -0
- package/runtime/dist/src/notifications/store.js +4 -0
- package/runtime/dist/src/personal-agent/app-context.js +113 -0
- package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
- package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
- package/runtime/dist/src/personal-agent/contracts.js +32 -0
- package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
- package/runtime/dist/src/personal-agent/conversations.js +187 -0
- package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2403 -0
- package/runtime/dist/src/personal-agent/intake-task.js +37 -0
- package/runtime/dist/src/personal-agent/life.js +260 -0
- package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
- package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
- package/runtime/dist/src/personal-agent/project-digests.js +242 -0
- package/runtime/dist/src/personal-agent/sampling.js +20 -0
- package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
- package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
- package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
- package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
- package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
- package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
- package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
- package/runtime/dist/src/personal-agent/source-priority.js +142 -0
- package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
- package/runtime/dist/src/personal-agent/source-walk.js +25 -0
- package/runtime/dist/src/personal-agent/sources.js +1484 -0
- package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
- package/runtime/dist/src/personal-agent/store.js +146 -0
- package/runtime/dist/src/personal-agent/task-loop.js +163 -0
- package/runtime/dist/src/personal-agent/tasks.js +920 -0
- package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
- package/runtime/dist/src/personal-agent/understanding.js +160 -0
- package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
- package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
- package/runtime/dist/src/projects/host-paths.js +133 -0
- package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
- package/runtime/dist/src/projects/project-confirmation.js +486 -0
- package/runtime/dist/src/projects/project-native-resource.js +284 -0
- package/runtime/dist/src/projects/project-node-files.js +169 -0
- package/runtime/dist/src/projects/project-root-file.js +16 -0
- package/runtime/dist/src/projects/project-state.js +504 -0
- package/runtime/dist/src/projects/project-store-files.js +1271 -0
- package/runtime/dist/src/projects/project-store.js +1345 -0
- package/runtime/dist/src/realtime/aoq.js +141 -0
- package/runtime/dist/src/realtime/bridge.js +652 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
- package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
- package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
- package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
- package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
- package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
- package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
- package/runtime/dist/src/realtime/dictation.js +28 -0
- package/runtime/dist/src/realtime/evidence.js +276 -0
- package/runtime/dist/src/realtime/floor.js +43 -0
- package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
- package/runtime/dist/src/realtime/gemini.js +517 -0
- package/runtime/dist/src/realtime/history.js +122 -0
- package/runtime/dist/src/realtime/host-delivery.js +1711 -0
- package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
- package/runtime/dist/src/realtime/memory-board.js +105 -0
- package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
- package/runtime/dist/src/realtime/playback.js +242 -0
- package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
- package/runtime/dist/src/realtime/prompt-language.js +17 -0
- package/runtime/dist/src/realtime/protocol.js +428 -0
- package/runtime/dist/src/realtime/provider-projection.js +526 -0
- package/runtime/dist/src/realtime/provider-session.js +491 -0
- package/runtime/dist/src/realtime/qwen-transport.js +144 -0
- package/runtime/dist/src/realtime/qwen.js +1377 -0
- package/runtime/dist/src/realtime/recall.js +341 -0
- package/runtime/dist/src/realtime/service-ports.js +8 -0
- package/runtime/dist/src/realtime/service-state.js +259 -0
- package/runtime/dist/src/realtime/service.js +1726 -0
- package/runtime/dist/src/realtime/session-state.js +684 -0
- package/runtime/dist/src/realtime/session.js +1976 -0
- package/runtime/dist/src/realtime/speech-prep.js +63 -0
- package/runtime/dist/src/realtime/streaming-speech.js +120 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
- package/runtime/dist/src/realtime/telemetry.js +144 -0
- package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
- package/runtime/dist/src/realtime/transport.js +8 -0
- package/runtime/dist/src/realtime/usage.js +55 -0
- package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
- package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
- package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
- package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
- package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
- package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
- package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
- package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
- package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
- package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
- package/runtime/dist/src/server/aoq-chat-server.js +497 -0
- package/runtime/dist/src/server/client-pairing.js +192 -0
- package/runtime/dist/src/server/client-protocol.js +87 -0
- package/runtime/dist/src/server/client-server.js +312 -0
- package/runtime/dist/src/server/server-config.js +72 -0
- package/runtime/dist/src/server-entry.js +135 -0
- package/runtime/dist/src/storage/bounded-json.js +63 -0
- package/runtime/dist/src/storage/native-file-lock.js +8 -0
- package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
- package/runtime/dist/src/storage/private-database.js +120 -0
- package/runtime/dist/src/text/canonical-json.js +60 -0
- package/runtime/dist/src/text/python-number.js +18 -0
- package/runtime/dist/src/text/python-text.js +92 -0
- package/runtime/dist/src/text/unicode-casefold.js +29 -0
- package/runtime/dist/src/text/unicode-normalize.js +138 -0
- package/runtime/dist/src/text/unicode-tables.js +210 -0
- package/runtime/dist/src/understanding/candidates.js +77 -0
- package/runtime/dist/src/understanding/fixture.js +7 -0
- package/runtime/dist/src/understanding/jev.js +20 -0
- package/runtime/dist/src/understanding/pipeline.js +38 -0
- package/runtime/dist/src/voicemem/store-client.js +476 -0
- package/runtime/dist/src/voicemem/store-worker.js +380 -0
- package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
- package/runtime/scripts/pair-device.mjs +109 -0
- package/src/command.mjs +48 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { abortable } from '../core/camera-session.js';
|
|
2
|
+
/** Cancel abandoned bodies; neither error messages nor upstream text cross this boundary. */
|
|
3
|
+
export async function readBoundedResponse(response, options) {
|
|
4
|
+
const declared = response.headers.get('content-length');
|
|
5
|
+
if (declared !== null && (!/^\d+$/u.test(declared) || Number(declared) > options.limit)) {
|
|
6
|
+
void response.body?.cancel().catch(() => { });
|
|
7
|
+
throw options.failure('response_too_large');
|
|
8
|
+
}
|
|
9
|
+
const body = response.body;
|
|
10
|
+
const reader = body?.getReader();
|
|
11
|
+
if (!reader)
|
|
12
|
+
return new Uint8Array();
|
|
13
|
+
const chunks = [];
|
|
14
|
+
let size = 0, done = false;
|
|
15
|
+
try {
|
|
16
|
+
for (;;) {
|
|
17
|
+
options.signal?.throwIfAborted();
|
|
18
|
+
const read = reader.read(), chunk = options.signal ? await abortable(read, options.signal) : await read;
|
|
19
|
+
if (chunk.done) {
|
|
20
|
+
done = true;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
size += chunk.value.byteLength;
|
|
24
|
+
options.consume?.(chunk.value.byteLength);
|
|
25
|
+
if (size > options.limit)
|
|
26
|
+
throw options.failure('response_too_large');
|
|
27
|
+
chunks.push(chunk.value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
if (!done)
|
|
32
|
+
void reader.cancel().catch(() => { });
|
|
33
|
+
reader.releaseLock();
|
|
34
|
+
}
|
|
35
|
+
return Buffer.concat(chunks, size);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=bounded-response.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export * from './core/causal-runtime.js';
|
|
2
|
+
export * from './composition/assembly.js';
|
|
3
|
+
export * from './executors/agent-controller.js';
|
|
4
|
+
export * from './core/calls.js';
|
|
5
|
+
export * from './core/clock.js';
|
|
6
|
+
export * from './executors/coding-executor.js';
|
|
7
|
+
export * from './core/approval-port.js';
|
|
8
|
+
export * from './cli.js';
|
|
9
|
+
export * from './config/config.js';
|
|
10
|
+
export * from './core/context-view.js';
|
|
11
|
+
export * from './desktop/desktop-session.js';
|
|
12
|
+
export * from './desktop.js';
|
|
13
|
+
export * from './core/effects.js';
|
|
14
|
+
export * from './core/events.js';
|
|
15
|
+
export * from './realtime/floor.js';
|
|
16
|
+
export * from './core/ids.js';
|
|
17
|
+
export * from './core/memory.js';
|
|
18
|
+
export * from './model/model-adapters.js';
|
|
19
|
+
export * from './model/proactivity.js';
|
|
20
|
+
export * from './model/personal-writer.js';
|
|
21
|
+
export * from './model/task-verifier.js';
|
|
22
|
+
export * from './model/model-gateway.js';
|
|
23
|
+
export * from './core/ports.js';
|
|
24
|
+
export * from './realtime/playback.js';
|
|
25
|
+
export * from './model/prompting.js';
|
|
26
|
+
export * from './composition/cascaded-realtime-assembly.js';
|
|
27
|
+
export * from './config/cascaded-realtime-config.js';
|
|
28
|
+
export * from './realtime/cascaded/llm.js';
|
|
29
|
+
export * from './realtime/cascaded/ports.js';
|
|
30
|
+
export * from './realtime/cascaded/adapter.js';
|
|
31
|
+
export * from './realtime/cascaded/provider.js';
|
|
32
|
+
export * from './realtime/cascaded/qwen-llm.js';
|
|
33
|
+
export { createChatCompletionsLlmFactory, ChatCompletionsLlmFailure } from './realtime/cascaded/chat-completions-llm.js';
|
|
34
|
+
export * from './realtime/cascaded/ark-llm.js';
|
|
35
|
+
export * from './realtime/evidence.js';
|
|
36
|
+
export * from './realtime/history.js';
|
|
37
|
+
export * from './realtime/memory-board.js';
|
|
38
|
+
export * from './projects/project-confirmation.js';
|
|
39
|
+
export * from './realtime/protocol.js';
|
|
40
|
+
export * from './realtime/recall.js';
|
|
41
|
+
export * from './realtime/provider-session.js';
|
|
42
|
+
export * from './realtime/session.js';
|
|
43
|
+
export * from './realtime/session-state.js';
|
|
44
|
+
export * from './realtime/speech-prep.js';
|
|
45
|
+
export * from './realtime/telemetry.js';
|
|
46
|
+
export * from './composition/realtime-assembly.js';
|
|
47
|
+
export * from './core/runtime.js';
|
|
48
|
+
export * from './core/sims.js';
|
|
49
|
+
export * from './core/slots.js';
|
|
50
|
+
export * from './core/suggestions.js';
|
|
51
|
+
export * from './core/tool-schema.js';
|
|
52
|
+
export * from './core/trace.js';
|
|
53
|
+
export * from './config/capability-registry.js';
|
|
54
|
+
export * from './executors/coding/target-resolution.js';
|
|
55
|
+
export { createGeminiAsrFactory, createGeminiTtsFactory } from './realtime/cascaded/gemini-speech.js';
|
|
56
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { resolveModelApiKey } from '../config/config.js';
|
|
4
|
+
import { DashScopeEmbeddingProvider } from './embeddings.js';
|
|
5
|
+
import { KnowledgeStoreClient } from './store-client.js';
|
|
6
|
+
import { KnowledgeService } from './service.js';
|
|
7
|
+
import { KnowledgeMcpAdapter, startKnowledgeMcpHttpServer } from './mcp.js';
|
|
8
|
+
/** Async resource admission before synchronous tool compilation or child creation. */
|
|
9
|
+
export async function prepareKnowledge(settings, capabilities, signal) {
|
|
10
|
+
if (!capabilities.modules.knowledge.enabled)
|
|
11
|
+
return undefined;
|
|
12
|
+
signal?.throwIfAborted();
|
|
13
|
+
const embedding = new DashScopeEmbeddingProvider({ baseUrl: settings.model_base_url,
|
|
14
|
+
apiKey: resolveModelApiKey(settings) ?? '', model: settings.embedding_model });
|
|
15
|
+
const configured = settings.knowledge_path;
|
|
16
|
+
const path = resolve(configured.startsWith('~/') ? resolve(homedir(), configured.slice(2)) : configured);
|
|
17
|
+
// Whole-computer sources share this store with manually added knowledge.
|
|
18
|
+
// The generic 100-source default would make every later scan import fail.
|
|
19
|
+
const service = new KnowledgeService({ store: new KnowledgeStoreClient({ path, maxSources: 2000 }), embedding, requireEvidenceLedger: true });
|
|
20
|
+
const adapter = new KnowledgeMcpAdapter(service);
|
|
21
|
+
let http;
|
|
22
|
+
let closing;
|
|
23
|
+
const close = () => closing ??= (async () => {
|
|
24
|
+
try {
|
|
25
|
+
await http?.close();
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
try {
|
|
29
|
+
await adapter.close();
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await service.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
})();
|
|
36
|
+
try {
|
|
37
|
+
await service.open();
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
await adapter.connect();
|
|
40
|
+
const grants = capabilities.modules.knowledge;
|
|
41
|
+
if ((grants.exposeToCodex || Object.values(grants.exposeToBackends ?? {}).some(Boolean))
|
|
42
|
+
&& capabilities.modules.coding.enabled)
|
|
43
|
+
http = await startKnowledgeMcpHttpServer(service);
|
|
44
|
+
signal?.throwIfAborted();
|
|
45
|
+
const tool = { enabled: true, timeoutMs: 8000, maxResultBytes: 32768, maxCallsPerTurn: 2 };
|
|
46
|
+
// One loopback entry; Codex reads `exposeTo.codex`, ACP backends read their own grant.
|
|
47
|
+
const codexEntries = http === undefined ? {} : { nova_knowledge: {
|
|
48
|
+
enabled: true, transport: 'streamable-http', url: http.url, headers: { authorization: `Bearer ${http.token}` },
|
|
49
|
+
exposeTo: { frontbrain: false, codex: grants.exposeToCodex,
|
|
50
|
+
...(grants.exposeToBackends === undefined ? {} : { backends: grants.exposeToBackends }) },
|
|
51
|
+
tools: { recall: { ...tool }, get_chunk: { ...tool } },
|
|
52
|
+
} };
|
|
53
|
+
return { service, embedding, adapter, capabilities, codexEntries, close };
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
await close();
|
|
57
|
+
throw new Error('knowledge_startup_failed');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=assembly.js.map
|
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { readBoundedResponse } from '../http/bounded-response.js';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { lookup as dnsLookup } from 'node:dns/promises';
|
|
4
|
+
import { constants } from 'node:fs';
|
|
5
|
+
import { basename, extname, isAbsolute, relative } from 'node:path';
|
|
6
|
+
import { BlockList, isIP } from 'node:net';
|
|
7
|
+
import { lstat, open, realpath } from 'node:fs/promises';
|
|
8
|
+
import { Worker } from 'node:worker_threads';
|
|
9
|
+
import { Agent, fetch as undiciFetch } from 'undici';
|
|
10
|
+
import { SensitiveContentPolicy, SensitivePathPolicy } from '../memory/sensitivity.js';
|
|
11
|
+
const MAX_FILE_BYTES = 10 * 1_024 * 1_024;
|
|
12
|
+
const MAX_TEXT_BYTES = 10 * 1_024 * 1_024;
|
|
13
|
+
const MAX_TEXT_CODE_POINTS = 2_621_440;
|
|
14
|
+
const CHUNK_CODE_POINTS = 3_200;
|
|
15
|
+
const CHUNK_OVERLAP = 480;
|
|
16
|
+
const FETCH_TIMEOUT_MS = 10_000;
|
|
17
|
+
const PARSE_TIMEOUT_MS = 15_000;
|
|
18
|
+
const MAX_REDIRECTS = 3;
|
|
19
|
+
const contentPolicy = new SensitiveContentPolicy();
|
|
20
|
+
const pathPolicy = new SensitivePathPolicy();
|
|
21
|
+
export class KnowledgeDocumentFailure extends Error {
|
|
22
|
+
code;
|
|
23
|
+
constructor(code) {
|
|
24
|
+
super(code);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.name = 'KnowledgeDocumentFailure';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Split screened text at Markdown headings, then bound long sections with 15% overlap. */
|
|
30
|
+
export function chunkKnowledgeText(text) {
|
|
31
|
+
if (typeof text !== 'string' || [...text].length > MAX_TEXT_CODE_POINTS) {
|
|
32
|
+
throw new KnowledgeDocumentFailure('invalid_text');
|
|
33
|
+
}
|
|
34
|
+
screenText('document', text);
|
|
35
|
+
const headings = [];
|
|
36
|
+
const sections = [];
|
|
37
|
+
let lines = [];
|
|
38
|
+
let currentHeading = '';
|
|
39
|
+
const flush = () => {
|
|
40
|
+
const value = lines.join('\n').trim();
|
|
41
|
+
if (value !== '')
|
|
42
|
+
sections.push({ heading: currentHeading, text: value });
|
|
43
|
+
lines = [];
|
|
44
|
+
};
|
|
45
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
46
|
+
const match = /^(#{1,6})[ \t]+(.+?)[ \t]*#*[ \t]*$/u.exec(line);
|
|
47
|
+
if (match === null) {
|
|
48
|
+
lines.push(line);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
flush();
|
|
52
|
+
const level = match[1]?.length ?? 1;
|
|
53
|
+
headings.length = level - 1;
|
|
54
|
+
headings[level - 1] = match[2]?.trim() ?? '';
|
|
55
|
+
currentHeading = headings.filter(Boolean).join(' > ');
|
|
56
|
+
}
|
|
57
|
+
flush();
|
|
58
|
+
const chunks = [];
|
|
59
|
+
for (const section of sections) {
|
|
60
|
+
const codePoints = [...section.text];
|
|
61
|
+
for (let start = 0; start < codePoints.length; start += CHUNK_CODE_POINTS - CHUNK_OVERLAP) {
|
|
62
|
+
const chunkText = codePoints.slice(start, start + CHUNK_CODE_POINTS).join('');
|
|
63
|
+
if (chunkText === '')
|
|
64
|
+
break;
|
|
65
|
+
chunks.push({
|
|
66
|
+
heading_path: section.heading,
|
|
67
|
+
text: chunkText,
|
|
68
|
+
token_estimate: Math.ceil(codePointsLength(chunkText) / 4),
|
|
69
|
+
});
|
|
70
|
+
if (start + CHUNK_CODE_POINTS >= codePoints.length)
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return chunks;
|
|
75
|
+
}
|
|
76
|
+
export async function readKnowledgeFile(path, signal, allowedRoot) {
|
|
77
|
+
signal?.throwIfAborted();
|
|
78
|
+
if (!isAbsolute(path) || !pathPolicy.allows(path))
|
|
79
|
+
throw new KnowledgeDocumentFailure('path_denied');
|
|
80
|
+
let canonical;
|
|
81
|
+
try {
|
|
82
|
+
canonical = await realpath(path);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new KnowledgeDocumentFailure('file_unavailable');
|
|
86
|
+
}
|
|
87
|
+
signal?.throwIfAborted();
|
|
88
|
+
if (!pathPolicy.allows(canonical))
|
|
89
|
+
throw new KnowledgeDocumentFailure('path_denied');
|
|
90
|
+
if (allowedRoot !== undefined) {
|
|
91
|
+
const child = relative(allowedRoot, canonical);
|
|
92
|
+
if (child === '..' || child.startsWith('../') || child.startsWith('..\\') || isAbsolute(child)) {
|
|
93
|
+
throw new KnowledgeDocumentFailure('path_denied');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const format = fileFormat(canonical);
|
|
97
|
+
if (format === null)
|
|
98
|
+
throw new KnowledgeDocumentFailure('unsupported_mime');
|
|
99
|
+
let admitted;
|
|
100
|
+
try {
|
|
101
|
+
admitted = await lstat(canonical, { bigint: true });
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
throw new KnowledgeDocumentFailure('file_unavailable');
|
|
105
|
+
}
|
|
106
|
+
if (!admitted.isFile() || admitted.isSymbolicLink())
|
|
107
|
+
throw new KnowledgeDocumentFailure('invalid_file');
|
|
108
|
+
signal?.throwIfAborted();
|
|
109
|
+
let handle;
|
|
110
|
+
try {
|
|
111
|
+
handle = await open(canonical, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
signal?.throwIfAborted();
|
|
115
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
116
|
+
}
|
|
117
|
+
let bytes;
|
|
118
|
+
try {
|
|
119
|
+
const info = await handle.stat({ bigint: true });
|
|
120
|
+
if (!info.isFile() || !sameIdentity(info, admitted))
|
|
121
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
122
|
+
await verifyPathIdentity(path, canonical, info);
|
|
123
|
+
if (info.size < 1n)
|
|
124
|
+
throw new KnowledgeDocumentFailure('invalid_file');
|
|
125
|
+
if (info.size > BigInt(MAX_FILE_BYTES))
|
|
126
|
+
throw new KnowledgeDocumentFailure('file_too_large');
|
|
127
|
+
signal?.throwIfAborted();
|
|
128
|
+
bytes = new Uint8Array(Number(info.size));
|
|
129
|
+
let offset = 0;
|
|
130
|
+
while (offset < bytes.length) {
|
|
131
|
+
signal?.throwIfAborted();
|
|
132
|
+
const read = await handle.read(bytes, offset, bytes.length - offset, offset);
|
|
133
|
+
if (read.bytesRead === 0)
|
|
134
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
135
|
+
offset += read.bytesRead;
|
|
136
|
+
}
|
|
137
|
+
const after = await handle.stat({ bigint: true });
|
|
138
|
+
if (!after.isFile() || after.size !== info.size || after.mtimeNs !== info.mtimeNs) {
|
|
139
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
140
|
+
}
|
|
141
|
+
await verifyPathIdentity(path, canonical, after);
|
|
142
|
+
}
|
|
143
|
+
catch (cause) {
|
|
144
|
+
signal?.throwIfAborted();
|
|
145
|
+
if (cause instanceof KnowledgeDocumentFailure)
|
|
146
|
+
throw cause;
|
|
147
|
+
throw new KnowledgeDocumentFailure('file_unavailable');
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
await handle.close().catch(() => undefined);
|
|
151
|
+
}
|
|
152
|
+
const text = await decodeDocument(bytes, format.kind, signal);
|
|
153
|
+
const title = basename(canonical);
|
|
154
|
+
screenText('title', title);
|
|
155
|
+
screenText('locator', canonical);
|
|
156
|
+
screenText('document', text);
|
|
157
|
+
return {
|
|
158
|
+
title,
|
|
159
|
+
mime: format.mime,
|
|
160
|
+
text,
|
|
161
|
+
bytes: bytes.byteLength,
|
|
162
|
+
fingerprint: createHash('sha256').update(bytes).digest('hex'),
|
|
163
|
+
locator: canonical,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export async function fetchKnowledgeUrl(url, signal) {
|
|
167
|
+
signal?.throwIfAborted();
|
|
168
|
+
let current = admittedUrl(url);
|
|
169
|
+
const dispatcher = new Agent({ connect: { lookup: publicLookup } });
|
|
170
|
+
const timeout = AbortSignal.timeout(FETCH_TIMEOUT_MS);
|
|
171
|
+
const combined = signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
|
|
172
|
+
try {
|
|
173
|
+
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects += 1) {
|
|
174
|
+
let response;
|
|
175
|
+
try {
|
|
176
|
+
response = await undiciFetch(current, { redirect: 'manual', signal: combined, dispatcher });
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
if (signal?.aborted === true)
|
|
180
|
+
signal.throwIfAborted();
|
|
181
|
+
if (timeout.aborted)
|
|
182
|
+
throw new KnowledgeDocumentFailure('timeout');
|
|
183
|
+
throw new KnowledgeDocumentFailure('network');
|
|
184
|
+
}
|
|
185
|
+
if (response.status >= 300 && response.status < 400) {
|
|
186
|
+
void response.body?.cancel();
|
|
187
|
+
if (redirects === MAX_REDIRECTS)
|
|
188
|
+
throw new KnowledgeDocumentFailure('redirect_denied');
|
|
189
|
+
const location = response.headers.get('location');
|
|
190
|
+
if (location === null)
|
|
191
|
+
throw new KnowledgeDocumentFailure('redirect_denied');
|
|
192
|
+
let redirected;
|
|
193
|
+
try {
|
|
194
|
+
redirected = new URL(location, current).href;
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
throw new KnowledgeDocumentFailure('redirect_denied');
|
|
198
|
+
}
|
|
199
|
+
current = admittedUrl(redirected);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (!response.ok) {
|
|
203
|
+
void response.body?.cancel();
|
|
204
|
+
throw new KnowledgeDocumentFailure('http_error');
|
|
205
|
+
}
|
|
206
|
+
const bytes = await readResponseBounded(response, combined);
|
|
207
|
+
const format = responseFormat(current, response.headers.get('content-type'));
|
|
208
|
+
const decoded = format.kind === 'html' ? decodeUtf8(bytes) : null;
|
|
209
|
+
const htmlTitle = decoded === null ? null : extractHtmlTitle(decoded);
|
|
210
|
+
const text = decoded === null ? await decodeDocument(bytes, format.kind, combined) : htmlToText(decoded);
|
|
211
|
+
if (text.trim() === '')
|
|
212
|
+
throw new KnowledgeDocumentFailure('empty_text');
|
|
213
|
+
const title = htmlTitle ?? (basename(current.pathname) || current.hostname);
|
|
214
|
+
screenText('title', title);
|
|
215
|
+
screenText('locator', current.href);
|
|
216
|
+
screenText('document', text);
|
|
217
|
+
return {
|
|
218
|
+
title,
|
|
219
|
+
mime: format.mime,
|
|
220
|
+
text,
|
|
221
|
+
bytes: bytes.byteLength,
|
|
222
|
+
fingerprint: createHash('sha256').update(bytes).digest('hex'),
|
|
223
|
+
locator: current.href,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
throw new KnowledgeDocumentFailure('redirect_denied');
|
|
227
|
+
}
|
|
228
|
+
catch (cause) {
|
|
229
|
+
if (signal?.aborted === true)
|
|
230
|
+
signal.throwIfAborted();
|
|
231
|
+
if (cause instanceof KnowledgeDocumentFailure)
|
|
232
|
+
throw cause;
|
|
233
|
+
if (timeout.aborted)
|
|
234
|
+
throw new KnowledgeDocumentFailure('timeout');
|
|
235
|
+
throw new KnowledgeDocumentFailure('network');
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
await dispatcher.close().catch(() => undefined);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const extensionFormats = new Map([
|
|
242
|
+
['.txt', { kind: 'text', mime: 'text/plain' }],
|
|
243
|
+
['.md', { kind: 'text', mime: 'text/markdown' }],
|
|
244
|
+
['.markdown', { kind: 'text', mime: 'text/markdown' }],
|
|
245
|
+
['.json', { kind: 'text', mime: 'application/json' }],
|
|
246
|
+
['.yaml', { kind: 'text', mime: 'application/yaml' }],
|
|
247
|
+
['.yml', { kind: 'text', mime: 'application/yaml' }],
|
|
248
|
+
['.csv', { kind: 'text', mime: 'text/csv' }],
|
|
249
|
+
['.ts', { kind: 'text', mime: 'text/plain' }],
|
|
250
|
+
['.tsx', { kind: 'text', mime: 'text/plain' }],
|
|
251
|
+
['.js', { kind: 'text', mime: 'text/plain' }],
|
|
252
|
+
['.jsx', { kind: 'text', mime: 'text/plain' }],
|
|
253
|
+
['.py', { kind: 'text', mime: 'text/plain' }],
|
|
254
|
+
['.rs', { kind: 'text', mime: 'text/plain' }],
|
|
255
|
+
['.go', { kind: 'text', mime: 'text/plain' }],
|
|
256
|
+
['.java', { kind: 'text', mime: 'text/plain' }],
|
|
257
|
+
['.c', { kind: 'text', mime: 'text/plain' }],
|
|
258
|
+
['.h', { kind: 'text', mime: 'text/plain' }],
|
|
259
|
+
['.cpp', { kind: 'text', mime: 'text/plain' }],
|
|
260
|
+
['.pdf', { kind: 'pdf', mime: 'application/pdf' }],
|
|
261
|
+
['.docx', { kind: 'docx', mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }],
|
|
262
|
+
]);
|
|
263
|
+
function fileFormat(path) {
|
|
264
|
+
return extensionFormats.get(extname(path).toLowerCase()) ?? null;
|
|
265
|
+
}
|
|
266
|
+
async function verifyPathIdentity(input, canonical, descriptor) {
|
|
267
|
+
let pathInfo;
|
|
268
|
+
let currentCanonical;
|
|
269
|
+
try {
|
|
270
|
+
[pathInfo, currentCanonical] = await Promise.all([lstat(canonical, { bigint: true }), realpath(input)]);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
274
|
+
}
|
|
275
|
+
if (!pathInfo.isFile() || pathInfo.isSymbolicLink() || !sameIdentity(pathInfo, descriptor)
|
|
276
|
+
|| currentCanonical !== canonical || !pathPolicy.allows(currentCanonical)) {
|
|
277
|
+
throw new KnowledgeDocumentFailure('file_changed');
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function sameIdentity(left, right) {
|
|
281
|
+
return left.dev === right.dev && left.ino === right.ino;
|
|
282
|
+
}
|
|
283
|
+
function responseFormat(url, header) {
|
|
284
|
+
const mime = header?.split(';', 1)[0]?.trim().toLowerCase() ?? '';
|
|
285
|
+
const byMime = {
|
|
286
|
+
'text/plain': { kind: 'text', mime: 'text/plain' },
|
|
287
|
+
'text/markdown': { kind: 'text', mime: 'text/markdown' },
|
|
288
|
+
'text/csv': { kind: 'text', mime: 'text/csv' },
|
|
289
|
+
'text/html': { kind: 'html', mime: 'text/html' },
|
|
290
|
+
'application/xhtml+xml': { kind: 'html', mime: 'text/html' },
|
|
291
|
+
'application/json': { kind: 'text', mime: 'application/json' },
|
|
292
|
+
'application/yaml': { kind: 'text', mime: 'application/yaml' },
|
|
293
|
+
'application/x-yaml': { kind: 'text', mime: 'application/yaml' },
|
|
294
|
+
'application/pdf': { kind: 'pdf', mime: 'application/pdf' },
|
|
295
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': {
|
|
296
|
+
kind: 'docx', mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
const exact = byMime[mime];
|
|
300
|
+
if (exact !== undefined)
|
|
301
|
+
return exact;
|
|
302
|
+
if (mime === '' || mime === 'application/octet-stream') {
|
|
303
|
+
const inferred = fileFormat(url.pathname);
|
|
304
|
+
if (inferred !== null)
|
|
305
|
+
return inferred;
|
|
306
|
+
}
|
|
307
|
+
throw new KnowledgeDocumentFailure('unsupported_mime');
|
|
308
|
+
}
|
|
309
|
+
async function decodeDocument(bytes, kind, signal) {
|
|
310
|
+
signal?.throwIfAborted();
|
|
311
|
+
if (kind === 'pdf' || kind === 'docx')
|
|
312
|
+
return await parseInWorker(kind, bytes, signal);
|
|
313
|
+
let text = decodeUtf8(bytes);
|
|
314
|
+
if (kind === 'html')
|
|
315
|
+
text = htmlToText(text);
|
|
316
|
+
if (text.trim() === '')
|
|
317
|
+
throw new KnowledgeDocumentFailure('empty_text');
|
|
318
|
+
return text;
|
|
319
|
+
}
|
|
320
|
+
function parseInWorker(kind, bytes, signal) {
|
|
321
|
+
return new Promise((resolve, reject) => {
|
|
322
|
+
let worker;
|
|
323
|
+
try {
|
|
324
|
+
worker = new Worker(new URL('./parse-worker.js', import.meta.url), {
|
|
325
|
+
resourceLimits: { maxOldGenerationSizeMb: 128, stackSizeMb: 4 },
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
reject(new KnowledgeDocumentFailure('parse_failed'));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
let settled = false;
|
|
333
|
+
const finish = (outcome) => {
|
|
334
|
+
if (settled)
|
|
335
|
+
return;
|
|
336
|
+
settled = true;
|
|
337
|
+
clearTimeout(timer);
|
|
338
|
+
signal?.removeEventListener('abort', abort);
|
|
339
|
+
void worker.terminate();
|
|
340
|
+
outcome();
|
|
341
|
+
};
|
|
342
|
+
const abort = () => finish(() => {
|
|
343
|
+
try {
|
|
344
|
+
signal?.throwIfAborted();
|
|
345
|
+
}
|
|
346
|
+
catch (cause) {
|
|
347
|
+
reject(cause instanceof Error ? cause : new DOMException('This operation was aborted', 'AbortError'));
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
reject(new DOMException('This operation was aborted', 'AbortError'));
|
|
351
|
+
});
|
|
352
|
+
const timer = setTimeout(() => finish(() => reject(new KnowledgeDocumentFailure('parse_timeout'))), PARSE_TIMEOUT_MS);
|
|
353
|
+
worker.once('message', (reply) => finish(() => {
|
|
354
|
+
if (typeof reply !== 'object' || reply === null || !('ok' in reply) || reply.ok !== true
|
|
355
|
+
|| !('text' in reply) || typeof reply.text !== 'string') {
|
|
356
|
+
reject(new KnowledgeDocumentFailure('parse_failed'));
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (Buffer.byteLength(reply.text, 'utf8') > MAX_TEXT_BYTES || reply.text.trim() === ''
|
|
360
|
+
|| containsBinaryControls(reply.text)) {
|
|
361
|
+
reject(new KnowledgeDocumentFailure('invalid_text'));
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
resolve(reply.text);
|
|
365
|
+
}));
|
|
366
|
+
worker.once('error', () => finish(() => reject(new KnowledgeDocumentFailure('parse_failed'))));
|
|
367
|
+
worker.once('exit', code => {
|
|
368
|
+
if (code !== 0)
|
|
369
|
+
finish(() => reject(new KnowledgeDocumentFailure('parse_failed')));
|
|
370
|
+
});
|
|
371
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
372
|
+
worker.postMessage({ kind, bytes });
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
async function readResponseBounded(response, signal) {
|
|
376
|
+
const bytes = await readBoundedResponse(response, { limit: MAX_FILE_BYTES, signal, failure: () => new KnowledgeDocumentFailure('file_too_large') });
|
|
377
|
+
if (!bytes.length)
|
|
378
|
+
throw new KnowledgeDocumentFailure('empty_text');
|
|
379
|
+
return bytes;
|
|
380
|
+
}
|
|
381
|
+
function admittedUrl(value) {
|
|
382
|
+
if (typeof value !== 'string' || value.length > 2_048)
|
|
383
|
+
throw new KnowledgeDocumentFailure('url_denied');
|
|
384
|
+
const scrubbed = contentPolicy.scrub('url', value);
|
|
385
|
+
if (scrubbed.kind !== 'clean')
|
|
386
|
+
throw new KnowledgeDocumentFailure('url_denied');
|
|
387
|
+
let url;
|
|
388
|
+
try {
|
|
389
|
+
url = new URL(value);
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
throw new KnowledgeDocumentFailure('url_denied');
|
|
393
|
+
}
|
|
394
|
+
if ((url.protocol !== 'http:' && url.protocol !== 'https:') || url.username !== '' || url.password !== ''
|
|
395
|
+
|| url.hash !== '')
|
|
396
|
+
throw new KnowledgeDocumentFailure('url_denied');
|
|
397
|
+
const address = url.hostname.startsWith('[') && url.hostname.endsWith(']')
|
|
398
|
+
? url.hostname.slice(1, -1)
|
|
399
|
+
: url.hostname;
|
|
400
|
+
if (isIP(address) !== 0 && !isPublicAddress(address)) {
|
|
401
|
+
throw new KnowledgeDocumentFailure('url_denied');
|
|
402
|
+
}
|
|
403
|
+
return url;
|
|
404
|
+
}
|
|
405
|
+
const deniedAddresses = new BlockList();
|
|
406
|
+
for (const [network, prefix, family] of [
|
|
407
|
+
['0.0.0.0', 8, 'ipv4'], ['10.0.0.0', 8, 'ipv4'], ['100.64.0.0', 10, 'ipv4'],
|
|
408
|
+
['127.0.0.0', 8, 'ipv4'], ['169.254.0.0', 16, 'ipv4'], ['172.16.0.0', 12, 'ipv4'],
|
|
409
|
+
['192.0.0.0', 24, 'ipv4'], ['192.0.2.0', 24, 'ipv4'], ['192.168.0.0', 16, 'ipv4'],
|
|
410
|
+
['198.18.0.0', 15, 'ipv4'], ['198.51.100.0', 24, 'ipv4'], ['203.0.113.0', 24, 'ipv4'],
|
|
411
|
+
['224.0.0.0', 4, 'ipv4'], ['240.0.0.0', 4, 'ipv4'],
|
|
412
|
+
['::', 96, 'ipv6'], ['::1', 128, 'ipv6'], ['fc00::', 7, 'ipv6'], ['fe80::', 10, 'ipv6'],
|
|
413
|
+
['ff00::', 8, 'ipv6'], ['2001:db8::', 32, 'ipv6'],
|
|
414
|
+
['::ffff:0:0', 96, 'ipv6'], ['64:ff9b::', 96, 'ipv6'], ['64:ff9b:1::', 48, 'ipv6'],
|
|
415
|
+
['fec0::', 10, 'ipv6'], ['2001::', 23, 'ipv6'], ['2002::', 16, 'ipv6'],
|
|
416
|
+
])
|
|
417
|
+
deniedAddresses.addSubnet(network, prefix, family);
|
|
418
|
+
function isPublicAddress(address) {
|
|
419
|
+
const family = isIP(address);
|
|
420
|
+
if (family === 0)
|
|
421
|
+
return false;
|
|
422
|
+
if (family === 6) {
|
|
423
|
+
const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/iu.exec(address)?.[1];
|
|
424
|
+
if (mapped !== undefined)
|
|
425
|
+
return isPublicAddress(mapped);
|
|
426
|
+
}
|
|
427
|
+
return !deniedAddresses.check(address, family === 4 ? 'ipv4' : 'ipv6');
|
|
428
|
+
}
|
|
429
|
+
const publicLookup = (hostname, options, callback) => {
|
|
430
|
+
void dnsLookup(hostname, { ...options, all: true, verbatim: true }).then(addresses => {
|
|
431
|
+
if (addresses.length === 0 || addresses.some(address => !isPublicAddress(address.address))) {
|
|
432
|
+
callback(Object.assign(new Error('address denied'), { code: 'ENOTFOUND' }), '');
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (options.all === true)
|
|
436
|
+
callback(null, addresses);
|
|
437
|
+
else
|
|
438
|
+
callback(null, addresses[0]?.address ?? '', addresses[0]?.family);
|
|
439
|
+
}, () => callback(Object.assign(new Error('lookup failed'), { code: 'ENOTFOUND' }), ''));
|
|
440
|
+
};
|
|
441
|
+
function htmlToText(html) {
|
|
442
|
+
return decodeEntities(html
|
|
443
|
+
.replace(/<(?:script|style|noscript)\b[^>]*>[\s\S]*?<\/(?:script|style|noscript)>/giu, ' ')
|
|
444
|
+
.replace(/<!--[^]*?-->/gu, ' ')
|
|
445
|
+
.replace(/<[^>]+>/gu, ' '))
|
|
446
|
+
.replace(/[ \t\f\v]+/gu, ' ')
|
|
447
|
+
.replace(/\s*\n\s*/gu, '\n')
|
|
448
|
+
.trim();
|
|
449
|
+
}
|
|
450
|
+
function decodeUtf8(bytes) {
|
|
451
|
+
let text;
|
|
452
|
+
try {
|
|
453
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
throw new KnowledgeDocumentFailure('invalid_text');
|
|
457
|
+
}
|
|
458
|
+
if (Buffer.byteLength(text, 'utf8') > MAX_TEXT_BYTES || containsBinaryControls(text)) {
|
|
459
|
+
throw new KnowledgeDocumentFailure('invalid_text');
|
|
460
|
+
}
|
|
461
|
+
return text;
|
|
462
|
+
}
|
|
463
|
+
function extractHtmlTitle(html) {
|
|
464
|
+
const match = /<title\b[^>]*>([\s\S]*?)<\/title>/iu.exec(html);
|
|
465
|
+
if (match?.[1] === undefined)
|
|
466
|
+
return null;
|
|
467
|
+
const title = decodeEntities(match[1].replace(/<[^>]+>/gu, ' ')).replace(/\s+/gu, ' ').trim();
|
|
468
|
+
return title === '' ? null : title.slice(0, 300);
|
|
469
|
+
}
|
|
470
|
+
function decodeEntities(value) {
|
|
471
|
+
const named = {
|
|
472
|
+
amp: '&', apos: "'", gt: '>', lt: '<', nbsp: ' ', quot: '"',
|
|
473
|
+
};
|
|
474
|
+
return value.replace(/&(?:#(x[0-9a-f]+|\d+)|([a-z]+));/giu, (match, numeric, name) => {
|
|
475
|
+
if (numeric !== undefined) {
|
|
476
|
+
const codePoint = Number.parseInt(numeric.startsWith('x') || numeric.startsWith('X') ? numeric.slice(1) : numeric, numeric.startsWith('x') || numeric.startsWith('X') ? 16 : 10);
|
|
477
|
+
return Number.isSafeInteger(codePoint) && codePoint > 0 && codePoint <= 0x10ffff
|
|
478
|
+
&& (codePoint < 0xd800 || codePoint > 0xdfff) ? String.fromCodePoint(codePoint) : '\ufffd';
|
|
479
|
+
}
|
|
480
|
+
return named[name?.toLowerCase() ?? ''] ?? match;
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
function screenText(field, value) {
|
|
484
|
+
if (contentPolicy.scrub(field, value).kind !== 'clean' || pathPolicy.scrubText(field, value).kind !== 'clean') {
|
|
485
|
+
throw new KnowledgeDocumentFailure('sensitive_content');
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
function containsBinaryControls(value) {
|
|
489
|
+
return /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/u.test(value);
|
|
490
|
+
}
|
|
491
|
+
function codePointsLength(value) {
|
|
492
|
+
return [...value].length;
|
|
493
|
+
}
|
|
494
|
+
/** Bounded extract from screened document prose; never infer facts from a filename. */
|
|
495
|
+
export function knowledgeExcerpt(text) {
|
|
496
|
+
let fenced = false;
|
|
497
|
+
const lines = [];
|
|
498
|
+
for (const raw of text.split('\n')) {
|
|
499
|
+
const line = raw.trim();
|
|
500
|
+
if (line.startsWith('```') || line.startsWith('~~~')) {
|
|
501
|
+
fenced = !fenced;
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
if (fenced || !line || /^(?:#|<|\||!\[|\[!\[|---|===)/u.test(line))
|
|
505
|
+
continue;
|
|
506
|
+
const plain = line.replace(/!\[[^\]]*\]\([^)]*\)/gu, '').replace(/\[([^\]]+)\]\([^)]*\)/gu, '$1').replace(/[*`_]/gu, '').trim();
|
|
507
|
+
if (plain)
|
|
508
|
+
lines.push(plain);
|
|
509
|
+
if ([...lines.join(' ')].length >= 450)
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
let excerpt = '';
|
|
513
|
+
for (const character of lines.join(' ')) {
|
|
514
|
+
if (excerpt.length + character.length > 450)
|
|
515
|
+
break;
|
|
516
|
+
excerpt += character;
|
|
517
|
+
}
|
|
518
|
+
return excerpt;
|
|
519
|
+
}
|
|
520
|
+
//# sourceMappingURL=documents.js.map
|