niceeval 0.6.1 → 0.7.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/INDEX.md +23 -23
- package/README.zh.md +6 -6
- package/dist/agents/types.d.ts +69 -7
- package/dist/context/types.d.ts +32 -12
- package/dist/i18n/en.d.ts +54 -0
- package/dist/i18n/zh-CN.d.ts +57 -3
- package/dist/o11y/types.d.ts +16 -2
- package/dist/report/aggregate.d.ts +32 -24
- package/dist/report/aggregate.js +158 -50
- package/dist/report/built-in/index.d.ts +2 -0
- package/dist/report/built-in/index.js +8 -0
- package/dist/report/components.d.ts +93 -160
- package/dist/report/components.js +377 -114
- package/dist/report/compute.d.ts +87 -81
- package/dist/report/compute.js +597 -417
- package/dist/report/flag.d.ts +32 -6
- package/dist/report/flag.js +92 -4
- package/dist/report/format.d.ts +19 -11
- package/dist/report/format.js +30 -13
- package/dist/report/index.d.ts +16 -16
- package/dist/report/index.js +20 -21
- package/dist/report/load.js +3 -2
- package/dist/report/locale.d.ts +57 -33
- package/dist/report/locale.js +122 -56
- package/dist/report/metrics.d.ts +23 -4
- package/dist/report/metrics.js +110 -25
- package/dist/report/primitives.d.ts +48 -15
- package/dist/report/primitives.js +135 -26
- package/dist/report/react/AttemptList.d.ts +9 -7
- package/dist/report/react/AttemptList.js +17 -10
- package/dist/report/react/DeltaTable.js +19 -18
- package/dist/report/react/EvalList.d.ts +4 -4
- package/dist/report/react/EvalList.js +0 -0
- package/dist/report/react/ExperimentComparison.d.ts +10 -0
- package/dist/report/react/ExperimentComparison.js +12 -0
- package/dist/report/react/ExperimentList.d.ts +4 -3
- package/dist/report/react/ExperimentList.js +17 -18
- package/dist/report/react/MetricBars.js +5 -4
- package/dist/report/react/MetricLine.js +12 -5
- package/dist/report/react/MetricMatrix.js +1 -1
- package/dist/report/react/MetricScatter.js +59 -28
- package/dist/report/react/MetricTable.js +2 -12
- package/dist/report/react/ScopeSummary.d.ts +10 -0
- package/dist/report/react/ScopeSummary.js +17 -0
- package/dist/report/react/Scoreboard.js +6 -6
- package/dist/report/react/cell.js +2 -2
- package/dist/report/react/chart-math.d.ts +23 -6
- package/dist/report/react/chart-math.js +71 -19
- package/dist/report/react/fixtures.d.ts +5 -9
- package/dist/report/react/fixtures.js +110 -147
- package/dist/report/react/index.d.ts +15 -5
- package/dist/report/react/index.js +18 -7
- package/dist/report/report.d.ts +137 -16
- package/dist/report/report.js +259 -28
- package/dist/report/text/faces.d.ts +17 -19
- package/dist/report/text/faces.js +253 -184
- package/dist/report/text/plot.js +1 -1
- package/dist/report/text/table.js +38 -7
- package/dist/report/tree.d.ts +90 -40
- package/dist/report/tree.js +252 -94
- package/dist/report/types.d.ts +247 -284
- package/dist/report/types.js +4 -3
- package/dist/report/web.d.ts +21 -5
- package/dist/report/web.js +42 -16
- package/dist/results/select.d.ts +38 -16
- package/dist/results/select.js +73 -25
- package/dist/results/types.d.ts +49 -14
- package/dist/runner/feedback/sink.d.ts +110 -0
- package/dist/runner/types.d.ts +513 -22
- package/dist/sandbox/docker.d.ts +23 -2
- package/dist/sandbox/e2b.d.ts +15 -1
- package/dist/sandbox/errors.d.ts +30 -3
- package/dist/sandbox/io-retry.d.ts +17 -0
- package/dist/sandbox/registry.d.ts +2 -0
- package/dist/sandbox/resolve.d.ts +18 -5
- package/dist/sandbox/retry.d.ts +11 -1
- package/dist/sandbox/types.d.ts +39 -5
- package/dist/sandbox/vercel.d.ts +7 -1
- package/dist/scoring/coverage.d.ts +30 -0
- package/dist/scoring/display.d.ts +21 -0
- package/dist/scoring/display.js +120 -0
- package/dist/scoring/types.d.ts +103 -20
- package/dist/shared/aggregate.d.ts +4 -2
- package/dist/shared/aggregate.js +8 -7
- package/dist/shared/types.d.ts +28 -0
- package/dist/tty-line.d.ts +0 -4
- package/dist/util.d.ts +23 -0
- package/docs-site/zh/README.md +44 -0
- package/docs-site/zh/examples/ai-agent-application.mdx +63 -0
- package/docs-site/zh/examples/coding-agent-extensions.mdx +57 -0
- package/docs-site/zh/examples/index.mdx +50 -0
- package/docs-site/zh/{concepts → explanation}/adapter.mdx +31 -13
- package/docs-site/zh/{concepts → explanation}/assert.mdx +7 -7
- package/docs-site/zh/{concepts → explanation}/drive.mdx +8 -8
- package/docs-site/zh/{concepts → explanation}/evals.mdx +4 -4
- package/docs-site/zh/{concepts → explanation}/experiment.mdx +8 -8
- package/docs-site/zh/{concepts → explanation}/hitl.mdx +8 -8
- package/docs-site/zh/{concepts → explanation}/judge.mdx +5 -5
- package/docs-site/zh/{concepts → explanation}/overview.mdx +11 -11
- package/docs-site/zh/{guides → explanation}/runner.mdx +18 -8
- package/docs-site/zh/{concepts → explanation}/tier.mdx +6 -6
- package/docs-site/zh/{guides → how-to}/agent-feedback-loop.mdx +35 -33
- package/docs-site/zh/{guides → how-to}/authoring.mdx +35 -2
- package/docs-site/zh/{guides → how-to}/ci-integration.mdx +23 -12
- package/docs-site/zh/{guides → how-to}/connect-otel.mdx +6 -6
- package/docs-site/zh/{guides → how-to}/connect-your-agent.mdx +47 -21
- package/docs-site/zh/{guides → how-to}/custom-reports.mdx +34 -39
- package/docs-site/zh/{guides → how-to}/dataset-fanout.mdx +25 -3
- package/docs-site/zh/{guides → how-to}/experiments.mdx +12 -5
- package/docs-site/zh/how-to/publish-report.mdx +105 -0
- package/docs-site/zh/{guides → how-to}/reporters.mdx +2 -2
- package/docs-site/zh/{guides → how-to}/sandbox-agent.mdx +56 -7
- package/docs-site/zh/how-to/sandbox-providers.mdx +350 -0
- package/docs-site/zh/{guides → how-to}/scoring-guide.mdx +4 -4
- package/docs-site/zh/{guides → how-to}/viewing-results.mdx +82 -39
- package/docs-site/zh/{guides → how-to}/write-experiment.mdx +6 -4
- package/docs-site/zh/{guides → how-to}/write-send.mdx +30 -14
- package/docs-site/zh/index.mdx +24 -26
- package/docs-site/zh/introduction.mdx +8 -8
- package/docs-site/zh/reference/builtin-agents.mdx +32 -5
- package/docs-site/zh/reference/capabilities.mdx +8 -8
- package/docs-site/zh/reference/cli.mdx +40 -12
- package/docs-site/zh/reference/define-agent.mdx +58 -5
- package/docs-site/zh/reference/define-config.mdx +1 -1
- package/docs-site/zh/reference/define-eval.mdx +42 -9
- package/docs-site/zh/reference/events.mdx +3 -3
- package/docs-site/zh/reference/expect.mdx +26 -1
- package/docs-site/zh/{guides → reference}/official-adapters.mdx +32 -8
- package/docs-site/zh/{guides → reference}/report-components.mdx +45 -33
- package/docs-site/zh/{guides → reference}/results-data.mdx +21 -13
- package/docs-site/zh/troubleshooting/debug-sandbox.mdx +57 -0
- package/docs-site/zh/troubleshooting/debugging.mdx +212 -0
- package/docs-site/zh/{quickstart.mdx → tutorials/quickstart.mdx} +5 -17
- package/package.json +10 -2
- package/src/agents/ai-sdk-otel.test.ts +1 -0
- package/src/agents/ai-sdk.test.ts +3 -0
- package/src/agents/ai-sdk.ts +3 -0
- package/src/agents/bub-install-spec.test.ts +34 -0
- package/src/agents/bub-install-spec.ts +32 -0
- package/src/agents/bub.ts +31 -32
- package/src/agents/claude-code.test.ts +130 -9
- package/src/agents/claude-code.ts +76 -4
- package/src/agents/codex.test.ts +189 -40
- package/src/agents/codex.ts +155 -14
- package/src/agents/coding-cli-versions.test.ts +15 -0
- package/src/agents/coding-cli-versions.ts +3 -0
- package/src/agents/index.ts +13 -2
- package/src/agents/langgraph.test.ts +204 -0
- package/src/agents/langgraph.ts +495 -0
- package/src/agents/marketplace.ts +85 -0
- package/src/agents/native-config.test.ts +179 -0
- package/src/agents/native-config.ts +267 -0
- package/src/agents/openai-compat.test.ts +1 -0
- package/src/agents/openai-compat.ts +1 -1
- package/src/agents/openclaw.test.ts +31 -0
- package/src/agents/openclaw.ts +171 -0
- package/src/agents/plugin-config.test.ts +1 -0
- package/src/agents/sdk-streams.test.ts +79 -0
- package/src/agents/sdk-streams.ts +55 -10
- package/src/agents/skills.test.ts +1 -0
- package/src/agents/streaming.test.ts +3 -9
- package/src/agents/streaming.ts +2 -2
- package/src/agents/types.ts +71 -8
- package/src/agents/ui-message-stream.test.ts +3 -0
- package/src/cli.ts +446 -124
- package/src/context/context.test.ts +51 -12
- package/src/context/context.ts +162 -30
- package/src/context/session.test.ts +2 -1
- package/src/context/session.ts +115 -7
- package/src/context/types.ts +30 -12
- package/src/define.test.ts +13 -8
- package/src/define.ts +25 -4
- package/src/expect/index.ts +53 -23
- package/src/i18n/en.ts +81 -17
- package/src/i18n/zh-CN.ts +80 -17
- package/src/o11y/cost.test.ts +1 -0
- package/src/o11y/execution-tree.test.ts +1 -20
- package/src/o11y/otlp/mappers/claude-code.test.ts +1 -0
- package/src/o11y/otlp/parse.test.ts +1 -0
- package/src/o11y/otlp/turn-otel.test.ts +1 -0
- package/src/o11y/parsers/bub.test.ts +1 -0
- package/src/o11y/parsers/claude-code.test.ts +1 -34
- package/src/o11y/parsers/openclaw.test.ts +154 -0
- package/src/o11y/parsers/openclaw.ts +310 -0
- package/src/o11y/prices.json +746 -311
- package/src/o11y/tool-names.test.ts +1 -0
- package/src/o11y/types.ts +16 -2
- package/src/report/aggregate.ts +178 -61
- package/src/report/built-in/index.tsx +9 -0
- package/src/report/components.tsx +625 -279
- package/src/report/compute.ts +723 -491
- package/src/report/dual-render.test.tsx +741 -1024
- package/src/report/flag.ts +104 -12
- package/src/report/format.ts +32 -12
- package/src/report/index.ts +119 -46
- package/src/report/load.ts +3 -2
- package/src/report/locale.ts +136 -65
- package/src/report/metrics.ts +108 -25
- package/src/report/primitives.tsx +196 -45
- package/src/report/react/AttemptList.tsx +30 -43
- package/src/report/react/DeltaTable.tsx +63 -45
- package/src/report/react/EvalList.tsx +0 -0
- package/src/report/react/ExperimentComparison.tsx +73 -0
- package/src/report/react/ExperimentList.tsx +50 -32
- package/src/report/react/MetricBars.tsx +5 -4
- package/src/report/react/MetricLine.tsx +13 -8
- package/src/report/react/MetricMatrix.tsx +2 -2
- package/src/report/react/MetricScatter.tsx +86 -34
- package/src/report/react/MetricTable.tsx +4 -76
- package/src/report/react/ScopeSummary.tsx +86 -0
- package/src/report/react/Scoreboard.tsx +28 -10
- package/src/report/react/cell.tsx +2 -2
- package/src/report/react/chart-math.test.ts +85 -0
- package/src/report/react/chart-math.ts +101 -22
- package/src/report/react/enhance.js +89 -5
- package/src/report/react/fixtures.ts +114 -154
- package/src/report/react/index.tsx +24 -39
- package/src/report/react/render.test.tsx +138 -158
- package/src/report/react/styles.css +243 -82
- package/src/report/report.test.ts +779 -841
- package/src/report/report.ts +423 -41
- package/src/report/text/faces.ts +290 -193
- package/src/report/text/plot.ts +1 -1
- package/src/report/text/table.ts +44 -7
- package/src/report/tree.ts +362 -104
- package/src/report/types.ts +261 -271
- package/src/report/web.ts +63 -20
- package/src/results/annotated-source.test.ts +62 -9
- package/src/results/annotated-source.ts +64 -6
- package/src/results/attempt-evidence.test.ts +13 -11
- package/src/results/attempt-evidence.ts +20 -13
- package/src/results/attempt-source.ts +6 -3
- package/src/results/copy.ts +150 -60
- package/src/results/host-equivalence.test.ts +34 -20
- package/src/results/index.ts +12 -4
- package/src/results/locator.test.ts +1 -22
- package/src/results/open.ts +15 -5
- package/src/results/publish.ts +149 -0
- package/src/results/results.test.ts +89 -54
- package/src/results/select.ts +104 -34
- package/src/results/truncate.ts +90 -0
- package/src/results/types.ts +43 -14
- package/src/results/writer.ts +31 -13
- package/src/runner/attempt.test.ts +138 -7
- package/src/runner/attempt.ts +603 -104
- package/src/runner/discover.test.ts +47 -0
- package/src/runner/discover.ts +36 -2
- package/src/runner/eval-source.test.ts +1 -27
- package/src/runner/feedback/agent.test.ts +504 -0
- package/src/runner/feedback/agent.ts +409 -0
- package/src/runner/feedback/ci.test.ts +562 -0
- package/src/runner/feedback/ci.ts +401 -0
- package/src/runner/feedback/coordinator.test.ts +317 -0
- package/src/runner/feedback/coordinator.ts +397 -0
- package/src/runner/feedback/failure.ts +40 -0
- package/src/runner/feedback/human.test.ts +616 -0
- package/src/runner/feedback/human.ts +535 -0
- package/src/runner/feedback/index.ts +66 -0
- package/src/runner/feedback/io.ts +78 -0
- package/src/runner/feedback/profile.test.ts +50 -0
- package/src/runner/feedback/profile.ts +58 -0
- package/src/runner/feedback/reducer.test.ts +395 -0
- package/src/runner/feedback/reducer.ts +260 -0
- package/src/runner/feedback/renderer.ts +82 -0
- package/src/runner/feedback/sink.ts +203 -0
- package/src/runner/feedback/testing.ts +106 -0
- package/src/runner/ledger.test.ts +230 -0
- package/src/runner/ledger.ts +329 -0
- package/src/runner/report.test.ts +128 -3
- package/src/runner/report.ts +33 -9
- package/src/runner/reporters/artifacts.ts +8 -2
- package/src/runner/reporters/braintrust.test.ts +8 -7
- package/src/runner/reporters/braintrust.ts +9 -2
- package/src/runner/reporters/index.ts +2 -2
- package/src/runner/reporters/json.test.ts +162 -0
- package/src/runner/reporters/json.ts +35 -8
- package/src/runner/reporters/shared.ts +1 -5
- package/src/runner/run.test.ts +760 -3
- package/src/runner/run.ts +243 -37
- package/src/runner/sandbox-prep.ts +3 -42
- package/src/runner/timing.ts +158 -0
- package/src/runner/types.ts +518 -22
- package/src/sandbox/checkpoint.test.ts +55 -0
- package/src/sandbox/checkpoint.ts +29 -8
- package/src/sandbox/cli-commands.ts +407 -0
- package/src/sandbox/docker.ts +115 -16
- package/src/sandbox/e2b-agent-template.test.ts +56 -0
- package/src/sandbox/e2b-agent-template.ts +94 -0
- package/src/sandbox/e2b.ts +74 -9
- package/src/sandbox/errors.ts +111 -4
- package/src/sandbox/index.ts +2 -0
- package/src/sandbox/io-retry.test.ts +58 -0
- package/src/sandbox/io-retry.ts +45 -0
- package/src/sandbox/keep-registry.test.ts +86 -0
- package/src/sandbox/keep-registry.ts +142 -0
- package/src/sandbox/keep.ts +178 -0
- package/src/sandbox/paths.test.ts +1 -0
- package/src/sandbox/paths.ts +19 -8
- package/src/sandbox/registry.ts +20 -3
- package/src/sandbox/resolve.ts +76 -11
- package/src/sandbox/retry.test.ts +70 -0
- package/src/sandbox/retry.ts +46 -4
- package/src/sandbox/types.ts +44 -6
- package/src/sandbox/vercel.ts +43 -20
- package/src/scoring/collector.ts +60 -17
- package/src/scoring/coverage.ts +95 -0
- package/src/scoring/diff.ts +81 -0
- package/src/scoring/display.test.ts +121 -0
- package/src/scoring/display.ts +133 -0
- package/src/scoring/evidence.test.ts +189 -0
- package/src/scoring/judge.test.ts +142 -0
- package/src/scoring/judge.ts +15 -18
- package/src/scoring/scoped.ts +217 -50
- package/src/scoring/types.ts +117 -20
- package/src/scoring/verdict.ts +16 -4
- package/src/shared/aggregate.ts +8 -6
- package/src/shared/types.ts +31 -0
- package/src/show/compose.ts +50 -67
- package/src/show/index.ts +127 -56
- package/src/show/render.ts +662 -131
- package/src/show/report-host.test.ts +188 -0
- package/src/show/report-host.ts +375 -0
- package/src/show/show.test.ts +320 -54
- package/src/tty-line.ts +8 -26
- package/src/util.test.ts +1 -0
- package/src/util.ts +41 -0
- package/src/view/app/App.test.tsx +69 -0
- package/src/view/app/App.tsx +144 -48
- package/src/view/app/components/AttemptModal.tsx +423 -11
- package/src/view/app/components/CodeView.tsx +41 -14
- package/src/view/app/components/CopyControls.tsx +2 -2
- package/src/view/app/i18n.ts +37 -17
- package/src/view/app/lib/attempt-route.test.ts +1 -0
- package/src/view/app/lib/verdict.ts +7 -9
- package/src/view/app/main.tsx +13 -8
- package/src/view/app/pages/{RunsPage.tsx → AttemptsPage.tsx} +6 -6
- package/src/view/app/types.ts +4 -1
- package/src/view/artifact-serving.test.ts +2 -1
- package/src/view/client-dist/app.css +1 -1
- package/src/view/client-dist/app.js +17 -17
- package/src/view/data.test.ts +10 -3
- package/src/view/data.ts +155 -49
- package/src/view/index.ts +56 -41
- package/src/view/server.ts +37 -15
- package/src/view/shared/types.ts +34 -5
- package/src/view/styles.css +227 -0
- package/src/view/view-report.test.ts +167 -62
- package/dist/report/built-ins/experiment-comparison.d.ts +0 -1
- package/dist/report/built-ins/experiment-comparison.js +0 -13
- package/dist/report/built-ins/index.d.ts +0 -1
- package/dist/report/built-ins/index.js +0 -2
- package/dist/report/react/GroupSummary.d.ts +0 -8
- package/dist/report/react/GroupSummary.js +0 -8
- package/dist/report/react/RunOverview.d.ts +0 -8
- package/dist/report/react/RunOverview.js +0 -12
- package/docs-site/zh/example/ai-agent-application.mdx +0 -152
- package/docs-site/zh/example/claude-code-codex-plugin.mdx +0 -167
- package/docs-site/zh/example/claude-code-codex-skill.mdx +0 -152
- package/docs-site/zh/example/showcase.mdx +0 -39
- package/docs-site/zh/guides/publish-report.mdx +0 -91
- package/docs-site/zh/guides/sandbox-providers.mdx +0 -102
- package/src/report/built-in-user-parity.test.tsx +0 -640
- package/src/report/built-ins/experiment-comparison.tsx +0 -19
- package/src/report/built-ins/index.ts +0 -2
- package/src/report/react/GroupSummary.tsx +0 -66
- package/src/report/react/RunOverview.tsx +0 -109
- package/src/runner/reporters/console.ts +0 -70
- package/src/runner/reporters/live.test.ts +0 -56
- package/src/runner/reporters/live.ts +0 -247
- package/src/runner/reporters/quiet.test.ts +0 -66
- package/src/runner/reporters/quiet.ts +0 -49
- package/src/runner/reporters/table.ts +0 -277
- /package/docs-site/zh/{example/tier1-ai-sdk-v7.mdx → examples/integrations/ai-sdk-v7.mdx} +0 -0
- /package/docs-site/zh/{example/tier1-claude-sdk.mdx → examples/integrations/claude-sdk.mdx} +0 -0
- /package/docs-site/zh/{example/tier1-codex-sdk.mdx → examples/integrations/codex-sdk.mdx} +0 -0
- /package/docs-site/zh/{example/tier1-langgraph.mdx → examples/integrations/langgraph.mdx} +0 -0
- /package/docs-site/zh/{example/tier1-pi-sdk.mdx → examples/integrations/pi-sdk.mdx} +0 -0
- /package/docs-site/zh/{guides → how-to}/fixtures.mdx +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// cases: docs/engineering/unit-tests/adapters/cases.md
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { parseOpenClawTranscript, parseOpenClaw, parseOpenClawRunJson } from "./openclaw.ts";
|
|
5
|
+
|
|
6
|
+
function jsonl(lines: unknown[]): string {
|
|
7
|
+
return lines.map((l) => JSON.stringify(l)).join("\n");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe("parseOpenClawTranscript", () => {
|
|
11
|
+
it("空 / 无内容 → 空事件流,parseSuccess true", () => {
|
|
12
|
+
expect(parseOpenClawTranscript(undefined)).toEqual({
|
|
13
|
+
events: [],
|
|
14
|
+
usage: { inputTokens: 0, outputTokens: 0 },
|
|
15
|
+
compactions: 0,
|
|
16
|
+
parseSuccess: true,
|
|
17
|
+
});
|
|
18
|
+
expect(parseOpenClawTranscript(" \n ").events).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("assistant 的 text/thinking/toolCall parts + toolResult 按 call ID 配对,usage 逐消息累加", () => {
|
|
22
|
+
const raw = jsonl([
|
|
23
|
+
{ role: "user", content: "改一下配置" },
|
|
24
|
+
{
|
|
25
|
+
role: "assistant",
|
|
26
|
+
content: [
|
|
27
|
+
{ type: "thinking", thinking: "先看文件" },
|
|
28
|
+
{ type: "text", text: "我来改" },
|
|
29
|
+
{ type: "toolCall", id: "tc_1", name: "read", arguments: { path: "a.ts" } },
|
|
30
|
+
],
|
|
31
|
+
usage: { input: 100, output: 20, cacheRead: 5, cacheWrite: 3, cost: { total: 0.01 } },
|
|
32
|
+
},
|
|
33
|
+
{ role: "toolResult", toolCallId: "tc_1", content: [{ type: "text", text: "file body" }], isError: false },
|
|
34
|
+
{
|
|
35
|
+
role: "assistant",
|
|
36
|
+
content: [{ type: "text", text: "改好了" }],
|
|
37
|
+
usage: { input: 50, output: 10, cacheRead: 0, cacheWrite: 0, cost: { total: 0.002 } },
|
|
38
|
+
},
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const parsed = parseOpenClawTranscript(raw);
|
|
42
|
+
expect(parsed.events).toEqual([
|
|
43
|
+
{ type: "message", role: "user", text: "改一下配置" },
|
|
44
|
+
{ type: "thinking", text: "先看文件" },
|
|
45
|
+
{ type: "message", role: "assistant", text: "我来改" },
|
|
46
|
+
{ type: "action.called", callId: "tc_1", name: "read", input: { path: "a.ts" }, tool: "file_read" },
|
|
47
|
+
{ type: "action.result", callId: "tc_1", output: "file body", status: "completed" },
|
|
48
|
+
{ type: "message", role: "assistant", text: "改好了" },
|
|
49
|
+
]);
|
|
50
|
+
expect(parsed.usage).toEqual({
|
|
51
|
+
inputTokens: 150,
|
|
52
|
+
outputTokens: 30,
|
|
53
|
+
cacheReadTokens: 5,
|
|
54
|
+
cacheWriteTokens: 3,
|
|
55
|
+
requests: 2,
|
|
56
|
+
costUSD: 0.012,
|
|
57
|
+
});
|
|
58
|
+
expect(parsed.parseSuccess).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("{ type: 'message', message: {...} } 包装条目与 snake_case 变体也认", () => {
|
|
62
|
+
const raw = jsonl([
|
|
63
|
+
{
|
|
64
|
+
type: "message",
|
|
65
|
+
message: {
|
|
66
|
+
role: "assistant",
|
|
67
|
+
content: [{ type: "tool_call", tool_call_id: "c9", toolName: "exec", input: { cmd: "ls" } }],
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{ type: "message", message: { role: "tool", tool_call_id: "c9", output: "a.txt", is_error: true } },
|
|
71
|
+
]);
|
|
72
|
+
expect(parseOpenClaw(raw)).toEqual([
|
|
73
|
+
{ type: "action.called", callId: "c9", name: "exec", input: { cmd: "ls" }, tool: "shell" },
|
|
74
|
+
{ type: "action.result", callId: "c9", output: "a.txt", status: "failed" },
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("compaction 条目计数,error 条目落 error 事件", () => {
|
|
79
|
+
const parsed = parseOpenClawTranscript(
|
|
80
|
+
jsonl([
|
|
81
|
+
{ type: "compaction", reason: "context-limit" },
|
|
82
|
+
{ type: "error", message: "boom" },
|
|
83
|
+
]),
|
|
84
|
+
);
|
|
85
|
+
expect(parsed.events).toEqual([
|
|
86
|
+
{ type: "compaction", reason: "context-limit" },
|
|
87
|
+
{ type: "error", message: "boom" },
|
|
88
|
+
]);
|
|
89
|
+
expect(parsed.compactions).toBe(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("坏 JSON 行不中断解析,标 parseSuccess: false 但保留其余行的事件", () => {
|
|
93
|
+
const raw = `{"role":"assistant","content":[{"type":"text","text":"hi"}]}\nnot-json\n`;
|
|
94
|
+
const parsed = parseOpenClawTranscript(raw);
|
|
95
|
+
expect(parsed.parseSuccess).toBe(false);
|
|
96
|
+
expect(parsed.events).toEqual([{ type: "message", role: "assistant", text: "hi" }]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("字符串形态的 arguments 先按 JSON 解;解不开原样保留", () => {
|
|
100
|
+
const raw = jsonl([
|
|
101
|
+
{
|
|
102
|
+
role: "assistant",
|
|
103
|
+
content: [{ type: "toolCall", id: "c1", name: "write", arguments: '{"path":"b.ts"}' }],
|
|
104
|
+
},
|
|
105
|
+
]);
|
|
106
|
+
expect(parseOpenClaw(raw)).toEqual([
|
|
107
|
+
{ type: "action.called", callId: "c1", name: "write", input: { path: "b.ts" }, tool: "file_write" },
|
|
108
|
+
]);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("parseOpenClawRunJson", () => {
|
|
113
|
+
it("整段 pretty-print JSON 封包:text / sessionId / usage / failed", () => {
|
|
114
|
+
const stdout = JSON.stringify(
|
|
115
|
+
{
|
|
116
|
+
sessionId: "sess-42",
|
|
117
|
+
result: { text: "done" },
|
|
118
|
+
usage: { input_tokens: 10, output_tokens: 3 },
|
|
119
|
+
status: "ok",
|
|
120
|
+
},
|
|
121
|
+
null,
|
|
122
|
+
2,
|
|
123
|
+
);
|
|
124
|
+
expect(parseOpenClawRunJson(stdout)).toEqual({
|
|
125
|
+
text: "done",
|
|
126
|
+
sessionId: "sess-42",
|
|
127
|
+
usage: { inputTokens: 10, outputTokens: 3 },
|
|
128
|
+
failed: false,
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("混日志行的 stdout:取最后一个完整 JSON 对象;payloads[].text 拼接", () => {
|
|
133
|
+
const stdout = [
|
|
134
|
+
"starting agent...",
|
|
135
|
+
JSON.stringify({ payloads: [{ text: "第一段" }, { text: "第二段" }], sessionKey: "k1" }),
|
|
136
|
+
].join("\n");
|
|
137
|
+
expect(parseOpenClawRunJson(stdout)).toEqual({
|
|
138
|
+
text: "第一段\n第二段",
|
|
139
|
+
sessionId: "k1",
|
|
140
|
+
failed: false,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("error 字段 / status=error / success=false → failed", () => {
|
|
145
|
+
expect(parseOpenClawRunJson(JSON.stringify({ error: "rate limited" })).failed).toBe(true);
|
|
146
|
+
expect(parseOpenClawRunJson(JSON.stringify({ status: "error" })).failed).toBe(true);
|
|
147
|
+
expect(parseOpenClawRunJson(JSON.stringify({ success: false })).failed).toBe(true);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("解析不出 JSON → 空摘要,failed 交给调用方 exitCode", () => {
|
|
151
|
+
expect(parseOpenClawRunJson("plain text output")).toEqual({ failed: false });
|
|
152
|
+
expect(parseOpenClawRunJson(undefined)).toEqual({ failed: false });
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// OpenClaw transcript / `agent --json` 解析器。OpenClaw 方言只住这里,不进 core
|
|
2
|
+
// (契约见 docs/feature/adapters/sdk/openclaw/README.md:字段事实以真实 CLI 与 transcript
|
|
3
|
+
// fixture 固定为准;fixture 未证明的形状这里只做防御式解析,认不出的行标 parseSuccess=false,
|
|
4
|
+
// 绝不从最终文本猜测工具轨迹)。
|
|
5
|
+
//
|
|
6
|
+
// 行为轨两个来源(collection.md 的通道优先级):
|
|
7
|
+
// 1. session transcript JSONL(~/.openclaw/agents/**/sessions/*.jsonl,pi-agent 系
|
|
8
|
+
// 消息格式:role/content parts,toolCall/toolResult 按 call ID 配对)→ parseOpenClawTranscript;
|
|
9
|
+
// 2. `openclaw agent --json` 的 stdout 结果封包(最终回复、session key、失败与 usage 摘要,
|
|
10
|
+
// 无完整工具轨迹)→ parseOpenClawRunJson,只在 transcript 拿不到时兜底。
|
|
11
|
+
|
|
12
|
+
import type { StreamEvent, Usage, ToolName, JsonValue } from "../../types.ts";
|
|
13
|
+
import type { ParsedTranscript } from "./index.ts";
|
|
14
|
+
import { GENERIC_VERB_ALIASES, normalizeToolName as normalizeShared } from "../tool-names.ts";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* OpenClaw 特有工具别名(CLI 自己的 transcript 词汇,不会撞用户域名,裸动词可 opt-in)
|
|
18
|
+
* + 通用裸动词基表。
|
|
19
|
+
*/
|
|
20
|
+
export const OPENCLAW_TOOL_ALIASES: Record<string, ToolName> = {
|
|
21
|
+
...GENERIC_VERB_ALIASES,
|
|
22
|
+
read: "file_read",
|
|
23
|
+
write: "file_write",
|
|
24
|
+
edit: "file_edit",
|
|
25
|
+
process: "shell",
|
|
26
|
+
browser: "web_fetch",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function normalizeToolName(name: string): ToolName {
|
|
30
|
+
return normalizeShared(name, OPENCLAW_TOOL_ALIASES);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function get(obj: unknown, key: string): unknown {
|
|
34
|
+
return obj && typeof obj === "object" ? (obj as Record<string, unknown>)[key] : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function str(v: unknown): string | undefined {
|
|
38
|
+
return typeof v === "string" && v ? v : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function num(obj: unknown, ...keys: string[]): number {
|
|
42
|
+
for (const k of keys) {
|
|
43
|
+
const v = get(obj, k);
|
|
44
|
+
if (typeof v === "number" && Number.isFinite(v)) return v;
|
|
45
|
+
}
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function coerceArgs(value: unknown): JsonValue {
|
|
50
|
+
if (typeof value === "string") {
|
|
51
|
+
try {
|
|
52
|
+
return JSON.parse(value) as JsonValue;
|
|
53
|
+
} catch {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return (value ?? {}) as JsonValue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** content parts / 字符串 → 纯文本(text 类 part 拼接)。 */
|
|
61
|
+
function extractText(content: unknown): string {
|
|
62
|
+
if (typeof content === "string") return content;
|
|
63
|
+
if (Array.isArray(content)) {
|
|
64
|
+
const parts: string[] = [];
|
|
65
|
+
for (const b of content) {
|
|
66
|
+
if (typeof b === "string") parts.push(b);
|
|
67
|
+
else {
|
|
68
|
+
const t = get(b, "text") ?? get(b, "content");
|
|
69
|
+
if (typeof t === "string" && t) parts.push(t);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return parts.join("\n");
|
|
73
|
+
}
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* OpenClaw session transcript(JSONL,pi-agent 系消息格式)→ 标准事件流 + 用量 + 压缩计数。
|
|
79
|
+
* assistant 消息的 text / thinking / toolCall parts → message / thinking / action.called,
|
|
80
|
+
* toolResult 消息按 toolCallId 配对成 action.result(isError → failed),消息级 usage 逐条
|
|
81
|
+
* 累加,compaction 条目计入压缩次数。认不出 / 解析失败的行标 `parseSuccess: false` 并跳过,
|
|
82
|
+
* 不猜测——transcript 不完整时负断言不可信(契约见 sdk/openclaw/README.md)。
|
|
83
|
+
*/
|
|
84
|
+
export function parseOpenClawTranscript(raw: string | undefined): ParsedTranscript {
|
|
85
|
+
const events: StreamEvent[] = [];
|
|
86
|
+
let inputTokens = 0;
|
|
87
|
+
let outputTokens = 0;
|
|
88
|
+
let cacheReadTokens = 0;
|
|
89
|
+
let cacheWriteTokens = 0;
|
|
90
|
+
let costUSD = 0;
|
|
91
|
+
let requests = 0;
|
|
92
|
+
let compactions = 0;
|
|
93
|
+
let parseSuccess = true;
|
|
94
|
+
|
|
95
|
+
if (!raw || !raw.trim()) {
|
|
96
|
+
return { events, usage: { inputTokens: 0, outputTokens: 0 }, compactions: 0, parseSuccess: true };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let synth = 0;
|
|
100
|
+
const nextSynthId = (): string => `oc_${++synth}`;
|
|
101
|
+
|
|
102
|
+
const addUsage = (usage: unknown): void => {
|
|
103
|
+
if (!usage || typeof usage !== "object") return;
|
|
104
|
+
// pi 系简写(input/output/cacheRead/cacheWrite/cost.total)与 snake_case 变体都认。
|
|
105
|
+
const input = num(usage, "input", "input_tokens", "inputTokens", "prompt_tokens");
|
|
106
|
+
const output = num(usage, "output", "output_tokens", "outputTokens", "completion_tokens");
|
|
107
|
+
const cacheRead = num(usage, "cacheRead", "cache_read_input_tokens", "cacheReadTokens");
|
|
108
|
+
const cacheWrite = num(usage, "cacheWrite", "cache_creation_input_tokens", "cacheWriteTokens");
|
|
109
|
+
const cost = num(usage, "cost") || num(get(usage, "cost"), "total");
|
|
110
|
+
if (input === 0 && output === 0 && cacheRead === 0 && cacheWrite === 0 && cost === 0) return;
|
|
111
|
+
inputTokens += input;
|
|
112
|
+
outputTokens += output;
|
|
113
|
+
cacheReadTokens += cacheRead;
|
|
114
|
+
cacheWriteTokens += cacheWrite;
|
|
115
|
+
costUSD += cost;
|
|
116
|
+
requests += 1;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
for (const line of raw.split("\n")) {
|
|
120
|
+
const trimmed = line.trim();
|
|
121
|
+
if (!trimmed) continue;
|
|
122
|
+
|
|
123
|
+
let entry: unknown;
|
|
124
|
+
try {
|
|
125
|
+
entry = JSON.parse(trimmed);
|
|
126
|
+
} catch {
|
|
127
|
+
parseSuccess = false;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
// 会话条目可能带 { type: "message", message: {...} } 包装,也可能就是消息本体。
|
|
133
|
+
const wrappedMessage = get(entry, "message");
|
|
134
|
+
const entryType = get(entry, "type");
|
|
135
|
+
const msg =
|
|
136
|
+
wrappedMessage && typeof wrappedMessage === "object" ? wrappedMessage : entry;
|
|
137
|
+
const role = get(msg, "role");
|
|
138
|
+
|
|
139
|
+
if (entryType === "compaction" || entryType === "compact" || get(entry, "kind") === "compaction") {
|
|
140
|
+
compactions += 1;
|
|
141
|
+
events.push({ type: "compaction", reason: str(get(entry, "reason")) });
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (entryType === "error" && role === undefined) {
|
|
145
|
+
events.push({ type: "error", message: String(get(entry, "message") ?? get(entry, "error") ?? "error") });
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (role === "assistant") {
|
|
150
|
+
addUsage(get(msg, "usage"));
|
|
151
|
+
const content = get(msg, "content");
|
|
152
|
+
if (typeof content === "string") {
|
|
153
|
+
if (content) events.push({ type: "message", role: "assistant", text: content });
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (!Array.isArray(content)) continue;
|
|
157
|
+
for (const part of content) {
|
|
158
|
+
const partType = get(part, "type");
|
|
159
|
+
if (partType === "text") {
|
|
160
|
+
const text = str(get(part, "text"));
|
|
161
|
+
if (text) events.push({ type: "message", role: "assistant", text });
|
|
162
|
+
} else if (partType === "thinking" || partType === "reasoning") {
|
|
163
|
+
const text = str(get(part, "thinking")) ?? str(get(part, "reasoning")) ?? str(get(part, "text"));
|
|
164
|
+
if (text) events.push({ type: "thinking", text });
|
|
165
|
+
} else if (partType === "toolCall" || partType === "tool_call" || partType === "tool-call" || partType === "toolUse") {
|
|
166
|
+
const name = String(get(part, "name") ?? get(part, "toolName") ?? "unknown");
|
|
167
|
+
const callId = str(get(part, "id")) ?? str(get(part, "toolCallId")) ?? str(get(part, "tool_call_id")) ?? nextSynthId();
|
|
168
|
+
events.push({
|
|
169
|
+
type: "action.called",
|
|
170
|
+
callId,
|
|
171
|
+
name,
|
|
172
|
+
input: coerceArgs(get(part, "arguments") ?? get(part, "args") ?? get(part, "input")),
|
|
173
|
+
tool: normalizeToolName(name),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (role === "user") {
|
|
181
|
+
const text = extractText(get(msg, "content") ?? get(msg, "text"));
|
|
182
|
+
if (text) events.push({ type: "message", role: "user", text });
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (role === "toolResult" || role === "tool") {
|
|
187
|
+
const callId =
|
|
188
|
+
str(get(msg, "toolCallId")) ?? str(get(msg, "tool_call_id")) ?? str(get(msg, "id")) ?? nextSynthId();
|
|
189
|
+
const isError = get(msg, "isError") === true || get(msg, "is_error") === true;
|
|
190
|
+
const output = get(msg, "output") ?? get(msg, "result") ?? get(msg, "content");
|
|
191
|
+
events.push({
|
|
192
|
+
type: "action.result",
|
|
193
|
+
callId,
|
|
194
|
+
output: (typeof output === "string" ? output : (extractText(output) || output)) as JsonValue,
|
|
195
|
+
status: isError ? "failed" : "completed",
|
|
196
|
+
});
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
// 其它条目(system prompt、订阅元数据等):无对应 StreamEvent。
|
|
200
|
+
} catch {
|
|
201
|
+
parseSuccess = false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const usage: Usage = { inputTokens, outputTokens };
|
|
206
|
+
if (cacheReadTokens > 0) usage.cacheReadTokens = cacheReadTokens;
|
|
207
|
+
if (cacheWriteTokens > 0) usage.cacheWriteTokens = cacheWriteTokens;
|
|
208
|
+
if (requests > 0) usage.requests = requests;
|
|
209
|
+
if (costUSD > 0) usage.costUSD = costUSD;
|
|
210
|
+
|
|
211
|
+
return { events, usage, compactions, parseSuccess };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 便捷形态:只要 StreamEvent[]。 */
|
|
215
|
+
export function parseOpenClaw(raw: string | undefined): StreamEvent[] {
|
|
216
|
+
return parseOpenClawTranscript(raw).events;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** `openclaw agent --json` 结果封包里抠出的摘要(无完整工具轨迹,只兜底)。 */
|
|
220
|
+
export interface OpenClawRunJson {
|
|
221
|
+
/** 最终回复文本(result.text / payloads[].text / reply 变体)。 */
|
|
222
|
+
text?: string;
|
|
223
|
+
/** 本次 run 的 session key(首轮 capture、后续 resume 用)。 */
|
|
224
|
+
sessionId?: string;
|
|
225
|
+
/** 封包报告的用量;没报就省略,不编造。 */
|
|
226
|
+
usage?: Usage;
|
|
227
|
+
/** 封包自报失败(error 字段 / status=error|failed / success=false)。 */
|
|
228
|
+
failed: boolean;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* `openclaw agent --json` 的 stdout → 结果摘要。整段 stdout 先按单个 JSON 文档解析
|
|
233
|
+
* (含 pretty-print 多行),失败再逐行扫最后一个 JSON 对象(混了日志行的场景)。
|
|
234
|
+
* 完全解析不出时返回空摘要(`failed: false`)——失败判定交给调用方的 exitCode。
|
|
235
|
+
*/
|
|
236
|
+
export function parseOpenClawRunJson(stdout: string | undefined): OpenClawRunJson {
|
|
237
|
+
const doc = parseJsonDocument(stdout);
|
|
238
|
+
if (!doc) return { failed: false };
|
|
239
|
+
|
|
240
|
+
const result = get(doc, "result");
|
|
241
|
+
const textFromPayloads = (payloads: unknown): string | undefined => {
|
|
242
|
+
if (!Array.isArray(payloads)) return undefined;
|
|
243
|
+
const texts = payloads.map((p) => str(get(p, "text"))).filter((t): t is string => !!t);
|
|
244
|
+
return texts.length ? texts.join("\n") : undefined;
|
|
245
|
+
};
|
|
246
|
+
const text =
|
|
247
|
+
str(get(result, "text")) ??
|
|
248
|
+
textFromPayloads(get(result, "payloads")) ??
|
|
249
|
+
textFromPayloads(get(doc, "payloads")) ??
|
|
250
|
+
str(get(doc, "text")) ??
|
|
251
|
+
str(get(doc, "reply")) ??
|
|
252
|
+
str(get(doc, "message"));
|
|
253
|
+
|
|
254
|
+
const sessionId =
|
|
255
|
+
str(get(doc, "sessionId")) ??
|
|
256
|
+
str(get(doc, "session_id")) ??
|
|
257
|
+
str(get(doc, "sessionKey")) ??
|
|
258
|
+
str(get(result, "sessionId")) ??
|
|
259
|
+
str(get(result, "session_id"));
|
|
260
|
+
|
|
261
|
+
const rawUsage = get(doc, "usage") ?? get(result, "usage") ?? get(get(doc, "meta"), "usage");
|
|
262
|
+
let usage: Usage | undefined;
|
|
263
|
+
if (rawUsage && typeof rawUsage === "object") {
|
|
264
|
+
const input = num(rawUsage, "input", "input_tokens", "inputTokens", "prompt_tokens");
|
|
265
|
+
const output = num(rawUsage, "output", "output_tokens", "outputTokens", "completion_tokens");
|
|
266
|
+
if (input > 0 || output > 0) {
|
|
267
|
+
usage = { inputTokens: input, outputTokens: output };
|
|
268
|
+
const cacheRead = num(rawUsage, "cacheRead", "cache_read_input_tokens", "cacheReadTokens");
|
|
269
|
+
if (cacheRead > 0) usage.cacheReadTokens = cacheRead;
|
|
270
|
+
const cost = num(rawUsage, "cost") || num(get(rawUsage, "cost"), "total");
|
|
271
|
+
if (cost > 0) usage.costUSD = cost;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const status = get(doc, "status");
|
|
276
|
+
const failed =
|
|
277
|
+
get(doc, "error") != null ||
|
|
278
|
+
get(doc, "success") === false ||
|
|
279
|
+
(typeof status === "string" && /^(error|failed)$/i.test(status));
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
...(text !== undefined ? { text } : {}),
|
|
283
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
284
|
+
...(usage !== undefined ? { usage } : {}),
|
|
285
|
+
failed,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function parseJsonDocument(stdout: string | undefined): Record<string, unknown> | undefined {
|
|
290
|
+
if (!stdout || !stdout.trim()) return undefined;
|
|
291
|
+
const trimmed = stdout.trim();
|
|
292
|
+
try {
|
|
293
|
+
const doc = JSON.parse(trimmed) as unknown;
|
|
294
|
+
if (doc && typeof doc === "object" && !Array.isArray(doc)) return doc as Record<string, unknown>;
|
|
295
|
+
} catch {
|
|
296
|
+
// 混日志行:逐行找最后一个完整 JSON 对象
|
|
297
|
+
}
|
|
298
|
+
const lines = trimmed.split("\n").reverse();
|
|
299
|
+
for (const line of lines) {
|
|
300
|
+
const l = line.trim();
|
|
301
|
+
if (!l.startsWith("{") || !l.endsWith("}")) continue;
|
|
302
|
+
try {
|
|
303
|
+
const doc = JSON.parse(l) as unknown;
|
|
304
|
+
if (doc && typeof doc === "object") return doc as Record<string, unknown>;
|
|
305
|
+
} catch {
|
|
306
|
+
// 继续往上找
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|