@citeark/agent 0.3.20
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 +202 -0
- package/README.md +128 -0
- package/data/dataset-source-registry.v1.json +300 -0
- package/dist/arkgraph/boot.js +6 -0
- package/dist/arkgraph/index.html +1 -0
- package/dist/arkgraph/viewer.css +1 -0
- package/dist/arkgraph/viewer.en.css +1 -0
- package/dist/arkgraph/viewer.en.js +49 -0
- package/dist/arkgraph/viewer.en.js.LEGAL.txt +56 -0
- package/dist/arkgraph/viewer.js +49 -0
- package/dist/arkgraph/viewer.js.LEGAL.txt +56 -0
- package/docker/claude-code/Dockerfile +97 -0
- package/docker/claude-code/codex-pro-relay.mjs +466 -0
- package/docker/claude-code/runtime-contract-check.mjs +79 -0
- package/docs/arkgraph-reading.md +79 -0
- package/docs/configuration.md +100 -0
- package/docs/integration.md +92 -0
- package/docs/maturity-plan.md +27 -0
- package/docs/npm-release.md +44 -0
- package/docs/paper-reading.md +40 -0
- package/docs/research-plan-granularity.md +27 -0
- package/docs/terminal.md +49 -0
- package/examples/toy-evaluation/compile-task.json +27 -0
- package/examples/toy-evaluation/paper.md +5 -0
- package/examples/toy-evaluation/repository/README.md +9 -0
- package/examples/toy-evaluation/repository/checkpoint.json +4 -0
- package/examples/toy-evaluation/repository/evaluate.py +17 -0
- package/examples/toy-evaluation/task.json +81 -0
- package/package.json +59 -0
- package/prompts/compile-research.md +58 -0
- package/prompts/execute-contract.md +72 -0
- package/prompts/execute-workspace-simple.md +51 -0
- package/prompts/execute-workspace.md +34 -0
- package/prompts/prepare-reproduction.md +82 -0
- package/prompts/repair-research.md +45 -0
- package/protocol/CAP.md +129 -0
- package/protocol/LICENSE +12 -0
- package/protocol/MAPPINGS.md +72 -0
- package/protocol/README.md +38 -0
- package/protocol/conformance-v2.0-alpha.1.json +36 -0
- package/protocol/examples/arkgraph/checkpoint-evaluation.json +309 -0
- package/protocol/examples/arkgraph/fixtures.mjs +49 -0
- package/protocol/examples/arkgraph/paper-free.json +291 -0
- package/protocol/examples/arkgraph/partial-failure.json +344 -0
- package/protocol/examples/arkgraph/training-evaluation.json +443 -0
- package/protocol/profiles/agent-trace.md +16 -0
- package/protocol/profiles/computational-run.md +16 -0
- package/protocol/profiles/core.md +15 -0
- package/protocol/profiles/public-bundle.md +18 -0
- package/protocol/profiles/reproduction.md +29 -0
- package/protocol/profiles/research-compilation.md +44 -0
- package/protocol/profiles/research-plan.md +39 -0
- package/protocol/profiles/restricted-evidence.md +15 -0
- package/runtime/bootstrap-autodl-runtime.sh +314 -0
- package/runtime/create-runtime-venv.sh +41 -0
- package/runtime/install-local-cpu-runtime.sh +23 -0
- package/runtime/install-scientific-runtime.sh +153 -0
- package/runtime/mineru/parse.py +62 -0
- package/runtime/mineru/requirements.txt +4 -0
- package/runtime/requirements-baseline.txt +38 -0
- package/schemas/cap/v2/activity.schema.json +47 -0
- package/schemas/cap/v2/agent.schema.json +32 -0
- package/schemas/cap/v2/assertion.schema.json +110 -0
- package/schemas/cap/v2/descriptor.schema.json +243 -0
- package/schemas/cap/v2/entity.schema.json +64 -0
- package/schemas/cap/v2/manifest.schema.json +67 -0
- package/schemas/cap/v2/relation.schema.json +82 -0
- package/schemas/compute-catalog.schema.json +63 -0
- package/schemas/compute-decision.schema.json +27 -0
- package/schemas/execution-contract.schema.json +1024 -0
- package/schemas/research-card.schema.json +30 -0
- package/schemas/research-inventory-draft.schema.json +366 -0
- package/schemas/research.schema.json +1044 -0
- package/schemas/result.schema.json +173 -0
- package/schemas/verification-policy.schema.json +47 -0
- package/schemas/verified-conclusion.schema.json +58 -0
- package/schemas/workspace-summary.schema.json +24 -0
- package/scripts/build-arkgraph-view.mjs +12 -0
- package/scripts/check-execution-feasibility.mjs +24 -0
- package/scripts/check-syntax.mjs +15 -0
- package/scripts/deterministic-asset-preparation.py +438 -0
- package/scripts/package-cap.mjs +23 -0
- package/scripts/package-local-agent.mjs +23 -0
- package/scripts/preview-arkgraph.mjs +25 -0
- package/scripts/replay-research-compiler-candidate.mjs +134 -0
- package/scripts/review-compiler-sources.mjs +44 -0
- package/scripts/run-asset-preparation.sh +17 -0
- package/scripts/run-research-plan.mjs +98 -0
- package/scripts/validate-asset-preparation.py +290 -0
- package/scripts/verify-local-runtime.mjs +57 -0
- package/scripts/verify-npm-package.mjs +57 -0
- package/src/adapters/paper2agent.mjs +107 -0
- package/src/assets/cache.mjs +159 -0
- package/src/assets/compute.mjs +98 -0
- package/src/assets/executor.mjs +145 -0
- package/src/assets/lifecycle.mjs +213 -0
- package/src/assets/manifest.mjs +242 -0
- package/src/assets/opportunistic-preparation.mjs +81 -0
- package/src/assets/plan.mjs +411 -0
- package/src/assets/prompts.mjs +29 -0
- package/src/assets/public-asset-probe.mjs +525 -0
- package/src/assets/qualification.mjs +119 -0
- package/src/assets/readiness.mjs +130 -0
- package/src/assets/reproduction-admission.mjs +355 -0
- package/src/assets/requirements.mjs +152 -0
- package/src/assets/source-grounding.mjs +341 -0
- package/src/assets/source-policy.mjs +118 -0
- package/src/autodl/client.mjs +260 -0
- package/src/autodl/ssh.mjs +380 -0
- package/src/autodl/tools.mjs +129 -0
- package/src/cap/redaction.mjs +38 -0
- package/src/cap/v2/archive.mjs +152 -0
- package/src/cap/v2/attestation.mjs +204 -0
- package/src/cap/v2/canonical-json.mjs +114 -0
- package/src/cap/v2/compilation-artifact.mjs +240 -0
- package/src/cap/v2/core.mjs +282 -0
- package/src/cap/v2/measurement-assessment-records.mjs +23 -0
- package/src/cap/v2/pipeline-artifact.mjs +922 -0
- package/src/cap/v2/read.mjs +41 -0
- package/src/cap/v2/reassessment-artifact.mjs +383 -0
- package/src/cap/v2/research-artifact.mjs +231 -0
- package/src/cap/v2/research-map-records.mjs +46 -0
- package/src/cap/v2/research-object-records.mjs +163 -0
- package/src/cap/v2/research-records.mjs +187 -0
- package/src/cap/v2/verify.mjs +642 -0
- package/src/cli.mjs +1146 -0
- package/src/compute/autodl-pro-compiler.mjs +347 -0
- package/src/compute/autodl-pro-executor.mjs +459 -0
- package/src/compute/autodl-pro-job.mjs +843 -0
- package/src/compute/autodl-pro-network.mjs +295 -0
- package/src/compute/autodl-pro-remote.mjs +810 -0
- package/src/compute/autodl-pro-staging.mjs +117 -0
- package/src/compute/campaign.mjs +110 -0
- package/src/compute/catalog.mjs +123 -0
- package/src/compute/checkpoint-protocol.mjs +154 -0
- package/src/compute/codex-account-lock.mjs +111 -0
- package/src/compute/codex-account-session.mjs +107 -0
- package/src/compute/compiler-profile.mjs +38 -0
- package/src/compute/compiler-router.mjs +23 -0
- package/src/compute/coordinator-recovery.mjs +210 -0
- package/src/compute/executor-router.mjs +29 -0
- package/src/compute/gcp-batch-compiler.mjs +685 -0
- package/src/compute/gcp-batch-executor.mjs +1215 -0
- package/src/compute/gcp-batch-failure.mjs +92 -0
- package/src/compute/gcp-batch-job.mjs +527 -0
- package/src/compute/gcp-batch-lifecycle.mjs +81 -0
- package/src/compute/gcp-checkpoint-worker.mjs +1633 -0
- package/src/compute/local-codex-compiler.mjs +52 -0
- package/src/compute/measurement-hardware.mjs +128 -0
- package/src/compute/remote-attempt.mjs +226 -0
- package/src/compute/requirements.mjs +124 -0
- package/src/compute/research-phases.mjs +48 -0
- package/src/compute/scheduler.mjs +452 -0
- package/src/compute/shared-workloads.mjs +26 -0
- package/src/compute/stage-archive.mjs +79 -0
- package/src/contracts/campaign-contract.mjs +52 -0
- package/src/contracts/execution-contract.mjs +819 -0
- package/src/contracts/execution-mode.mjs +19 -0
- package/src/contracts/execution-timeouts.mjs +45 -0
- package/src/contracts/execution-workload.mjs +68 -0
- package/src/contracts/preflight-schema.mjs +25 -0
- package/src/contracts/public-contract.mjs +63 -0
- package/src/contracts/subject-tags.mjs +31 -0
- package/src/dashboard/data.mjs +898 -0
- package/src/dashboard/server.mjs +79 -0
- package/src/dashboard/static/dashboard.css +366 -0
- package/src/dashboard/static/dashboard.js +560 -0
- package/src/dashboard/static/index.html +85 -0
- package/src/deployment/community-policy.mjs +9 -0
- package/src/deployment/environment.mjs +112 -0
- package/src/deployment/guided.mjs +98 -0
- package/src/deployment/handoff.mjs +102 -0
- package/src/deployment/local-contract.mjs +31 -0
- package/src/deployment/local.mjs +100 -0
- package/src/deployment/prepare.mjs +46 -0
- package/src/deployment/recipe.mjs +108 -0
- package/src/deployment/supplement.mjs +51 -0
- package/src/deployment/terminal.mjs +43 -0
- package/src/diagnosis/renderer.mjs +75 -0
- package/src/diagnosis/target-failure.mjs +46 -0
- package/src/evidence/parser-registry.mjs +54 -0
- package/src/evidence/parsers/fasttext-classification.mjs +82 -0
- package/src/evidence/parsers/json-scalar.mjs +96 -0
- package/src/evidence/parsers/simcse-senteval.mjs +104 -0
- package/src/evidence/parsers/starspace-classification.mjs +78 -0
- package/src/evidence/registry.mjs +147 -0
- package/src/execution/runner-audit.mjs +473 -0
- package/src/gcp/auth.mjs +106 -0
- package/src/gcp/batch-client.mjs +120 -0
- package/src/gcp/resource-discovery.mjs +177 -0
- package/src/gcp/rest.mjs +82 -0
- package/src/gcp/secret-manager.mjs +34 -0
- package/src/gcp/signed-url.mjs +133 -0
- package/src/gcp/storage.mjs +220 -0
- package/src/graph/command.mjs +41 -0
- package/src/graph/execution.mjs +97 -0
- package/src/graph/model.mjs +37 -0
- package/src/graph/presentation.mjs +110 -0
- package/src/graph/query.mjs +159 -0
- package/src/graph/research-relations.mjs +69 -0
- package/src/graph/source-page.mjs +12 -0
- package/src/graph/source-preview.mjs +34 -0
- package/src/graph/validate.mjs +76 -0
- package/src/job.mjs +496 -0
- package/src/network/autodl-routing-proxy.mjs +462 -0
- package/src/network/egress-proxy.mjs +158 -0
- package/src/observability/event-contract.mjs +230 -0
- package/src/observability/pipeline-monitor.mjs +166 -0
- package/src/pipeline/orchestrator.mjs +1281 -0
- package/src/pipeline/recovery-error.mjs +11 -0
- package/src/pipeline/replay.mjs +304 -0
- package/src/pipeline/shared-execution.mjs +115 -0
- package/src/pipeline/stage-checkpoint.mjs +86 -0
- package/src/pipeline/stage-recovery.mjs +101 -0
- package/src/pipeline/targets.mjs +110 -0
- package/src/process.mjs +143 -0
- package/src/protocol.mjs +312 -0
- package/src/provider/codex-account.mjs +44 -0
- package/src/provider/codex-completion.mjs +49 -0
- package/src/provider/completion.mjs +292 -0
- package/src/provider/model-client.mjs +44 -0
- package/src/provider/model-route.mjs +29 -0
- package/src/provider/openrouter-readiness.mjs +189 -0
- package/src/provider/reader-bridge.mjs +35 -0
- package/src/provider/relay.mjs +263 -0
- package/src/provider/runtime-auth.mjs +40 -0
- package/src/public/cap.d.mts +90 -0
- package/src/public/cap.mjs +12 -0
- package/src/public/contracts.d.mts +2 -0
- package/src/public/host.mjs +171 -0
- package/src/public/operations.d.mts +11 -0
- package/src/public/presentation.d.mts +4 -0
- package/src/records/views.mjs +26 -0
- package/src/remote/command.mjs +178 -0
- package/src/remote/ssh.mjs +59 -0
- package/src/repository-origin.mjs +81 -0
- package/src/reproduction/evidence-feedback.mjs +96 -0
- package/src/reproduction/incomplete-initialization.mjs +25 -0
- package/src/reproduction/lifecycle.mjs +253 -0
- package/src/reproduction/plan.mjs +132 -0
- package/src/reproduction/prompts.mjs +70 -0
- package/src/reproduction/runner.mjs +188 -0
- package/src/reproduction/summary.mjs +130 -0
- package/src/reproduction/workspace-mode.mjs +7 -0
- package/src/research/automatic-admission.mjs +156 -0
- package/src/research/compiler-coverage.mjs +85 -0
- package/src/research/compiler-failure.mjs +24 -0
- package/src/research/compiler-normalization-guards.mjs +112 -0
- package/src/research/compiler-repair.mjs +3 -0
- package/src/research/compiler.mjs +853 -0
- package/src/research/continuation-selection.mjs +26 -0
- package/src/research/execution-graph-context.mjs +43 -0
- package/src/research/experiment-importance.mjs +15 -0
- package/src/research/inventory-handoff.mjs +104 -0
- package/src/research/inventory-revisions.mjs +32 -0
- package/src/research/mineru-local.mjs +73 -0
- package/src/research/paper-command.mjs +19 -0
- package/src/research/paper-markdown.mjs +180 -0
- package/src/research/paper-source-map.mjs +69 -0
- package/src/research/planning-policy.mjs +88 -0
- package/src/research/reference-materials.mjs +11 -0
- package/src/research/reproduction-scope.mjs +30 -0
- package/src/research/research-map.mjs +94 -0
- package/src/research/research-objects.mjs +88 -0
- package/src/research/source-discovery.mjs +646 -0
- package/src/research/source-observations.mjs +75 -0
- package/src/research/source-review-cli-mcp.mjs +26 -0
- package/src/research/source-review-input.mjs +209 -0
- package/src/research/source-review-local-codex.mjs +36 -0
- package/src/research/source-review-model.mjs +70 -0
- package/src/research/source-review.mjs +173 -0
- package/src/research/structure.mjs +3163 -0
- package/src/research-card/renderer.mjs +277 -0
- package/src/research-card/verified-conclusion.mjs +143 -0
- package/src/results/output-registry.mjs +183 -0
- package/src/runtime/claude-code.mjs +52 -0
- package/src/runtime/codex-capacity-retry.mjs +87 -0
- package/src/runtime/codex.mjs +64 -0
- package/src/runtime/config.mjs +157 -0
- package/src/runtime/final-output.mjs +40 -0
- package/src/runtime/index.mjs +21 -0
- package/src/runtime/local-codex.mjs +74 -0
- package/src/runtime/opencode.mjs +95 -0
- package/src/runtime/prompt.mjs +13 -0
- package/src/sandbox/docker.mjs +363 -0
- package/src/settings/command.mjs +297 -0
- package/src/settings/store.mjs +119 -0
- package/src/telemetry/pricing.mjs +68 -0
- package/src/telemetry/usage.mjs +265 -0
- package/src/terminal/events.mjs +97 -0
- package/src/terminal/input.mjs +40 -0
- package/src/terminal/plain.mjs +40 -0
- package/src/terminal/remote-stream.mjs +22 -0
- package/src/terminal/screen.mjs +214 -0
- package/src/terminal/transcript.mjs +69 -0
- package/src/util.mjs +107 -0
- package/src/verification/ai-assessor.mjs +534 -0
- package/src/verification/claim-evaluator.mjs +242 -0
- package/src/verification/evidence-context.mjs +165 -0
- package/src/verification/evidence-reader.mjs +95 -0
- package/src/verification/integrity.mjs +570 -0
- package/src/verification/tolerance.mjs +32 -0
- package/src/workloads/cpu-research-preparation.mjs +56 -0
- package/src/workloads/definition.mjs +74 -0
- package/src/workloads/phase-aware-reproduction.mjs +46 -0
- package/src/workloads/reproduction.mjs +85 -0
- package/src/workspace/command.mjs +242 -0
- package/src/workspace/control.mjs +49 -0
- package/src/workspace/entry.mjs +28 -0
- package/src/workspace/input.mjs +93 -0
- package/src/workspace/interactive.mjs +94 -0
- package/src/workspace/jobs.mjs +418 -0
- package/src/workspace/session.mjs +97 -0
- package/src/workspace/worker.mjs +137 -0
- package/ui/arkgraph/ambient-motion.mjs +10 -0
- package/ui/arkgraph/app.jsx +153 -0
- package/ui/arkgraph/boot.js +6 -0
- package/ui/arkgraph/camera-motion.mjs +20 -0
- package/ui/arkgraph/context-reveal.mjs +39 -0
- package/ui/arkgraph/details.css +3 -0
- package/ui/arkgraph/entry.jsx +28 -0
- package/ui/arkgraph/experiment-curves.mjs +17 -0
- package/ui/arkgraph/experiment-selection.mjs +15 -0
- package/ui/arkgraph/experiment-style.css +26 -0
- package/ui/arkgraph/experiment-ui.jsx +32 -0
- package/ui/arkgraph/frame.html +1 -0
- package/ui/arkgraph/graph-gestures.mjs +62 -0
- package/ui/arkgraph/label-layout.mjs +57 -0
- package/ui/arkgraph/locales/en.json +229 -0
- package/ui/arkgraph/locales/source-types.json +15 -0
- package/ui/arkgraph/localization-build.mjs +27 -0
- package/ui/arkgraph/material-build.mjs +23 -0
- package/ui/arkgraph/material-colors.mjs +39 -0
- package/ui/arkgraph/material-style.css +15 -0
- package/ui/arkgraph/open-graph.jsx +326 -0
- package/ui/arkgraph/outline.jsx +49 -0
- package/ui/arkgraph/package-lock.json +888 -0
- package/ui/arkgraph/package.json +17 -0
- package/ui/arkgraph/reading-layout.mjs +130 -0
- package/ui/arkgraph/reading-presentation.mjs +73 -0
- package/ui/arkgraph/record-detail.css +51 -0
- package/ui/arkgraph/record-details.jsx +29 -0
- package/ui/arkgraph/research-types.mjs +31 -0
- package/ui/arkgraph/selection-mark.jsx +6 -0
- package/ui/arkgraph/soft-spine.mjs +26 -0
- package/ui/arkgraph/steering-style.css +187 -0
- package/ui/arkgraph/style.css +272 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { terminalKeys } from './input.mjs';
|
|
2
|
+
import { createWriteStream } from 'node:fs';
|
|
3
|
+
import { finished } from 'node:stream/promises';
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import { format } from 'node:util';
|
|
6
|
+
import { STAGES, Transcript, wrapText, cleanText } from './transcript.mjs';
|
|
7
|
+
|
|
8
|
+
const ESC = '\x1b[';
|
|
9
|
+
const muted = text => `${ESC}2m${text}${ESC}0m`;
|
|
10
|
+
const bold = text => `${ESC}1m${text}${ESC}0m`;
|
|
11
|
+
|
|
12
|
+
export class TerminalScreen {
|
|
13
|
+
constructor({ input = process.stdin, output = process.stdout, version = '', color = !process.env.NO_COLOR, onExit, onInput } = {}) {
|
|
14
|
+
this.input = input; this.output = output; this.version = version; this.color = color;
|
|
15
|
+
this.transcript = new Transcript(); this.expanded = false; this.pending = null;
|
|
16
|
+
this.startedAt = Date.now(); this.context = ''; this.buffer = ''; this.cursor = 0;
|
|
17
|
+
this.rawWrite = output.write.bind(output);
|
|
18
|
+
this.onExit = onExit; this.onInput = onInput;
|
|
19
|
+
this.onKey = this.onKey.bind(this); this.render = this.render.bind(this);
|
|
20
|
+
}
|
|
21
|
+
start() {
|
|
22
|
+
this.wasRaw = this.input.isRaw;
|
|
23
|
+
this.releaseKeys = terminalKeys(this.input, this.onKey);
|
|
24
|
+
this.input.setRawMode(true); this.input.resume();
|
|
25
|
+
this.output.on('resize', this.render);
|
|
26
|
+
this.rawWrite(`${ESC}?1049h${ESC}?25l${ESC}?1000h${ESC}?1006h${ESC}?2004h`);
|
|
27
|
+
this.savedLog = console.log; this.savedError = console.error; this.savedWarn = console.warn;
|
|
28
|
+
console.log = (...args) => this.event({ kind: 'status', text: format(...args) });
|
|
29
|
+
console.error = console.warn = (...args) => this.event({ kind: 'error', text: format(...args) });
|
|
30
|
+
this.timer = setInterval(this.render, 250); this.timer.unref(); this.render();
|
|
31
|
+
}
|
|
32
|
+
async attach(filename) {
|
|
33
|
+
const initial = [...this.transcript.entries];
|
|
34
|
+
let previous;
|
|
35
|
+
try { previous = await readFile(filename, 'utf8'); } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
36
|
+
if (previous) {
|
|
37
|
+
const fresh = this.transcript.entries;
|
|
38
|
+
this.transcript = new Transcript();
|
|
39
|
+
for (const line of previous.split('\n').filter(Boolean)) this.transcript.add(JSON.parse(line));
|
|
40
|
+
this.transcript.add({ kind: 'status', text: 'Session resumed' });
|
|
41
|
+
for (const event of fresh) this.transcript.add(event);
|
|
42
|
+
this.cached = null;
|
|
43
|
+
}
|
|
44
|
+
this.file = createWriteStream(filename, { flags: 'a', mode: 0o600 });
|
|
45
|
+
this.file.on('error', error => { this.fileError = error; });
|
|
46
|
+
for (const event of initial) this.file.write(`${JSON.stringify(event)}\n`);
|
|
47
|
+
this.event({ kind: 'status', text: `Transcript: ${filename}` });
|
|
48
|
+
}
|
|
49
|
+
async stopRecording() {
|
|
50
|
+
if (!this.file) return;
|
|
51
|
+
const file = this.file; this.file = null; file.end(); await finished(file);
|
|
52
|
+
}
|
|
53
|
+
async view(filename) {
|
|
54
|
+
const saved = await readFile(filename, 'utf8');
|
|
55
|
+
for (const line of saved.split('\n').filter(Boolean)) this.transcript.add(JSON.parse(line));
|
|
56
|
+
this.context = filename;
|
|
57
|
+
await this.finish('Saved conversation');
|
|
58
|
+
}
|
|
59
|
+
event(event) {
|
|
60
|
+
const value = { at: new Date().toISOString(), ...event };
|
|
61
|
+
this.transcript.add(value);
|
|
62
|
+
if (this.file && !this.fileError) this.file.write(`${JSON.stringify(value)}\n`);
|
|
63
|
+
this.dirty = true;
|
|
64
|
+
}
|
|
65
|
+
stage(stage, status = 'Running') { this.event({ kind: 'stage', stage, status }); }
|
|
66
|
+
async openResearch(options) {
|
|
67
|
+
this.event({ kind: 'assistant', text: 'Start with a paper, or continue a saved workspace.\nEnter a paper URL, file path, or CiteArk paper ID.\n\n/resume Continue a workspace\n/cap Open a research artifact\n/model Choose a model\n/compute Choose compute\n/help Show commands' });
|
|
68
|
+
while (true) {
|
|
69
|
+
const value = await this.ask('What would you like to research?');
|
|
70
|
+
if (!value) continue;
|
|
71
|
+
if (value === '/model' || value === '/compute') {
|
|
72
|
+
const { settingsMenu } = await import('../settings/command.mjs');
|
|
73
|
+
await settingsMenu(value.slice(1), this); continue;
|
|
74
|
+
}
|
|
75
|
+
if (value === '/help') {
|
|
76
|
+
this.event({ kind: 'status', text: '/resume · /cap · /model · /compute · /help\nAdd --repository, --guide, or --goal when starting from your shell.\nModel and compute settings are requested when needed.' });
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (value === '/resume') {
|
|
80
|
+
options['--resume'] = true;
|
|
81
|
+
options['--work-dir'] = await this.ask('Workspace directory');
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
if (value === '/cap' || value.endsWith('.cap')) {
|
|
85
|
+
options['--cap'] = value === '/cap' ? await this.ask('CAP file path') : value;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (value.startsWith('/')) {
|
|
89
|
+
// Absolute file paths are also valid inputs.
|
|
90
|
+
if (!value.includes('.', 1) && !value.slice(1).includes('/')) {
|
|
91
|
+
this.event({ kind: 'error', text: `Unknown command: ${value}. Use /help for available commands.` });
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (/^[a-z0-9]{6}$/.test(value)) return value;
|
|
96
|
+
options['--paper'] = value;
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async ask(label, { defaultValue = '', secret = false, choices } = {}) {
|
|
101
|
+
this.transcript.follow(); this.buffer = defaultValue; this.cursor = this.buffer.length;
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
this.pending = { label, secret, choices, selected: Math.max(0, choices?.findIndex(item => item.value === defaultValue) ?? 0), resolve, reject };
|
|
104
|
+
this.render();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
choose(label, choices, defaultValue = choices[0]?.value) { return this.ask(label, { choices, defaultValue }); }
|
|
108
|
+
async confirm(label) {
|
|
109
|
+
return await this.choose(label, [{ value: 'yes', label: 'Continue' }, { value: 'no', label: 'Cancel' }], 'no') === 'yes';
|
|
110
|
+
}
|
|
111
|
+
onKey(text, key = {}) {
|
|
112
|
+
const page = Math.max(1, this.bodyHeight ?? 10);
|
|
113
|
+
const scroll = amount => { this.transcript.scroll(amount, this.lines?.length ?? 0, page); this.render(); };
|
|
114
|
+
const mouse = /^\x1b\[<(64|65);\d+;\d+[Mm]$/.exec(key.sequence ?? text ?? '');
|
|
115
|
+
if (mouse) { scroll(mouse[1] === '64' ? -3 : 3); return; }
|
|
116
|
+
if (key.name === 'pageup') { scroll(-page); return; }
|
|
117
|
+
if (key.name === 'pagedown') { scroll(page); return; }
|
|
118
|
+
if (key.name === 'end' && (!this.pending || this.transcript.offset !== null)) { this.transcript.follow(); this.render(); return; }
|
|
119
|
+
if (key.name === 'home' && !this.pending) { this.transcript.offset = 0; this.render(); return; }
|
|
120
|
+
if (key.ctrl && key.name === 'o') { this.expanded = !this.expanded; this.cached = null; this.render(); return; }
|
|
121
|
+
if (key.ctrl && key.name === 'c') {
|
|
122
|
+
if (this.pending) {
|
|
123
|
+
const pending = this.pending; this.pending = null;
|
|
124
|
+
pending.reject(Object.assign(Error('Operation cancelled.'), { exitCode: 130 }));
|
|
125
|
+
} else {
|
|
126
|
+
this.transcript.status = 'Disconnecting…';
|
|
127
|
+
this.onExit?.();
|
|
128
|
+
this.render();
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const question = this.pending ?? (this.onInput ? { label: 'Send an instruction · /pause · /resume · /cancel', live: true } : null);
|
|
133
|
+
if (!question) return;
|
|
134
|
+
if (question.choices && ['up', 'down'].includes(key.name)) {
|
|
135
|
+
question.selected = (question.selected + (key.name === 'up' ? -1 : 1) + question.choices.length) % question.choices.length;
|
|
136
|
+
} else if (key.name === 'return') {
|
|
137
|
+
const answer = question.choices ? question.choices[question.selected].value : this.buffer.trim();
|
|
138
|
+
const shown = question.choices ? question.choices[question.selected].label : question.secret ? '[hidden]' : answer;
|
|
139
|
+
this.pending = null; this.buffer = ''; this.cursor = 0;
|
|
140
|
+
this.event({ kind: 'user', text: `${question.label}\n${shown}` });
|
|
141
|
+
if (question.live) this.onInput(answer).catch(error => this.event({ kind: 'error', text: error.message }));
|
|
142
|
+
else question.resolve(answer);
|
|
143
|
+
} else if (!question.choices) {
|
|
144
|
+
if (key.name === 'backspace') {
|
|
145
|
+
const chars = Array.from(this.buffer.slice(0, this.cursor)); const removed = chars.pop() ?? '';
|
|
146
|
+
this.buffer = chars.join('') + this.buffer.slice(this.cursor); this.cursor -= removed.length;
|
|
147
|
+
} else if (key.name === 'delete') {
|
|
148
|
+
this.buffer = this.buffer.slice(0, this.cursor) + Array.from(this.buffer.slice(this.cursor)).slice(1).join('');
|
|
149
|
+
} else if (key.name === 'left') this.cursor -= Array.from(this.buffer.slice(0, this.cursor)).at(-1)?.length ?? 0;
|
|
150
|
+
else if (key.name === 'right') this.cursor += Array.from(this.buffer.slice(this.cursor))[0]?.length ?? 0;
|
|
151
|
+
else if (key.name === 'home' || key.ctrl && key.name === 'a') this.cursor = 0;
|
|
152
|
+
else if (key.name === 'end' || key.ctrl && key.name === 'e') this.cursor = this.buffer.length;
|
|
153
|
+
else if (key.ctrl && key.name === 'u') { this.buffer = this.buffer.slice(this.cursor); this.cursor = 0; }
|
|
154
|
+
else if (text && !key.ctrl && !key.meta && !text.startsWith('\x1b')) {
|
|
155
|
+
const value = cleanText(text.replaceAll('\r', ' ')).replaceAll('\n', ' ');
|
|
156
|
+
this.buffer = this.buffer.slice(0, this.cursor) + value + this.buffer.slice(this.cursor); this.cursor += value.length;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
this.render();
|
|
160
|
+
}
|
|
161
|
+
render() {
|
|
162
|
+
if (this.closed) return;
|
|
163
|
+
const width = Math.max(12, (this.output.columns ?? 80) - 2);
|
|
164
|
+
const height = this.output.rows ?? 24;
|
|
165
|
+
const dim = this.color ? muted : text => text;
|
|
166
|
+
const strong = this.color ? bold : text => text;
|
|
167
|
+
const stage = this.transcript.stage;
|
|
168
|
+
const labels = width >= 115 ? STAGES : ['Paper', 'Research', 'Materials', 'Experiments', 'Delivery'];
|
|
169
|
+
const symbols = labels.map((name, index) => `${index < stage ? '✓' : index === stage ? this.done ? '✓' : '●' : '○'} ${name}`);
|
|
170
|
+
const steps = wrapText(symbols.join(' · '), width);
|
|
171
|
+
const header = [strong(`CiteArk Agent ${this.version}`), dim(wrapText(this.context || 'Your independent research workspace', width)[0]), '', ...steps.map(dim), '─'.repeat(width)];
|
|
172
|
+
const question = this.pending ?? (this.onInput ? { label: 'Send an instruction · /pause · /resume · /cancel', live: true } : null);
|
|
173
|
+
const footer = [];
|
|
174
|
+
if (this.transcript.offset !== null) footer.push(...wrapText(`Viewing history · ${this.transcript.unseen} new events · End to follow`, width).map(dim));
|
|
175
|
+
footer.push('─'.repeat(width));
|
|
176
|
+
if (question) {
|
|
177
|
+
footer.push(...wrapText(question.label, width));
|
|
178
|
+
if (question.choices) {
|
|
179
|
+
const start = Math.max(0, question.selected - 3);
|
|
180
|
+
question.choices.slice(start, start + 5).forEach((choice, index) => footer.push(wrapText(`${index + start === question.selected ? '›' : ' '} ${choice.label}`, width)[0]));
|
|
181
|
+
footer.push(...wrapText('↑/↓ select · Enter confirm · PgUp/PgDn history', width).map(dim));
|
|
182
|
+
} else {
|
|
183
|
+
const before = this.buffer.slice(0, this.cursor), after = this.buffer.slice(this.cursor);
|
|
184
|
+
const displayed = question.secret ? `${'•'.repeat(Array.from(before).length)}▏${'•'.repeat(Array.from(after).length)}` : `${before}▏${after}`;
|
|
185
|
+
const rows = wrapText(`› ${displayed}`, width);
|
|
186
|
+
footer.push(...rows.slice(-3), ...wrapText('Enter submit · Ctrl+U clear · PgUp/PgDn history', width).map(dim));
|
|
187
|
+
}
|
|
188
|
+
} else footer.push(...wrapText('PgUp/PgDn scroll · End follow · Ctrl+O expand', width).map(dim),
|
|
189
|
+
...wrapText('Ctrl+C exits CLI; compute may continue', width).map(dim),
|
|
190
|
+
...wrapText(`${stage + 1}/5 ${STAGES[stage]} · ${this.transcript.status} · ${Math.floor((Date.now() - this.startedAt) / 1000)}s`, width).map(dim));
|
|
191
|
+
this.bodyHeight = Math.max(1, height - header.length - footer.length - 1);
|
|
192
|
+
const cacheKey = `${this.transcript.revision}:${width}:${this.expanded}`;
|
|
193
|
+
if (this.cached !== cacheKey) { this.lines = this.transcript.lines(width, this.expanded); this.cached = cacheKey; }
|
|
194
|
+
const start = this.transcript.offset === null ? Math.max(0, this.lines.length - this.bodyHeight) : Math.min(this.transcript.offset, Math.max(0, this.lines.length - this.bodyHeight));
|
|
195
|
+
const body = this.lines.slice(start, start + this.bodyHeight);
|
|
196
|
+
while (body.length < this.bodyHeight) body.push('');
|
|
197
|
+
const rows = [...header, ...body, ...footer].slice(0, height - 1);
|
|
198
|
+
const frame = `${ESC}H${rows.map(line => `${line}${ESC}K`).join('\r\n')}${ESC}J`;
|
|
199
|
+
if (frame !== this.lastFrame) { this.rawWrite(frame); this.lastFrame = frame; }
|
|
200
|
+
}
|
|
201
|
+
async finish(status = 'Completed') {
|
|
202
|
+
this.done = status === 'Completed'; this.transcript.status = status;
|
|
203
|
+
await this.ask(`${status}. Press Enter to return to your terminal.`);
|
|
204
|
+
}
|
|
205
|
+
async close() {
|
|
206
|
+
if (this.closed) return;
|
|
207
|
+
this.closed = true; clearInterval(this.timer);
|
|
208
|
+
this.releaseKeys?.(); this.output.off('resize', this.render);
|
|
209
|
+
this.input.setRawMode(this.wasRaw ?? false); this.input.pause();
|
|
210
|
+
console.log = this.savedLog; console.error = this.savedError; console.warn = this.savedWarn;
|
|
211
|
+
this.rawWrite(`${ESC}?2004l${ESC}?1000l${ESC}?1006l${ESC}?25h${ESC}?1049l`);
|
|
212
|
+
if (this.file) { this.file.end(); await finished(this.file); }
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
2
|
+
|
|
3
|
+
export const STAGES = ['Get paper', 'Understand research', 'Prepare materials', 'Run experiments', 'Assess & deliver'];
|
|
4
|
+
const stageIndex = { intake: 0, source_discovery: 0, research_compilation: 1, research_import: 1,
|
|
5
|
+
research_preparation: 1, reproduction_scheduling: 1, planning: 1, awaiting_selection: 1,
|
|
6
|
+
research_plan: 1, contract_creation: 2, compute_scheduling: 2,
|
|
7
|
+
asset_preparation: 2, reproduction: 3, executing: 3, evidence_parsing: 4, verification: 4,
|
|
8
|
+
research_card: 4, execution_handoff: 4, packaging: 4, archive: 4, publishing: 4, completed: 4 };
|
|
9
|
+
export function workflowStage(stage) { return stageIndex[stage]; }
|
|
10
|
+
export function cleanText(value) { return stripVTControlCharacters(String(value)).replace(/[\x00-\x08\x0b-\x1f\x7f]/g, ''); }
|
|
11
|
+
|
|
12
|
+
export class Transcript {
|
|
13
|
+
entries = [];
|
|
14
|
+
indices = new Map();
|
|
15
|
+
revision = 0;
|
|
16
|
+
offset = null;
|
|
17
|
+
unseen = 0;
|
|
18
|
+
stage = 0;
|
|
19
|
+
status = 'Ready';
|
|
20
|
+
add(event) {
|
|
21
|
+
const entry = { ...event, text: cleanText(event.text ?? '') };
|
|
22
|
+
if (event.kind === 'stage') {
|
|
23
|
+
const index = workflowStage(event.stage);
|
|
24
|
+
if (index !== undefined) this.stage = index;
|
|
25
|
+
this.status = event.status ?? 'Running';
|
|
26
|
+
}
|
|
27
|
+
if (!entry.text) return;
|
|
28
|
+
if (entry.id && this.indices.has(entry.id)) this.entries[this.indices.get(entry.id)] = entry;
|
|
29
|
+
else { if (entry.id) this.indices.set(entry.id, this.entries.length); this.entries.push(entry); }
|
|
30
|
+
this.revision++;
|
|
31
|
+
if (this.offset !== null) this.unseen++;
|
|
32
|
+
}
|
|
33
|
+
lines(width, expanded = false) {
|
|
34
|
+
const lines = [];
|
|
35
|
+
for (const entry of this.entries) {
|
|
36
|
+
const prefix = { assistant: '● ', user: '› ', tool: '→ ', error: '! ', status: '· ', stage: '◆ ' }[entry.kind] ?? ' ';
|
|
37
|
+
const raw = entry.text.split('\n');
|
|
38
|
+
const visible = !expanded && entry.kind === 'tool-output' && raw.length > 12 ? [...raw.slice(0, 8), `… ${raw.length - 8} more lines · Ctrl+O to expand`] : raw;
|
|
39
|
+
visible.forEach((line, index) => {
|
|
40
|
+
const text = (index ? ' ' : prefix) + line.replaceAll('\t', ' ');
|
|
41
|
+
lines.push(...wrapText(text, width));
|
|
42
|
+
});
|
|
43
|
+
lines.push('');
|
|
44
|
+
}
|
|
45
|
+
return lines;
|
|
46
|
+
}
|
|
47
|
+
scroll(delta, total, height) {
|
|
48
|
+
const bottom = Math.max(0, total - height);
|
|
49
|
+
this.offset = Math.max(0, Math.min(bottom, (this.offset ?? bottom) + delta));
|
|
50
|
+
if (this.offset >= bottom) this.follow();
|
|
51
|
+
}
|
|
52
|
+
follow() { this.offset = null; this.unseen = 0; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function characterWidth(char) {
|
|
56
|
+
const n = char.codePointAt(0);
|
|
57
|
+
if (/\p{Mark}/u.test(char) || n === 0x200d || n === 0xfe0f) return 0;
|
|
58
|
+
return n >= 0x1100 && (n <= 0x115f || n >= 0x2e80 && n <= 0xa4cf || n >= 0xac00 && n <= 0xd7a3 || n >= 0xf900 && n <= 0xfaff || n >= 0xfe10 && n <= 0xfe6f || n >= 0xff00 && n <= 0xff60 || n >= 0x1f300) ? 2 : 1;
|
|
59
|
+
}
|
|
60
|
+
export function wrapText(text, width) {
|
|
61
|
+
const lines = []; let line = ''; let size = 0;
|
|
62
|
+
for (const char of text) {
|
|
63
|
+
const count = characterWidth(char);
|
|
64
|
+
if (size + count > width && line) { lines.push(line); line = ''; size = 0; }
|
|
65
|
+
line += char; size += count;
|
|
66
|
+
}
|
|
67
|
+
lines.push(line);
|
|
68
|
+
return lines;
|
|
69
|
+
}
|
package/src/util.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
export class CiteArkError extends Error {
|
|
7
|
+
constructor(message, options = {}) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
this.name = "CiteArkError";
|
|
10
|
+
this.exitCode = options.exitCode ?? 1;
|
|
11
|
+
this.retryable = options.retryable;
|
|
12
|
+
this.failureCode = options.failureCode;
|
|
13
|
+
this.failureCategory = options.failureCategory;
|
|
14
|
+
this.retryDisposition = options.retryDisposition;
|
|
15
|
+
this.failureOwner = options.failureOwner;
|
|
16
|
+
this.recoveryAction = options.recoveryAction;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function pathExists(target) {
|
|
21
|
+
try {
|
|
22
|
+
await access(target);
|
|
23
|
+
return true;
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function readJson(target) {
|
|
30
|
+
let raw;
|
|
31
|
+
try {
|
|
32
|
+
raw = await readFile(target, "utf8");
|
|
33
|
+
} catch (error) {
|
|
34
|
+
throw new CiteArkError(`无法读取 JSON 文件:${target}`, { cause: error });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(raw);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new CiteArkError(`JSON 格式无效:${target}\n${error.message}`, {
|
|
41
|
+
cause: error,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function writeJson(target, value) {
|
|
47
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
48
|
+
await writeFile(target, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createRunId(now = new Date()) {
|
|
52
|
+
const stamp = now.toISOString().replaceAll(/[-:.TZ]/g, "").slice(0, 14);
|
|
53
|
+
return `car_${stamp}_${randomBytes(4).toString("hex")}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createSessionId() {
|
|
57
|
+
return randomUUID();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function sha256File(target) {
|
|
61
|
+
const hash = createHash("sha256");
|
|
62
|
+
await new Promise((resolve, reject) => {
|
|
63
|
+
const stream = createReadStream(target);
|
|
64
|
+
stream.on("data", (chunk) => hash.update(chunk));
|
|
65
|
+
stream.on("error", reject);
|
|
66
|
+
stream.on("end", resolve);
|
|
67
|
+
});
|
|
68
|
+
return hash.digest("hex");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function canonicalJson(value) {
|
|
72
|
+
return JSON.stringify(sortCanonical(value));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function sha256Value(value) {
|
|
76
|
+
return createHash("sha256").update(canonicalJson(value)).digest("hex");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function sortCanonical(value) {
|
|
80
|
+
if (Array.isArray(value)) return value.map(sortCanonical);
|
|
81
|
+
if (!isRecord(value)) return value;
|
|
82
|
+
return Object.fromEntries(
|
|
83
|
+
Object.keys(value)
|
|
84
|
+
.sort()
|
|
85
|
+
.map((key) => [key, sortCanonical(value[key])]),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isRecord(value) {
|
|
90
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function shellQuote(value) {
|
|
94
|
+
if (/^[A-Za-z0-9_./:=+-]+$/.test(value)) return value;
|
|
95
|
+
return `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function printableCommand(command, args) {
|
|
99
|
+
return [command, ...args].map(shellQuote).join(" ");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function safeRelativePath(value) {
|
|
103
|
+
if (typeof value !== "string" || value.length === 0) return false;
|
|
104
|
+
if (path.isAbsolute(value)) return false;
|
|
105
|
+
const normalized = path.normalize(value);
|
|
106
|
+
return normalized !== ".." && !normalized.startsWith(`..${path.sep}`);
|
|
107
|
+
}
|