@danceiny/gotry 0.0.1-rc.21 → 0.0.1-rc.24
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/README.md +102 -194
- package/README.zh-CN.md +115 -210
- package/bin/gotry-backend.js +53 -0
- package/bin/gotry-bootstrap.js +466 -35
- package/bin/gotry-inner.js +255 -36
- package/bin/gotry-process-liveness.d.ts +27 -0
- package/bin/gotry-process-liveness.js +97 -0
- package/bin/gotry-runtime-resolution.d.ts +1 -1
- package/bin/gotry-runtime-resolution.js +2 -2
- package/client/client.js +174 -0
- package/cordis.gotry-patch.yml +70 -93
- package/dist/capabilities/agent-reach-deep.js +45 -122
- package/dist/capabilities/agent-reach.js +63 -148
- package/dist/capabilities/anything.js +89 -75
- package/dist/capabilities/artifacts.js +123 -104
- package/dist/capabilities/channel-health.js +65 -45
- package/dist/capabilities/channel-registry.js +108 -87
- package/dist/capabilities/doctor.js +357 -235
- package/dist/capabilities/effect.js +223 -309
- package/dist/capabilities/fact-log.js +28 -14
- package/dist/capabilities/flyai.js +245 -156
- package/dist/capabilities/ground-transfer.js +509 -0
- package/dist/capabilities/hbcli.js +144 -148
- package/dist/capabilities/hotelbyte-transaction.js +194 -0
- package/dist/capabilities/incident-log.js +131 -61
- package/dist/capabilities/model-override.js +30 -10
- package/dist/capabilities/opensky.js +54 -43
- package/dist/capabilities/resilience.js +50 -32
- package/dist/capabilities/session/action-cache.js +50 -44
- package/dist/capabilities/session/adapters/ctrip-flight.js +119 -62
- package/dist/capabilities/session/adapters/ctrip-hotel.js +125 -110
- package/dist/capabilities/session/adapters/dida-portal.js +206 -0
- package/dist/capabilities/session/adapters/meituan-local.js +35 -54
- package/dist/capabilities/session/adapters/rail-12306.js +169 -112
- package/dist/capabilities/session/benchmark.js +85 -149
- package/dist/capabilities/session/extension-bridge.js +320 -259
- package/dist/capabilities/session/extension-channel.js +57 -76
- package/dist/capabilities/session/extension-distribution.js +115 -119
- package/dist/capabilities/session/extract.js +31 -21
- package/dist/capabilities/session/golden-score.js +55 -33
- package/dist/capabilities/session/health-watch.js +146 -154
- package/dist/capabilities/session/read-guard.js +55 -47
- package/dist/capabilities/session/static-flight-golden.js +49 -36
- package/dist/capabilities/session/transport.js +54 -61
- package/dist/capabilities/session/wizard.js +63 -93
- package/dist/capabilities/session-consent.js +143 -47
- package/dist/capabilities/session-login.js +95 -126
- package/dist/capabilities/session-search.js +391 -253
- package/dist/capabilities/sponsor-plugin.js +341 -0
- package/dist/capabilities/visa-policy.js +87 -73
- package/dist/capabilities/weather.js +163 -235
- package/dist/scripts/action-cache-tests.js +24 -59
- package/dist/scripts/agent-planning-turn-deadline-e2e.js +91 -142
- package/dist/scripts/agent-planning-turn-deadline-tests.js +80 -176
- package/dist/scripts/agent-reach-deep-tests.js +30 -40
- package/dist/scripts/agent-reach-tests.js +26 -18
- package/dist/scripts/agent-reach-wrapper-tests.js +46 -50
- package/dist/scripts/anything-tests.js +58 -62
- package/dist/scripts/artifact-client-contract-tests.js +69 -0
- package/dist/scripts/artifacts-capability-tests.js +235 -0
- package/dist/scripts/async-collect.js +26 -15
- package/dist/scripts/benchmark-environment-bridge-e2e.js +287 -684
- package/dist/scripts/benchmark-environment-bridge-tests.js +818 -3123
- package/dist/scripts/booking-copilot-availability-ledger-binding-tests.js +66 -279
- package/dist/scripts/booking-copilot-availability-policy-tests.js +153 -1257
- package/dist/scripts/booking-copilot-bin-proof-tests.js +22 -45
- package/dist/scripts/booking-copilot-crossrepo-fixture-server.js +64 -76
- package/dist/scripts/booking-copilot-dsh-core-proof-tests.js +69 -199
- package/dist/scripts/booking-copilot-dsh-planner-proof-tests.js +393 -759
- package/dist/scripts/booking-copilot-dsh-plugin-proof-tests.js +33 -75
- package/dist/scripts/booking-copilot-event-sequence-concurrency-proof-tests.js +60 -110
- package/dist/scripts/booking-copilot-gap-code-contract-proof-tests.js +28 -66
- package/dist/scripts/booking-copilot-operation-ledger-concurrency-proof-tests.js +72 -271
- package/dist/scripts/booking-copilot-receipt-ledger-concurrency-proof-tests.js +73 -275
- package/dist/scripts/booking-copilot-runtime-proof-tests.js +784 -3398
- package/dist/scripts/booking-copilot-server-proof-tests.js +63 -168
- package/dist/scripts/booking-copilot-startup-proof-tests.js +38 -124
- package/dist/scripts/booking-executor-tests.js +80 -0
- package/dist/scripts/booking-recovery-chain-tests.js +182 -0
- package/dist/scripts/booking-saga-tests.js +61 -123
- package/dist/scripts/booking-surface-contract-proof-tests.js +152 -1167
- package/dist/scripts/bootstrap-tests.js +1383 -128
- package/dist/scripts/build-changelog.js +89 -130
- package/dist/scripts/build-metrics-report.js +135 -143
- package/dist/scripts/changelog-tests.js +35 -82
- package/dist/scripts/channel-probe-tests.js +44 -116
- package/dist/scripts/channel-probe.js +84 -167
- package/dist/scripts/channel-registry-tests.js +97 -185
- package/dist/scripts/companion-tests.js +25 -70
- package/dist/scripts/dida-cookie-probe.js +87 -0
- package/dist/scripts/dida-sw-probe.js +114 -0
- package/dist/scripts/diff-test.js +17 -12
- package/dist/scripts/doctor-tests.js +428 -120
- package/dist/scripts/dsh-artifact-e2e.js +150 -0
- package/dist/scripts/dsh-artifact-web-e2e.js +443 -0
- package/dist/scripts/dsh-http-proxy-sse-proof.js +90 -0
- package/dist/scripts/dsh-runtime-closure-tests.js +110 -153
- package/dist/scripts/dsh-runtime-closure.js +75 -63
- package/dist/scripts/dsh-session-v3-migration-proof.js +106 -0
- package/dist/scripts/dsh-subprocess-local-proof.js +149 -0
- package/dist/scripts/dsh-target-closure-proof.js +178 -0
- package/dist/scripts/effect-tests.js +165 -552
- package/dist/scripts/engine-run.js +1 -3
- package/dist/scripts/engine-tests.js +16 -10
- package/dist/scripts/evaluation-cadence-tests.js +69 -242
- package/dist/scripts/evaluation-contract-tests.js +193 -499
- package/dist/scripts/extension-distribution-cli.js +19 -15
- package/dist/scripts/extension-distribution-tests.js +98 -211
- package/dist/scripts/extension-tests.js +366 -389
- package/dist/scripts/fact-gate-tests.js +725 -263
- package/dist/scripts/fixtures/extension-bridge-unref-child.mjs +21 -0
- package/dist/scripts/flight-hotel-anchor-363-registered-e2e.js +169 -0
- package/dist/scripts/flight-malformed-tests.js +159 -0
- package/dist/scripts/flyai-tests.js +325 -68
- package/dist/scripts/g5-guard-tests.js +131 -0
- package/dist/scripts/g5-guard.js +189 -0
- package/dist/scripts/gen-lunar-anchors.js +106 -0
- package/dist/scripts/gotry-backend-kernel-tests.js +79 -0
- package/dist/scripts/ground-transfer-tests.js +956 -0
- package/dist/scripts/hbcli-e2e-tests.js +71 -90
- package/dist/scripts/hbcli-live-optin-tests.js +96 -0
- package/dist/scripts/hbcli-release-contract-tests.js +122 -0
- package/dist/scripts/hbcli-tests.js +41 -115
- package/dist/scripts/health-watch-cli.js +42 -28
- package/dist/scripts/hotel-date-gate-tests.js +496 -0
- package/dist/scripts/hotelbyte-reconcile-tests.js +141 -0
- package/dist/scripts/hotelbyte-spawn-e2e-tests.js +373 -0
- package/dist/scripts/i18n-tests.js +16 -9
- package/dist/scripts/incident-tests.js +281 -87
- package/dist/scripts/issue-194-job-id-guard-tests.js +106 -0
- package/dist/scripts/issue-233-cancel-refund-commission-tests.js +392 -0
- package/dist/scripts/issue-271-liveness-tests.js +547 -0
- package/dist/scripts/issue-289-model-retry-real-tests.js +296 -0
- package/dist/scripts/issue-289-model-retry-tests.js +441 -0
- package/dist/scripts/issue-289-web-retry-e2e.js +630 -0
- package/dist/scripts/issue-338-home-city-e2e.js +577 -0
- package/dist/scripts/issue343-real-entry-e2e.js +160 -0
- package/dist/scripts/journey-tests.js +22 -27
- package/dist/scripts/kernel-manifest-evidence.js +109 -0
- package/dist/scripts/kernel-manifest-gate.js +126 -0
- package/dist/scripts/kernel-manifest-tests.js +212 -0
- package/dist/scripts/kernel-manifest-trace-entry.js +7 -0
- package/dist/scripts/kernel-manifest-trace-hook.mjs +28 -0
- package/dist/scripts/kernel-manifest.js +369 -0
- package/dist/scripts/ledger-repair-apply-tests.js +316 -0
- package/dist/scripts/ledger-repair-plan-tests.js +375 -0
- package/dist/scripts/ledger-tests.js +325 -340
- package/dist/scripts/ledger-workflow-crash.js +10 -20
- package/dist/scripts/map-tools-vendor-package-proof.js +206 -134
- package/dist/scripts/memory-capture-tests.js +242 -61
- package/dist/scripts/memory-decay-tests.js +18 -52
- package/dist/scripts/memory-lifecycle-tests.js +657 -0
- package/dist/scripts/memory-lifecycle.js +196 -0
- package/dist/scripts/memory-metrics.js +24 -12
- package/dist/scripts/memory-value-report.js +474 -222
- package/dist/scripts/memory-value-tests.js +328 -0
- package/dist/scripts/metrics-report-tests.js +62 -199
- package/dist/scripts/model-override-e2e.js +66 -105
- package/dist/scripts/nightly-evidence-tests.js +64 -89
- package/dist/scripts/nightly-evidence.js +108 -111
- package/dist/scripts/nudge-digest.js +39 -28
- package/dist/scripts/onboarding-tests.js +63 -88
- package/dist/scripts/opensky-check.js +18 -13
- package/dist/scripts/opensky-tests.js +28 -28
- package/dist/scripts/persona-surface-guard-tests.js +37 -12
- package/dist/scripts/pnpm-dsh-closure-proof.js +2 -5
- package/dist/scripts/policy-anchor-359-registered-e2e.js +214 -0
- package/dist/scripts/price-drift-tests.js +126 -227
- package/dist/scripts/price-drift-watch.js +209 -253
- package/dist/scripts/probe-poi-tests.js +52 -45
- package/dist/scripts/product-metrics.js +187 -273
- package/dist/scripts/publish-preverify.js +47 -41
- package/dist/scripts/realtime-pricing-tests.js +37 -76
- package/dist/scripts/replay-async.js +20 -18
- package/dist/scripts/replay-real.js +16 -18
- package/dist/scripts/replay.js +52 -82
- package/dist/scripts/session-attach-diagnose.js +16 -12
- package/dist/scripts/session-attach-poc.js +34 -37
- package/dist/scripts/session-benchmark.js +66 -147
- package/dist/scripts/session-dida-live-e2e.js +151 -0
- package/dist/scripts/session-extract-tests.js +14 -35
- package/dist/scripts/session-login.js +14 -13
- package/dist/scripts/session-tests.js +545 -583
- package/dist/scripts/sf-live-benchmark.js +92 -110
- package/dist/scripts/sf-live-cli-tests.js +2 -9
- package/dist/scripts/sf-soft-score-tests.js +19 -60
- package/dist/scripts/sf-summary-tests.js +149 -0
- package/dist/scripts/sf-summary.js +466 -84
- package/dist/scripts/skeleton-check.js +16 -14
- package/dist/scripts/skeleton-integration-test.js +11 -10
- package/dist/scripts/skills-contract-tests.js +51 -46
- package/dist/scripts/smoke-session-gate-tests.js +12 -20
- package/dist/scripts/smoke.js +541 -422
- package/dist/scripts/sponsor-reuse-tests.js +262 -0
- package/dist/scripts/state-cli-tests.js +226 -101
- package/dist/scripts/state-cli.js +549 -212
- package/dist/scripts/static-golden-tests.js +46 -180
- package/dist/scripts/time-eval-tests.js +210 -192
- package/dist/scripts/travel-timeline-tests.js +29 -93
- package/dist/scripts/turn-handoff-collect-tests.js +46 -93
- package/dist/scripts/turn-handoff-collect.js +71 -99
- package/dist/scripts/turn-policy-tests.js +28 -29
- package/dist/scripts/typed-contract-canary.js +77 -162
- package/dist/scripts/tz-resolver-sanity.js +50 -0
- package/dist/scripts/tz-resolver-tests.js +584 -0
- package/dist/scripts/unified-tests.js +23 -19
- package/dist/scripts/visa-policy-tests.js +31 -44
- package/dist/scripts/weather-tests.js +121 -543
- package/dist/scripts/wish-channel-gate-tests.js +29 -86
- package/dist/scripts/write-gate-tests.js +522 -0
- package/dist/scripts/z3-lifecycle-fault-tests.js +188 -0
- package/dist/scripts/z3-lifecycle-tests.js +170 -0
- package/dist/scripts/z3-race-repeat-tests.js +39 -0
- package/dist/scripts/z3-race-tests.js +18 -12
- package/dist/src/artifact-gate.js +652 -221
- package/dist/src/backend/kernel.js +91 -0
- package/dist/src/backend/modules/booking-copilot.js +55 -0
- package/dist/src/backend/modules/booking-executor.js +124 -0
- package/dist/src/backend/modules/session-search.js +194 -0
- package/dist/src/benchmark-agent-conformance.js +296 -262
- package/dist/src/benchmark-environment-bridge.js +298 -413
- package/dist/src/benchmark-headless-child-diagnostics.js +93 -100
- package/dist/src/benchmark-tool-isolation.js +94 -67
- package/dist/src/bookable-facts.js +299 -174
- package/dist/src/booking-saga.js +64 -120
- package/dist/src/booking-surface/availability-policy.js +443 -576
- package/dist/src/booking-surface/cancel-refund-commission.js +379 -0
- package/dist/src/booking-surface/contracts.js +24 -80
- package/dist/src/booking-surface/dsh-planner.js +510 -281
- package/dist/src/booking-surface/dsh-plugin.js +17 -1
- package/dist/src/booking-surface/error-codes.js +82 -10
- package/dist/src/booking-surface/index.js +0 -3
- package/dist/src/booking-surface/profile.js +10 -54
- package/dist/src/booking-surface/recovery-chain.js +367 -0
- package/dist/src/booking-surface/runtime.js +765 -891
- package/dist/src/booking-surface/server.js +200 -251
- package/dist/src/booking-surface/startup.js +58 -54
- package/dist/src/booking-surface/validation.js +150 -218
- package/dist/src/bridge.js +20 -13
- package/dist/src/companions.js +48 -74
- package/dist/src/contracts.js +21 -43
- package/dist/src/dsh-llm.js +82 -120
- package/dist/src/engine.js +253 -239
- package/dist/src/evaluation-cadence.js +72 -209
- package/dist/src/evaluation-contracts.js +220 -843
- package/dist/src/flight-pack-adapter.js +54 -0
- package/dist/src/flight-pack-contract.js +2 -0
- package/dist/src/gotry-backend.js +70 -0
- package/dist/src/hotel-date-gate.js +118 -0
- package/dist/src/i18n.js +18 -7
- package/dist/src/index.js +1425 -1629
- package/dist/src/journey.js +132 -81
- package/dist/src/ledger-repair-apply.js +538 -0
- package/dist/src/ledger-repair-plan.js +463 -0
- package/dist/src/loop.js +257 -166
- package/dist/src/memory-capture.js +180 -32
- package/dist/src/memory-decay.js +30 -14
- package/dist/src/memory-lifecycle.js +1310 -0
- package/dist/src/memory-utility.js +27 -40
- package/dist/src/mock-llm.js +36 -61
- package/dist/src/model.js +81 -36
- package/dist/src/realtime-pricing.js +63 -72
- package/dist/src/slot-spec.js +84 -106
- package/dist/src/state-ledger.js +310 -471
- package/dist/src/subagent-job-id-guard.js +58 -0
- package/dist/src/time-anchor.js +231 -33
- package/dist/src/tool-packet.js +20 -8
- package/dist/src/travel-slots.js +58 -35
- package/dist/src/travel-timeline.js +44 -63
- package/dist/src/turn-deadline.js +121 -127
- package/dist/src/turn-policy.js +61 -89
- package/dist/src/tz-resolver.js +143 -0
- package/dist/src/unified.js +504 -381
- package/dist/src/wish-pool.js +39 -14
- package/dist/src/write-gate.js +495 -0
- package/dist/src/z3-shared.js +273 -7
- package/extension/README.md +4 -3
- package/extension/background.js +125 -14
- package/extension/content-bridge.js +23 -1
- package/extension/content-main.js +15 -4
- package/extension/manifest.json +16 -8
- package/package.json +251 -220
- package/ts/capabilities/agent-reach.ts +4 -4
- package/ts/capabilities/anything.ts +1 -1
- package/ts/capabilities/artifacts.ts +46 -9
- package/ts/capabilities/flyai.ts +156 -37
- package/ts/capabilities/hbcli.ts +1 -1
- package/ts/capabilities/incident-log.ts +88 -59
- package/ts/capabilities/session/adapters/ctrip-flight.ts +99 -36
- package/ts/capabilities/session/adapters/dida-portal.ts +250 -0
- package/ts/capabilities/session/adapters/rail-12306.ts +123 -24
- package/ts/capabilities/session/extension-bridge.ts +226 -101
- package/ts/capabilities/session/extension-channel.ts +22 -9
- package/ts/capabilities/session/wizard.ts +2 -2
- package/ts/capabilities/session-consent.ts +98 -14
- package/ts/capabilities/session-login.ts +1 -0
- package/ts/capabilities/session-search.ts +315 -31
- package/ts/dsh-runtime/vendor/README.md +8 -4
- package/ts/dsh-runtime/vendor/dsh-map-tools/README.en.md +7 -1
- package/ts/dsh-runtime/vendor/dsh-map-tools/README.md +5 -3
- package/ts/dsh-runtime/vendor/dsh-map-tools/lib/settings-ns.js +1 -1
- package/ts/dsh-runtime/vendor/dsh-map-tools/lib/types/settings-ns.d.ts +1 -1
- package/ts/dsh-runtime/vendor/dsh-map-tools/package.json +6 -6
- package/ts/package.json +236 -16
- package/ts/scripts/map-tools-vendor-package-proof.ts +152 -31
- package/ts/scripts/state-cli.ts +418 -51
- package/ts/src/artifact-gate.ts +561 -15
- package/ts/src/benchmark-environment-bridge.ts +37 -20
- package/ts/src/bookable-facts.ts +183 -7
- package/ts/src/booking-surface/cancel-refund-commission.ts +488 -0
- package/ts/src/booking-surface/dsh-planner.ts +240 -32
- package/ts/src/booking-surface/dsh-plugin.js +17 -1
- package/ts/src/booking-surface/error-codes.ts +75 -0
- package/ts/src/booking-surface/recovery-chain.ts +409 -0
- package/ts/src/booking-surface/server.ts +84 -51
- package/ts/src/booking-surface/validation.ts +2 -0
- package/ts/src/contracts.ts +12 -2
- package/ts/src/dsh-llm.ts +4 -8
- package/ts/src/index.ts +609 -90
- package/ts/src/loop.ts +116 -7
- package/ts/src/memory-capture.ts +227 -3
- package/ts/src/mock-llm.ts +4 -8
- package/ts/src/model.ts +88 -2
- package/ts/src/state-ledger.ts +47 -29
- package/ts/src/subagent-job-id-guard.ts +72 -0
- package/ts/src/time-anchor.ts +217 -13
- package/ts/src/unified.ts +316 -54
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const JOB_TOOLS = new Set(['job_output', 'job_kill']);
|
|
2
|
+
function subagentsOf(ctx) {
|
|
3
|
+
try {
|
|
4
|
+
return ctx.subagents;
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
// Minimal hosts and benchmark contexts may not mount the optional service.
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function jobIdOf(exec) {
|
|
12
|
+
if (!exec.arguments || typeof exec.arguments !== 'object')
|
|
13
|
+
return undefined;
|
|
14
|
+
const jobId = exec.arguments.job_id;
|
|
15
|
+
return typeof jobId === 'string' && jobId.length > 0 ? jobId : undefined;
|
|
16
|
+
}
|
|
17
|
+
function isContinuableChild(entry, jobId) {
|
|
18
|
+
return entry.kind === 'child' && entry.mode === 'continuable' && entry.id === jobId;
|
|
19
|
+
}
|
|
20
|
+
function recoveryReason(toolName, jobId) {
|
|
21
|
+
const stopHint = toolName === 'job_kill'
|
|
22
|
+
? '; use interrupt_agent to stop its current turn.'
|
|
23
|
+
: '.';
|
|
24
|
+
return [
|
|
25
|
+
`SUBAGENT_ID_IS_NOT_JOB_ID: ${JSON.stringify(jobId)} is a continuable subagent durable id, not a background job id.`,
|
|
26
|
+
'Its completion arrives as a completion notice; use list_agents to inspect it and send_message to continue it',
|
|
27
|
+
stopHint,
|
|
28
|
+
].join(' ');
|
|
29
|
+
}
|
|
30
|
+
/** Install the GoTry-owned safety net around dsh's jobs tools. */
|
|
31
|
+
export function installSubagentJobIdGuard(ctx) {
|
|
32
|
+
const ctxOn = ctx.on;
|
|
33
|
+
if (typeof ctxOn !== 'function')
|
|
34
|
+
return;
|
|
35
|
+
ctx.on('tools/pre-execute', async (exec, next) => {
|
|
36
|
+
if (!JOB_TOOLS.has(exec.name))
|
|
37
|
+
return next();
|
|
38
|
+
const jobId = jobIdOf(exec);
|
|
39
|
+
const agent = exec.agent;
|
|
40
|
+
const subagents = subagentsOf(ctx);
|
|
41
|
+
if (!jobId || !agent || !subagents)
|
|
42
|
+
return next();
|
|
43
|
+
let children;
|
|
44
|
+
try {
|
|
45
|
+
children = await subagents.listChildren(agent.id, exec.signal);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
// The guard is diagnostic, not an alternate ownership or availability
|
|
49
|
+
// path. Preserve cancellation; otherwise retain dsh's native job error.
|
|
50
|
+
if (exec.signal.aborted)
|
|
51
|
+
throw error;
|
|
52
|
+
return next();
|
|
53
|
+
}
|
|
54
|
+
if (!children.some(entry => isContinuableChild(entry, jobId)))
|
|
55
|
+
return next();
|
|
56
|
+
return { kind: 'deny', reason: recoveryReason(exec.name, jobId) };
|
|
57
|
+
}, { prepend: true });
|
|
58
|
+
}
|
package/dist/src/time-anchor.js
CHANGED
|
@@ -1,49 +1,146 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 时间锚点层(确定性纯函数,零依赖):「今天是哪天、相对日期怎么换算」的算术全部在这里,
|
|
3
|
+
* LLM 只查锚点卡不自算(仓规:算术只在代码层;tool-owned dates 的执行面)。
|
|
4
|
+
*
|
|
5
|
+
* 消费面:
|
|
6
|
+
* - dsh 成品路径:index.ts 注册 {{time_anchor_card}} 变量,系统时钟;
|
|
7
|
+
* - legacy 抽取路径:dsh-llm.ts 注入 FACTS/SKELETON/槽位抽取 prompt;
|
|
8
|
+
* - 评测:time-eval-tests.ts 用固定 now(2026-08-27)保证确定性。
|
|
9
|
+
*
|
|
10
|
+
* 节日锚点表 SPRING_FESTIVAL:由构建期生成器机械生成(issue #274 清偿 D-9 手抄债务,覆盖 2026-2099);
|
|
11
|
+
* 扩表/换库 = 重跑 ts/scripts/gen-lunar-anchors.ts(--check 为漂移闸,run-all §58)。
|
|
12
|
+
* 到期即红守卫 springFestivalHorizonOk:最晚锚点年份 < 当前年+3 时测试红;表耗尽后锚点卡
|
|
13
|
+
* 显式告警(不再静默省略)——「换个日子的同款债务」的静默耗尽模式已消灭。
|
|
14
|
+
*/
|
|
15
|
+
const WEEKDAYS_ZH = ['日', '一', '二', '三', '四', '五', '六'];
|
|
10
16
|
export function ymd(d) {
|
|
11
17
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
12
18
|
}
|
|
13
19
|
function addDays(d, n) {
|
|
14
20
|
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
|
|
15
21
|
}
|
|
22
|
+
/** ISO 周周一:getDay() 周日=0 归到上周一(-6) */
|
|
16
23
|
function weekMonday(d) {
|
|
17
24
|
return addDays(d, -((d.getDay() + 6) % 7));
|
|
18
25
|
}
|
|
19
|
-
|
|
26
|
+
/**
|
|
27
|
+
* 春节锚点表(正月初一;仅此节日漂移大,故表驱动;元旦/国庆固定月日按「下一次发生」算)。
|
|
28
|
+
* 本块由构建期生成器机械生成,禁止手改(issue #274:手抄常量表=换个日子的同款债务,已否决)。
|
|
29
|
+
* provenance:lunar-typescript@1.8.6(MIT,零传递依赖,devDependency 不进 runtime)· 生成时间 2026-09-11T17:19:45.752Z
|
|
30
|
+
* 命令:cd ts && npx tsx scripts/gen-lunar-anchors.ts(扩表/换库=重跑;--check=漂移闸,run-all §58)
|
|
31
|
+
* 交叉验证:2026-2031 旧表 6 条 + 港天文台 2032-2040 核实 9 条逐条一致(time-eval §7 双 oracle)。
|
|
32
|
+
* 到期守卫:springFestivalHorizonOk——最晚锚点年份 < 当前年+3 测试即红(临近耗尽先红于静默缺失)。
|
|
33
|
+
*/
|
|
34
|
+
// [gen:lunar-anchors:begin] 生成块起点——手改会被下次生成覆盖
|
|
35
|
+
export const SPRING_FESTIVAL = {
|
|
20
36
|
2026: '2026-02-17',
|
|
21
37
|
2027: '2027-02-06',
|
|
22
38
|
2028: '2028-01-26',
|
|
23
39
|
2029: '2029-02-13',
|
|
24
40
|
2030: '2030-02-03',
|
|
25
|
-
2031: '2031-01-23'
|
|
41
|
+
2031: '2031-01-23',
|
|
42
|
+
2032: '2032-02-11',
|
|
43
|
+
2033: '2033-01-31',
|
|
44
|
+
2034: '2034-02-19',
|
|
45
|
+
2035: '2035-02-08',
|
|
46
|
+
2036: '2036-01-28',
|
|
47
|
+
2037: '2037-02-15',
|
|
48
|
+
2038: '2038-02-04',
|
|
49
|
+
2039: '2039-01-24',
|
|
50
|
+
2040: '2040-02-12',
|
|
51
|
+
2041: '2041-02-01',
|
|
52
|
+
2042: '2042-01-22',
|
|
53
|
+
2043: '2043-02-10',
|
|
54
|
+
2044: '2044-01-30',
|
|
55
|
+
2045: '2045-02-17',
|
|
56
|
+
2046: '2046-02-06',
|
|
57
|
+
2047: '2047-01-26',
|
|
58
|
+
2048: '2048-02-14',
|
|
59
|
+
2049: '2049-02-02',
|
|
60
|
+
2050: '2050-01-23',
|
|
61
|
+
2051: '2051-02-11',
|
|
62
|
+
2052: '2052-02-01',
|
|
63
|
+
2053: '2053-02-19',
|
|
64
|
+
2054: '2054-02-08',
|
|
65
|
+
2055: '2055-01-28',
|
|
66
|
+
2056: '2056-02-15',
|
|
67
|
+
2057: '2057-02-04',
|
|
68
|
+
2058: '2058-01-24',
|
|
69
|
+
2059: '2059-02-12',
|
|
70
|
+
2060: '2060-02-02',
|
|
71
|
+
2061: '2061-01-21',
|
|
72
|
+
2062: '2062-02-09',
|
|
73
|
+
2063: '2063-01-29',
|
|
74
|
+
2064: '2064-02-17',
|
|
75
|
+
2065: '2065-02-05',
|
|
76
|
+
2066: '2066-01-26',
|
|
77
|
+
2067: '2067-02-14',
|
|
78
|
+
2068: '2068-02-03',
|
|
79
|
+
2069: '2069-01-23',
|
|
80
|
+
2070: '2070-02-11',
|
|
81
|
+
2071: '2071-01-31',
|
|
82
|
+
2072: '2072-02-19',
|
|
83
|
+
2073: '2073-02-07',
|
|
84
|
+
2074: '2074-01-27',
|
|
85
|
+
2075: '2075-02-15',
|
|
86
|
+
2076: '2076-02-05',
|
|
87
|
+
2077: '2077-01-24',
|
|
88
|
+
2078: '2078-02-12',
|
|
89
|
+
2079: '2079-02-02',
|
|
90
|
+
2080: '2080-01-22',
|
|
91
|
+
2081: '2081-02-09',
|
|
92
|
+
2082: '2082-01-29',
|
|
93
|
+
2083: '2083-02-17',
|
|
94
|
+
2084: '2084-02-06',
|
|
95
|
+
2085: '2085-01-26',
|
|
96
|
+
2086: '2086-02-14',
|
|
97
|
+
2087: '2087-02-03',
|
|
98
|
+
2088: '2088-01-24',
|
|
99
|
+
2089: '2089-02-10',
|
|
100
|
+
2090: '2090-01-30',
|
|
101
|
+
2091: '2091-02-18',
|
|
102
|
+
2092: '2092-02-07',
|
|
103
|
+
2093: '2093-01-27',
|
|
104
|
+
2094: '2094-02-15',
|
|
105
|
+
2095: '2095-02-05',
|
|
106
|
+
2096: '2096-01-25',
|
|
107
|
+
2097: '2097-02-12',
|
|
108
|
+
2098: '2098-02-01',
|
|
109
|
+
2099: '2099-01-21',
|
|
26
110
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
111
|
+
// [gen:lunar-anchors:end]
|
|
112
|
+
/** 到期即红守卫阈值(issue #274):最晚春节锚点年份必须 ≥ 当前年+3——临近耗尽先红于静默缺失。 */
|
|
113
|
+
export const SPRING_FESTIVAL_HORIZON_MIN_YEARS = 3;
|
|
114
|
+
/** 表内最晚锚点年份距 now 的年数(守卫与 time-eval §7 共用;旧表「牙齿证明」传表参)。 */
|
|
115
|
+
export function springFestivalHorizonYears(table, now = new Date()) {
|
|
116
|
+
return Math.max(...Object.keys(table).map(Number)) - now.getFullYear();
|
|
117
|
+
}
|
|
118
|
+
/** D-9 到期即红守卫:最晚锚点年份 < 当前年+3 → 测试红(而非春节锚点静默缺失)。 */
|
|
119
|
+
export function springFestivalHorizonOk(now = new Date()) {
|
|
120
|
+
return springFestivalHorizonYears(SPRING_FESTIVAL, now) >= SPRING_FESTIVAL_HORIZON_MIN_YEARS;
|
|
121
|
+
}
|
|
122
|
+
export function formatUtcOffsetLabel(utcOffsetMinutes) {
|
|
123
|
+
const sign = utcOffsetMinutes >= 0 ? '+' : '-';
|
|
124
|
+
const abs = Math.abs(utcOffsetMinutes);
|
|
31
125
|
const h = Math.floor(abs / 60);
|
|
32
126
|
const m = abs % 60;
|
|
33
|
-
return `UTC${sign}${
|
|
127
|
+
return `UTC${sign}${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;
|
|
128
|
+
}
|
|
129
|
+
function tzLabelOf(d) {
|
|
130
|
+
return formatUtcOffsetLabel(-d.getTimezoneOffset());
|
|
34
131
|
}
|
|
35
132
|
function weekdayOf(d) {
|
|
36
133
|
return `周${WEEKDAYS_ZH[d.getDay()]}`;
|
|
37
134
|
}
|
|
135
|
+
/** 一周七天的渲染:周一 2026-08-31|周二 …|周日 … */
|
|
38
136
|
function weekRow(label, monday) {
|
|
39
|
-
const days = Array.from({
|
|
40
|
-
length: 7
|
|
41
|
-
}, (_, i)=>{
|
|
137
|
+
const days = Array.from({ length: 7 }, (_, i) => {
|
|
42
138
|
const d = addDays(monday, i);
|
|
43
139
|
return `${weekdayOf(d)} ${ymd(d)}`;
|
|
44
140
|
});
|
|
45
141
|
return `${label}:${days.join('|')}`;
|
|
46
142
|
}
|
|
143
|
+
/** 固定月日节日的下一次发生(已过则取明年) */
|
|
47
144
|
function nextOccurrence(todayYmd, year, monthDay) {
|
|
48
145
|
const thisYear = `${year}-${monthDay}`;
|
|
49
146
|
return thisYear >= todayYmd ? thisYear : `${year + 1}-${monthDay}`;
|
|
@@ -55,19 +152,24 @@ export function buildTimeAnchor(now = new Date()) {
|
|
|
55
152
|
const tomorrow = addDays(now, 1);
|
|
56
153
|
const dayAfter = addDays(now, 2);
|
|
57
154
|
const inThreeDays = addDays(now, 3);
|
|
155
|
+
// 下个月:初(1-10)/中旬(11-20)/下旬(21-月末)
|
|
58
156
|
const nm = new Date(year, now.getMonth() + 1, 1);
|
|
59
157
|
const nmYear = nm.getFullYear();
|
|
60
158
|
const nmMonth = nm.getMonth();
|
|
61
159
|
const nmLastDay = new Date(nmYear, nmMonth + 1, 0).getDate();
|
|
62
160
|
const mm = String(nmMonth + 1).padStart(2, '0');
|
|
161
|
+
// 下个季度初
|
|
63
162
|
const nextQuarterMonth0 = Math.floor(now.getMonth() / 3) * 3 + 3;
|
|
64
163
|
const nq = new Date(nextQuarterMonth0 >= 12 ? year + 1 : year, nextQuarterMonth0 % 12, 1);
|
|
65
164
|
const festivals = [
|
|
66
165
|
`国庆 ${nextOccurrence(today, year, '10-01')}`,
|
|
67
|
-
`元旦 ${nextOccurrence(today, year, '01-01')}
|
|
166
|
+
`元旦 ${nextOccurrence(today, year, '01-01')}`,
|
|
68
167
|
];
|
|
69
|
-
const spring = Object.values(SPRING_FESTIVAL).filter(
|
|
70
|
-
if (spring)
|
|
168
|
+
const spring = Object.values(SPRING_FESTIVAL).filter(d => d >= today).sort()[0];
|
|
169
|
+
if (spring)
|
|
170
|
+
festivals.push(`春节 ${spring}`);
|
|
171
|
+
else
|
|
172
|
+
festivals.push('春节(锚点表已到期:重跑 ts/scripts/gen-lunar-anchors.ts 扩表,勿静默省略)');
|
|
71
173
|
const card = [
|
|
72
174
|
`今天 ${today} ${weekdayOf(now)}(时区 ${tzLabelOf(now)})`,
|
|
73
175
|
`明天 ${ymd(tomorrow)} ${weekdayOf(tomorrow)}|后天 ${ymd(dayAfter)} ${weekdayOf(dayAfter)}|大后天 ${ymd(inThreeDays)} ${weekdayOf(inThreeDays)}`,
|
|
@@ -76,25 +178,121 @@ export function buildTimeAnchor(now = new Date()) {
|
|
|
76
178
|
weekRow('下下周', addDays(monday, 14)),
|
|
77
179
|
`下个月 ${nmYear}-${mm}:初 ${mm}-01~${mm}-10|中旬 ${mm}-11~${mm}-20|下旬 ${mm}-21~${mm}-${nmLastDay}`,
|
|
78
180
|
`下个季度初:${ymd(nq)} 起`,
|
|
79
|
-
`节日锚点:${festivals.join('|')}(「国庆前」= 该日之前,以此类推)
|
|
181
|
+
`节日锚点:${festivals.join('|')}(「国庆前」= 该日之前,以此类推)`,
|
|
80
182
|
].join('\n');
|
|
183
|
+
return { today, todayWeekdayZh: weekdayOf(now), tzLabel: tzLabelOf(now), card };
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Extract the smallest named-year intent needed by the planning guard.
|
|
187
|
+
* Relative future years (今年/明年/后年/年内) resolve against the current
|
|
188
|
+
* TimeAnchor, so a future-tense phrase without a four-digit year still yields
|
|
189
|
+
* a named-year window. Historical bypass is intentionally narrow: a negated
|
|
190
|
+
* warning about past dates is not historical lookup, and multiple eligible
|
|
191
|
+
* years are ambiguous. Same-year restatements (「今年年内」「2026 年内」) are
|
|
192
|
+
* one window, not a conflict.
|
|
193
|
+
*/
|
|
194
|
+
export function parsePlanningWindow(text, anchor) {
|
|
195
|
+
const historical = /(?:回测|复盘|backtest|historical)|(?:历史|过去).{0,8}(?:查询|查|检索|回看|回测|复盘)|(?:查询|查|检索|回看).{0,8}(?:历史|过去)/i.test(text);
|
|
196
|
+
const anchorYear = Number(anchor.today.slice(0, 4));
|
|
197
|
+
const relativeYear = (token) => token === '今年' ? anchorYear : token === '明年' ? anchorYear + 1 : anchorYear + 2;
|
|
198
|
+
const matches = [];
|
|
199
|
+
for (const m of text.matchAll(/(?<!\d)(\d{4})\s*年/g)) {
|
|
200
|
+
const start = m.index ?? 0;
|
|
201
|
+
matches.push({ year: Number(m[1]), start, end: start + m[0].length });
|
|
202
|
+
}
|
|
203
|
+
for (const m of text.matchAll(/\b(?:in|within|by)\s+(\d{4})\b/gi)) {
|
|
204
|
+
const start = m.index ?? 0;
|
|
205
|
+
matches.push({ year: Number(m[1]), start, end: start + m[0].length });
|
|
206
|
+
}
|
|
207
|
+
for (const m of text.matchAll(/今年|明年|后年/g)) {
|
|
208
|
+
const start = m.index ?? 0;
|
|
209
|
+
matches.push({ year: relativeYear(m[0]), start, end: start + m[0].length });
|
|
210
|
+
}
|
|
211
|
+
for (const m of text.matchAll(/年内/g)) {
|
|
212
|
+
const start = m.index ?? 0;
|
|
213
|
+
// 「明年年内」「2025 年内」继承紧邻前缀的年份(「年」可已被年份 token 消费);裸「年内」= 锚点当前年
|
|
214
|
+
const adjacent = text.slice(0, start).match(/(今年|明年|后年|\d{4})\s*年?\s*$/);
|
|
215
|
+
matches.push({ year: adjacent ? (/^\d{4}$/.test(adjacent[1]) ? Number(adjacent[1]) : relativeYear(adjacent[1])) : anchorYear, start, end: start + m[0].length });
|
|
216
|
+
}
|
|
217
|
+
if (matches.some(m => !Number.isInteger(m.year) || m.year < 1_000 || m.year > 9_999))
|
|
218
|
+
return null;
|
|
219
|
+
const distinct = [...new Map(matches.map(m => [m.year, m])).values()];
|
|
220
|
+
if (historical && distinct.length !== 1)
|
|
221
|
+
return null;
|
|
222
|
+
const planningCue = /计划|规划|安排|打算|希望|想在|要在|完成|出行|旅行|预订|预定/i;
|
|
223
|
+
const eligibleYears = new Set(matches
|
|
224
|
+
.filter(m => {
|
|
225
|
+
const before = text.slice(Math.max(0, m.start - 24), m.start);
|
|
226
|
+
const after = text.slice(m.end, m.end + 24);
|
|
227
|
+
return planningCue.test(before) || /^\s*(?:内|以内|完成|出行|旅行|预订|预定)/i.test(after);
|
|
228
|
+
})
|
|
229
|
+
.map(m => m.year));
|
|
230
|
+
const selectedYear = eligibleYears.size === 1 ? [...eligibleYears][0]
|
|
231
|
+
: (historical && distinct.length === 1 ? distinct[0].year : null);
|
|
232
|
+
if (selectedYear === null)
|
|
233
|
+
return null;
|
|
234
|
+
return { referenceDate: anchor.today, requestedYear: selectedYear, intent: historical ? 'historical' : 'future' };
|
|
235
|
+
}
|
|
236
|
+
export function planningWindowBounds(window, anchor) {
|
|
237
|
+
if (window.intent === 'historical')
|
|
238
|
+
return null;
|
|
239
|
+
const currentYear = Number(anchor.today.slice(0, 4));
|
|
240
|
+
if (window.requestedYear < currentYear)
|
|
241
|
+
return null;
|
|
242
|
+
const year = String(window.requestedYear).padStart(4, '0');
|
|
81
243
|
return {
|
|
82
|
-
today
|
|
83
|
-
|
|
84
|
-
tzLabel: tzLabelOf(now),
|
|
85
|
-
card
|
|
244
|
+
start: window.requestedYear === currentYear ? anchor.today : `${year}-01-01`,
|
|
245
|
+
end: `${year}-12-31`,
|
|
86
246
|
};
|
|
87
247
|
}
|
|
248
|
+
/** 严格 ISO 校验:YYYY-MM-DD 必须落在真实日历(2026-02-30/2026-13-01 一律拒)。
|
|
249
|
+
* 复用 Date 月末自动进位:同年/月/day 还原回同一串 = 真日历;否则越界。
|
|
250
|
+
* 同时拒掉非有限值(NaN/Infinity)与越界月日(1-12/1-31)。
|
|
251
|
+
* 供 parseAbsoluteDate 词表口径 + hotel-date-gate 终消费层 复用。 */
|
|
252
|
+
export function isRealIsoDate(y, m, d) {
|
|
253
|
+
if (!Number.isFinite(y) || !Number.isFinite(m) || !Number.isFinite(d))
|
|
254
|
+
return false;
|
|
255
|
+
if (m < 1 || m > 12)
|
|
256
|
+
return false;
|
|
257
|
+
if (d < 1 || d > 31)
|
|
258
|
+
return false;
|
|
259
|
+
const dt = new Date(y, m - 1, d);
|
|
260
|
+
return dt.getFullYear() === y && dt.getMonth() === m - 1 && dt.getDate() === d;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* 绝对月日表达 → YYYY-MM-DD(年缺省时按锚点年)。只识别**绝对**表达:
|
|
264
|
+
* ISO(2026-08-01)、数字点分(8.1 / 9.10)、中文(8月5日 / 8月5号)。
|
|
265
|
+
* 严格校验真日历存在性:2026-02-30 / 2026-13-01 / 2025-02-29 等一律返回 null——
|
|
266
|
+
* 词表外不一致日期不冒充合法,闸会归 unresolved 拒绝发供应商查询(issue #283 红线)。
|
|
267
|
+
* 相对/模糊表达(下周一/明天/本周三/下个月中旬/近期/Aug-5 英文月名)一律返回 null——
|
|
268
|
+
* 逐字保留给下游,本层不做换算也不做过期判定(与评测 golden 对齐:英文月日不判过期)。
|
|
269
|
+
*/
|
|
88
270
|
export function parseAbsoluteDate(expr, anchorYear) {
|
|
89
271
|
const t = expr.trim();
|
|
90
272
|
let m = t.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/);
|
|
91
|
-
if (m)
|
|
273
|
+
if (m) {
|
|
274
|
+
const y = Number(m[1]);
|
|
275
|
+
const mo = Number(m[2]);
|
|
276
|
+
const d = Number(m[3]);
|
|
277
|
+
if (!isRealIsoDate(y, mo, d))
|
|
278
|
+
return null;
|
|
279
|
+
return `${m[1]}-${m[2].padStart(2, '0')}-${m[3].padStart(2, '0')}`;
|
|
280
|
+
}
|
|
92
281
|
m = t.match(/^(\d{1,2})[./](\d{1,2})(?:日|号)?$/);
|
|
93
|
-
if (m)
|
|
282
|
+
if (m) {
|
|
283
|
+
const mo = Number(m[1]);
|
|
284
|
+
const d = Number(m[2]);
|
|
285
|
+
if (!isRealIsoDate(anchorYear, mo, d))
|
|
286
|
+
return null;
|
|
287
|
+
return `${anchorYear}-${m[1].padStart(2, '0')}-${m[2].padStart(2, '0')}`;
|
|
288
|
+
}
|
|
94
289
|
m = t.match(/^(\d{1,2})月(\d{1,2})[日号]?$/);
|
|
95
|
-
if (m)
|
|
290
|
+
if (m) {
|
|
291
|
+
const mo = Number(m[1]);
|
|
292
|
+
const d = Number(m[2]);
|
|
293
|
+
if (!isRealIsoDate(anchorYear, mo, d))
|
|
294
|
+
return null;
|
|
295
|
+
return `${anchorYear}-${m[1].padStart(2, '0')}-${m[2].padStart(2, '0')}`;
|
|
296
|
+
}
|
|
96
297
|
return null;
|
|
97
298
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
//# sourceURL=ts/src/time-anchor.ts
|
package/dist/src/tool-packet.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具调用 packet 纪律(RFC S1,loopx agent-loop-effect-interpreter 映射):
|
|
3
|
+
* 每次工具交互 = effect_request(args) → interpretation(interpretArgs)→ observation(返回值)。
|
|
4
|
+
*
|
|
5
|
+
* envelope 是**平铺**的(house style,与 guardToolExecute 兜底同形):
|
|
6
|
+
* - 成功:{ ok: true, ...payload } —— 渲染/调用方按字段读,不受扰;
|
|
7
|
+
* - 失败:{ ok: false, summary, evidence? } —— guard 异常降级天然就是这一支。
|
|
8
|
+
* Handler 是数据不是 callable:observation 必须可 JSON 序列化(跨 dsh/cordis 边界)。
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* 参数归一(interpretation 层):dsh 模型侧对 query 包装参数的三种形态——
|
|
12
|
+
* ① 包装对象 { query: {...} } ② 裸对象(平铺字段) ③ 字符串(按主键收)。
|
|
13
|
+
* 唯一归一入口;工具面不再各自猜形状。
|
|
14
|
+
*/
|
|
1
15
|
export function interpretArgs(args, stringKey) {
|
|
2
|
-
if (args.query && typeof args.query === 'object')
|
|
16
|
+
if (args.query && typeof args.query === 'object')
|
|
17
|
+
return args.query;
|
|
3
18
|
const { query, ...rest } = args;
|
|
4
|
-
if (Object.keys(rest).length > 0)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
19
|
+
if (Object.keys(rest).length > 0)
|
|
20
|
+
return rest;
|
|
21
|
+
if (typeof query === 'string' && stringKey)
|
|
22
|
+
return { [stringKey]: query };
|
|
8
23
|
return {};
|
|
9
24
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//# sourceURL=ts/src/tool-packet.ts
|
package/dist/src/travel-slots.js
CHANGED
|
@@ -1,30 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 槽位抽取层(travel_slot_extraction.v1):面向差旅 intake 的槽位 schema + 确定性过期校验 + 评分器。
|
|
3
|
+
*
|
|
4
|
+
* 设计约定(与评测 golden 逐条对齐):
|
|
5
|
+
* - 时间表达**逐字保留**(「下周一」就写「下周一」),换算归下游工具——tool-owned dates;
|
|
6
|
+
* - 过期判定在代码层(flagExpiredSlots),LLM 只负责逐字抽取;
|
|
7
|
+
* - 过期只判**主日期字段**(requisition.start_date / flight.departure_date / hotel.check_in_date),
|
|
8
|
+
* 且槽位原值保留,仅往 missing_slots 追加 "<domain>.<field> (date is expired)";
|
|
9
|
+
* - 相对/模糊表达永不判过期(「本周三」即使已过也逐字传递,由人裁决语义)。
|
|
10
|
+
*
|
|
11
|
+
* slot→spec 求解桥接不在本期(architecture §10 债务)。
|
|
12
|
+
*/
|
|
1
13
|
import { parseAbsoluteDate } from './time-anchor.js';
|
|
14
|
+
/** 各域的主日期字段——过期校验只判它(对齐 golden:8.1-8.10 仅 start_date 进 missing_slots) */
|
|
2
15
|
const PRIMARY_DATE_FIELD = {
|
|
3
16
|
requisition: 'start_date',
|
|
4
17
|
flight: 'departure_date',
|
|
5
|
-
hotel: 'check_in_date'
|
|
18
|
+
hotel: 'check_in_date',
|
|
6
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* 确定性过期校验(纯函数,不改入参):主日期字段是**绝对**月日表达且早于锚点今天,
|
|
22
|
+
* 往 missing_slots 追加 "<domain>.<field> (date is expired)"(幂等去重);槽位原值不动。
|
|
23
|
+
*/
|
|
7
24
|
export function flagExpiredSlots(ext, anchor) {
|
|
8
25
|
const anchorYear = Number(anchor.today.slice(0, 4));
|
|
9
|
-
const missing = [
|
|
10
|
-
|
|
11
|
-
];
|
|
12
|
-
for (const domain of ext.domains){
|
|
26
|
+
const missing = [...ext.missing_slots];
|
|
27
|
+
for (const domain of ext.domains) {
|
|
13
28
|
const slots = ext.slots[domain];
|
|
14
29
|
const field = PRIMARY_DATE_FIELD[domain];
|
|
15
30
|
const value = slots?.[field];
|
|
16
|
-
if (typeof value !== 'string')
|
|
31
|
+
if (typeof value !== 'string')
|
|
32
|
+
continue;
|
|
17
33
|
const resolved = parseAbsoluteDate(value, anchorYear);
|
|
18
34
|
if (resolved && resolved < anchor.today) {
|
|
19
35
|
const flag = `${domain}.${field} (date is expired)`;
|
|
20
|
-
if (!missing.includes(flag))
|
|
36
|
+
if (!missing.includes(flag))
|
|
37
|
+
missing.push(flag);
|
|
21
38
|
}
|
|
22
39
|
}
|
|
23
|
-
return {
|
|
24
|
-
...ext,
|
|
25
|
-
missing_slots: missing
|
|
26
|
-
};
|
|
40
|
+
return { ...ext, missing_slots: missing };
|
|
27
41
|
}
|
|
42
|
+
/** 槽位抽取 system prompt:锚点卡注入 + 逐字保留铁律(LLM 不做算术、不判过期) */
|
|
28
43
|
export function buildSlotSystem(anchor) {
|
|
29
44
|
return `你是差旅需求的槽位抽取器。从用户原话抽取槽位,只输出 JSON:
|
|
30
45
|
{"schema_version":"travel_slot_extraction.v1","language":"zh|en|mixed","domains":["requisition"|"flight"|"hotel",...],"slots":{...},"missing_slots":[...]}
|
|
@@ -78,32 +93,45 @@ ${anchor.card}
|
|
|
78
93
|
|
|
79
94
|
只输出 JSON,不要多余文本。`;
|
|
80
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* language 的确定性判定(判定归代码层,不信模型):只看用户原话——
|
|
98
|
+
* 含中文且含拉丁字母 → mixed;仅中文 → zh;仅拉丁 → en。
|
|
99
|
+
* (对齐 golden:「next Thursday 去北京」=mixed;全英文带中文槽位值仍=en)
|
|
100
|
+
*/
|
|
81
101
|
export function detectLanguage(utterance) {
|
|
82
102
|
const hasCjk = /[一-鿿]/.test(utterance);
|
|
83
103
|
const hasLatin = /[A-Za-z]/.test(utterance);
|
|
84
|
-
if (hasCjk && hasLatin)
|
|
85
|
-
|
|
104
|
+
if (hasCjk && hasLatin)
|
|
105
|
+
return 'mixed';
|
|
106
|
+
if (hasLatin)
|
|
107
|
+
return 'en';
|
|
86
108
|
return 'zh';
|
|
87
109
|
}
|
|
110
|
+
/** 模型原始 JSON → 规范化 v1 抽取结果(缺省补齐,域外键容忍;language 由 detectLanguage 覆写) */
|
|
88
111
|
export function normalizeExtraction(obj, utterance) {
|
|
89
112
|
const domains = obj['domains'];
|
|
90
|
-
if (!Array.isArray(domains) || domains.length === 0)
|
|
91
|
-
|
|
92
|
-
|
|
113
|
+
if (!Array.isArray(domains) || domains.length === 0)
|
|
114
|
+
return null;
|
|
115
|
+
const valid = domains.filter((d) => d === 'requisition' || d === 'flight' || d === 'hotel');
|
|
116
|
+
if (valid.length === 0)
|
|
117
|
+
return null;
|
|
93
118
|
const lang = obj['language'];
|
|
94
|
-
const slots = obj['slots'] ?? {};
|
|
119
|
+
const slots = (obj['slots'] ?? {});
|
|
95
120
|
const missing = obj['missing_slots'];
|
|
96
121
|
return {
|
|
97
122
|
schema_version: 'travel_slot_extraction.v1',
|
|
98
|
-
language: utterance !== undefined ? detectLanguage(utterance) : lang === 'en' || lang === 'mixed' ? lang : 'zh',
|
|
123
|
+
language: utterance !== undefined ? detectLanguage(utterance) : (lang === 'en' || lang === 'mixed' ? lang : 'zh'),
|
|
99
124
|
domains: valid,
|
|
100
125
|
slots,
|
|
101
|
-
missing_slots: Array.isArray(missing) ? missing.map(String) : []
|
|
126
|
+
missing_slots: Array.isArray(missing) ? missing.map(String) : [],
|
|
102
127
|
};
|
|
103
128
|
}
|
|
104
|
-
const sortedSet = (xs)=>[
|
|
105
|
-
|
|
106
|
-
|
|
129
|
+
const sortedSet = (xs) => [...xs].sort();
|
|
130
|
+
/**
|
|
131
|
+
* 评分器:case pass 的口径 = language 一致 + domains 集合一致 +
|
|
132
|
+
* expected 的每个槽位键值在 actual 中精确命中(actual 多出的键只记 warning)+
|
|
133
|
+
* missing_slots 集合一致。逐字保留语义下,值比较就是字符串精确比较。
|
|
134
|
+
*/
|
|
107
135
|
export function scoreExtraction(expected, actual) {
|
|
108
136
|
const diffs = [];
|
|
109
137
|
const warnings = [];
|
|
@@ -113,32 +141,27 @@ export function scoreExtraction(expected, actual) {
|
|
|
113
141
|
if (JSON.stringify(sortedSet(expected.domains)) !== JSON.stringify(sortedSet(actual.domains))) {
|
|
114
142
|
diffs.push(`domains: 期望 [${sortedSet(expected.domains)}],实际 [${sortedSet(actual.domains)}]`);
|
|
115
143
|
}
|
|
116
|
-
for (const domain of expected.domains){
|
|
144
|
+
for (const domain of expected.domains) {
|
|
117
145
|
const exp = expected.slots[domain];
|
|
118
146
|
const act = actual.slots[domain];
|
|
119
|
-
if (!exp)
|
|
147
|
+
if (!exp)
|
|
148
|
+
continue;
|
|
120
149
|
if (!act) {
|
|
121
150
|
diffs.push(`slots.${domain}: 实际缺失整个域`);
|
|
122
151
|
continue;
|
|
123
152
|
}
|
|
124
|
-
for (const [k, v] of Object.entries(exp)){
|
|
153
|
+
for (const [k, v] of Object.entries(exp)) {
|
|
125
154
|
if (JSON.stringify(act[k]) !== JSON.stringify(v)) {
|
|
126
155
|
diffs.push(`slots.${domain}.${k}: 期望 ${JSON.stringify(v)},实际 ${JSON.stringify(act[k])}`);
|
|
127
156
|
}
|
|
128
157
|
}
|
|
129
|
-
for (const k of Object.keys(act)){
|
|
130
|
-
if (!(k in exp))
|
|
158
|
+
for (const k of Object.keys(act)) {
|
|
159
|
+
if (!(k in exp))
|
|
160
|
+
warnings.push(`slots.${domain}.${k}: 实际多出键 ${JSON.stringify(act[k])}`);
|
|
131
161
|
}
|
|
132
162
|
}
|
|
133
163
|
if (JSON.stringify(sortedSet(expected.missing_slots)) !== JSON.stringify(sortedSet(actual.missing_slots))) {
|
|
134
164
|
diffs.push(`missing_slots: 期望 [${expected.missing_slots}],实际 [${actual.missing_slots}]`);
|
|
135
165
|
}
|
|
136
|
-
return {
|
|
137
|
-
pass: diffs.length === 0,
|
|
138
|
-
diffs,
|
|
139
|
-
warnings
|
|
140
|
-
};
|
|
166
|
+
return { pass: diffs.length === 0, diffs, warnings };
|
|
141
167
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
//# sourceURL=ts/src/travel-slots.ts
|