@arcticdata/dsh-datarc-assistant 26.39.0 → 26.39.1
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 +45 -10
- package/api-operations.js +404 -0
- package/client.js +452 -87
- package/cordis.patch.yml +4 -4
- package/entry/SKILL.md +34 -0
- package/icon.png +0 -0
- package/index.js +35 -47
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/native-api.mjs +326 -0
- package/native-contracts.js +195 -0
- package/native-execution.js +184 -0
- package/native-runner.mjs +107 -0
- package/native-state.js +67 -0
- package/native-tools.js +241 -0
- package/operations.js +1260 -0
- package/package.json +65 -51
- package/{skills → runtime}/datarc-access-permissions/SKILL.md +30 -30
- package/{skills → runtime}/datarc-access-permissions/references/api.md +31 -31
- package/{skills → runtime}/datarc-access-permissions/references/write-order.md +16 -16
- package/{skills → runtime}/datarc-analysis-report/SKILL.md +125 -125
- package/{skills → runtime}/datarc-analysis-report/agents/openai.yaml +6 -6
- package/{skills → runtime}/datarc-analysis-report/references/analyst-frameworks.md +215 -215
- package/{skills → runtime}/datarc-analysis-report/references/api-contracts.md +478 -478
- package/{skills → runtime}/datarc-analysis-report/references/direct-card-fallback-design.md +66 -66
- package/{skills → runtime}/datarc-analysis-report/references/human-in-the-loop.md +142 -142
- package/{skills → runtime}/datarc-analysis-report/references/loop-personalization-policy.md +138 -138
- package/{skills → runtime}/datarc-analysis-report/references/manifest-example.json +779 -779
- package/{skills → runtime}/datarc-analysis-report/references/narrative-schema.md +160 -160
- package/{skills → runtime}/datarc-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-analysis-report/scripts/run_analysis_report.py +2225 -2225
- package/{skills → runtime}/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -1914
- package/{skills → runtime}/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -5295
- package/{skills → runtime}/datarc-data-search/SKILL.md +243 -243
- package/{skills → runtime}/datarc-data-search/scripts/calculate.py +116 -116
- package/{skills → runtime}/datarc-data-search/scripts/data-search.mjs +628 -628
- package/{skills/datarc-dataseek-search → runtime/datarc-data-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-data-search/scripts/search-result.mjs +211 -211
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/SKILL.md +59 -59
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -36
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -30
- package/{skills/datarc-data-search → runtime/datarc-dataseek-batch-evaluation}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -1020
- package/{skills → runtime}/datarc-dataseek-search/SKILL.md +96 -96
- package/{skills/datarc-dataseek-batch-evaluation → runtime/datarc-dataseek-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -257
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/SKILL.md +44 -44
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/evals/evals.json +165 -165
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/dataset.md +77 -77
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -93
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/interventions.md +64 -64
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -47
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/rollback.md +11 -11
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -95
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -29
- package/{skills → runtime}/datarc-dataseek-testset-generation/SKILL.md +47 -47
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -526
- package/{skills → runtime}/datarc-datasource/SKILL.md +97 -97
- package/{skills → runtime}/datarc-oauth/SKILL.md +50 -50
- package/{skills → runtime}/datarc-oauth/scripts/auth.mjs +192 -192
- package/{skills → runtime}/datarc-oauth/scripts/credentials.mjs +237 -237
- package/{skills → runtime}/datarc-report-add-card/SKILL.md +90 -90
- package/{skills → runtime}/datarc-report-add-card/agents/openai.yaml +4 -4
- package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/{skills → runtime}/datarc-report-add-card/scripts/report-card.mjs +439 -439
- package/{skills → runtime}/datarc-semantic-alias/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias-plan.mjs +422 -422
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-calibration/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -683
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-core/SKILL.md +12 -12
- package/{skills → runtime}/datarc-semantic-core/scripts/artifact-journal.mjs +853 -830
- package/{skills → runtime}/datarc-semantic-core/scripts/canonical.mjs +48 -48
- package/{skills → runtime}/datarc-semantic-core/scripts/case-model.mjs +608 -608
- package/{skills → runtime}/datarc-semantic-core/scripts/cli.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/datarc-client.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -57
- package/{skills → runtime}/datarc-semantic-core/scripts/interventions.mjs +693 -693
- package/{skills → runtime}/datarc-semantic-core/scripts/live-resolution.mjs +305 -305
- package/{skills → runtime}/datarc-semantic-core/scripts/model-scope.mjs +49 -49
- package/{skills → runtime}/datarc-semantic-core/scripts/reproduction.mjs +330 -330
- package/{skills → runtime}/datarc-semantic-core/scripts/semantic-cli.mjs +799 -740
- package/{skills → runtime}/datarc-semantic-core/scripts/shared.mjs +312 -312
- package/{skills → runtime}/datarc-semantic-core/scripts/verification.mjs +1111 -1014
- package/{skills → runtime}/datarc-semantic-dataset/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -749
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -813
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-knowledge/SKILL.md +38 -38
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -61
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-generation.json +16 -16
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -347
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -848
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -31
- package/{skills → runtime}/datarc-semantic-prompt/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -447
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt.mjs +15 -15
- package/{skills → runtime}/datarc-semantic-synonym/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -688
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym.mjs +11 -11
- package/{skills → runtime}/datarc-viz-analysis-report/SKILL.md +108 -108
- package/{skills → runtime}/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -8
- package/{skills → runtime}/datarc-viz-analysis-report/assets/echarts.min.js +45 -45
- package/{skills → runtime}/datarc-viz-analysis-report/references/api-contracts.md +163 -163
- package/{skills → runtime}/datarc-viz-analysis-report/references/derivation-log.md +157 -157
- package/{skills → runtime}/datarc-viz-analysis-report/references/evidence-framework.md +76 -76
- package/{skills → runtime}/datarc-viz-analysis-report/references/html-template.md +92 -92
- package/{skills → runtime}/datarc-viz-analysis-report/references/provenance-format.md +56 -56
- package/{skills → runtime}/datarc-viz-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -1708
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -964
- package/{skills → runtime}/datarc-workspace-configuration/SKILL.md +59 -59
- package/{skills → runtime}/datarc-workspace-configuration/references/artifacts.md +75 -75
- package/{skills → runtime}/datarc-workspace-configuration/references/verification.md +20 -20
- package/{skills → runtime}/datarc-workspace-configuration/scripts/configuration.mjs +722 -662
- package/{skills → runtime}/datarc-workspace-context/SKILL.md +43 -43
- package/{skills → runtime}/datarc-workspace-context/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-context/scripts/datarc-client.mjs +31 -31
- package/{skills → runtime}/datarc-workspace-context/scripts/query-display.mjs +114 -114
- package/{skills → runtime}/datarc-workspace-context/scripts/workspace-context.mjs +58 -58
- package/{skills → runtime}/datarc-workspace-custom-tables/SKILL.md +89 -89
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -165
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -601
- package/{skills → runtime}/datarc-workspace-keywords/SKILL.md +197 -197
- package/{skills → runtime}/datarc-workspace-keywords/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-keywords/references/api-fields.md +79 -79
- package/{skills → runtime}/datarc-workspace-keywords/references/derived-measures.md +177 -177
- package/{skills → runtime}/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -145
- package/{skills → runtime}/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -2421
- package/{skills → runtime}/datarc-workspace-membership/SKILL.md +29 -29
- package/{skills → runtime}/datarc-workspace-membership/references/api.md +19 -19
- package/{skills → runtime}/datarc-workspace-modeling/SKILL.md +209 -209
- package/{skills → runtime}/datarc-workspace-modeling/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-modeling/references/join-preview.md +38 -38
- package/{skills → runtime}/datarc-workspace-modeling/references/model-plan.md +300 -300
- package/{skills → runtime}/datarc-workspace-modeling/references/model-tags.md +95 -95
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-contract.json +29 -29
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -2423
- package/{skills → runtime}/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -137
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -238
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -1261
- package/skills-provider.js +51 -0
- package/workflow-index.json +434 -0
- package/icon.svg +0 -4
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +0 -464
- package/skills-index.json +0 -194
|
@@ -1,330 +1,330 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
|
|
3
|
-
import { actionInterpretations } from "./interpretation-pairs.mjs";
|
|
4
|
-
import {
|
|
5
|
-
canonicalPositiveInteger,
|
|
6
|
-
fail,
|
|
7
|
-
isRecord,
|
|
8
|
-
requireClient,
|
|
9
|
-
requiredTrimmedString,
|
|
10
|
-
safeOpaqueIdentifier,
|
|
11
|
-
uniquePairs,
|
|
12
|
-
} from "./shared.mjs";
|
|
13
|
-
|
|
14
|
-
const ALLOWED_INPUT_KEYS = new Set([
|
|
15
|
-
"caseId",
|
|
16
|
-
"question",
|
|
17
|
-
"sourceType",
|
|
18
|
-
"workspaceId",
|
|
19
|
-
]);
|
|
20
|
-
const ALLOWED_SOURCES = new Set([
|
|
21
|
-
"current_search_correction",
|
|
22
|
-
"reported_search_correction",
|
|
23
|
-
]);
|
|
24
|
-
const SAFE_OPAQUE_ID = /^[0-9A-Za-z._:+/-]+$/u;
|
|
25
|
-
const SAFE_ROUTE_ID = /^[0-9A-Za-z._:+-]+$/u;
|
|
26
|
-
const SAFE_ACTION_TYPE = /^[A-Za-z][0-9A-Za-z_.:-]{0,63}$/u;
|
|
27
|
-
|
|
28
|
-
export function normalizeReproductionInput(input) {
|
|
29
|
-
if (!isRecord(input))
|
|
30
|
-
fail("INVALID_REPRODUCTION_INPUT", "reproduction input must be an object");
|
|
31
|
-
const unknownKeys = Object.keys(input).filter(
|
|
32
|
-
(key) => !ALLOWED_INPUT_KEYS.has(key),
|
|
33
|
-
);
|
|
34
|
-
if (unknownKeys.length)
|
|
35
|
-
fail(
|
|
36
|
-
"INVALID_REPRODUCTION_INPUT",
|
|
37
|
-
"reproduction input contains unsupported fields",
|
|
38
|
-
);
|
|
39
|
-
if (!Number.isInteger(input.workspaceId) || input.workspaceId <= 0)
|
|
40
|
-
fail(
|
|
41
|
-
"INVALID_REPRODUCTION_INPUT",
|
|
42
|
-
"workspaceId must be a positive integer",
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const caseId = requiredTrimmedString(input.caseId);
|
|
46
|
-
const question = requiredTrimmedString(input.question);
|
|
47
|
-
const sourceType = requiredTrimmedString(input.sourceType);
|
|
48
|
-
if (!caseId || !question || !ALLOWED_SOURCES.has(sourceType))
|
|
49
|
-
fail(
|
|
50
|
-
"INVALID_REPRODUCTION_INPUT",
|
|
51
|
-
"caseId, question, and a supported correction source are required",
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
workspaceId: input.workspaceId,
|
|
56
|
-
caseId,
|
|
57
|
-
sourceType,
|
|
58
|
-
question,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function reproduceCase(client, input, options = {}) {
|
|
63
|
-
const normalized = normalizeReproductionInput(input);
|
|
64
|
-
requireClient(client);
|
|
65
|
-
const poll = normalizePollOptions(options);
|
|
66
|
-
const started = readClock(poll.now);
|
|
67
|
-
const deadline = started.milliseconds + poll.maxWaitMs;
|
|
68
|
-
const workspacePath = `/api/v3/account-projects/${normalized.workspaceId}`;
|
|
69
|
-
|
|
70
|
-
const admin = await client.request(
|
|
71
|
-
"GET",
|
|
72
|
-
`/api/v2/admin-projects/${normalized.workspaceId}/`,
|
|
73
|
-
);
|
|
74
|
-
validateAdminIdentity(admin, normalized.workspaceId);
|
|
75
|
-
|
|
76
|
-
const createdThread = await client.request(
|
|
77
|
-
"POST",
|
|
78
|
-
`${workspacePath}/threads/`,
|
|
79
|
-
{ body: {} },
|
|
80
|
-
);
|
|
81
|
-
const thread = normalizeThread(createdThread);
|
|
82
|
-
|
|
83
|
-
const cutResponse = await client.request("POST", `${workspacePath}/cut/`, {
|
|
84
|
-
body: { tokens: normalized.question },
|
|
85
|
-
});
|
|
86
|
-
const cut = normalizeCut(cutResponse);
|
|
87
|
-
|
|
88
|
-
const messageCollection = `${workspacePath}/threads/${thread.uid}/messages/`;
|
|
89
|
-
const createdMessage = await client.request("POST", messageCollection, {
|
|
90
|
-
body: { message: normalized.question },
|
|
91
|
-
});
|
|
92
|
-
const message = normalizeCreatedMessage(createdMessage);
|
|
93
|
-
const messagePath = `${messageCollection}${message.id}/`;
|
|
94
|
-
|
|
95
|
-
while (true) {
|
|
96
|
-
const response = await client.request("GET", messagePath);
|
|
97
|
-
const terminal = inspectPolledMessage(response, message.id);
|
|
98
|
-
if (terminal) {
|
|
99
|
-
const observed = readClock(poll.now);
|
|
100
|
-
return buildResult(
|
|
101
|
-
normalized,
|
|
102
|
-
started.iso,
|
|
103
|
-
observed.iso,
|
|
104
|
-
thread,
|
|
105
|
-
message,
|
|
106
|
-
cut,
|
|
107
|
-
terminal,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const current = readClock(poll.now);
|
|
112
|
-
const remainingMs = deadline - current.milliseconds;
|
|
113
|
-
if (remainingMs <= 0)
|
|
114
|
-
fail(
|
|
115
|
-
"REPRODUCTION_TIMEOUT",
|
|
116
|
-
"DataSeek reproduction did not reach a terminal result",
|
|
117
|
-
{
|
|
118
|
-
workspaceId: normalized.workspaceId,
|
|
119
|
-
state: "pending",
|
|
120
|
-
},
|
|
121
|
-
);
|
|
122
|
-
await poll.sleep(Math.min(poll.pollIntervalMs, remainingMs));
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function buildResult(
|
|
127
|
-
input,
|
|
128
|
-
startedAt,
|
|
129
|
-
observedAt,
|
|
130
|
-
thread,
|
|
131
|
-
message,
|
|
132
|
-
cut,
|
|
133
|
-
terminal,
|
|
134
|
-
) {
|
|
135
|
-
const actions = terminal.actions;
|
|
136
|
-
return {
|
|
137
|
-
workspaceId: input.workspaceId,
|
|
138
|
-
caseId: input.caseId,
|
|
139
|
-
sourceType: input.sourceType,
|
|
140
|
-
questionHash: createHash("sha256").update(input.question).digest("hex"),
|
|
141
|
-
startedAt,
|
|
142
|
-
observedAt,
|
|
143
|
-
thread,
|
|
144
|
-
message: {
|
|
145
|
-
...message,
|
|
146
|
-
state: terminal.state,
|
|
147
|
-
actionTypes: actions
|
|
148
|
-
.map((action) => safeActionType(action?.type))
|
|
149
|
-
.filter(Boolean),
|
|
150
|
-
resultCount: actions.length,
|
|
151
|
-
interpretations:
|
|
152
|
-
terminal.state === "failed" ? [] : actionInterpretations(actions),
|
|
153
|
-
},
|
|
154
|
-
cut,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function validateAdminIdentity(response, workspaceId) {
|
|
159
|
-
if (!isRecord(response))
|
|
160
|
-
fail(
|
|
161
|
-
"INVALID_ADMIN_RESPONSE",
|
|
162
|
-
"administrator workspace response is invalid",
|
|
163
|
-
);
|
|
164
|
-
const responseId = canonicalPositiveInteger(response.id);
|
|
165
|
-
if (responseId === null)
|
|
166
|
-
fail(
|
|
167
|
-
"INVALID_ADMIN_RESPONSE",
|
|
168
|
-
"administrator workspace response is missing its identity",
|
|
169
|
-
);
|
|
170
|
-
if (responseId !== workspaceId)
|
|
171
|
-
fail(
|
|
172
|
-
"WORKSPACE_IDENTITY_MISMATCH",
|
|
173
|
-
"administrator response belongs to another workspace",
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function normalizeThread(response) {
|
|
178
|
-
if (!isRecord(response))
|
|
179
|
-
fail("INVALID_THREAD_RESPONSE", "new thread response is invalid");
|
|
180
|
-
const uid = safeRouteIdentifier(response.uid);
|
|
181
|
-
const createdAt = safeTimestamp(response.createdAt ?? response.created_at);
|
|
182
|
-
if (!uid || !createdAt)
|
|
183
|
-
fail(
|
|
184
|
-
"INVALID_THREAD_RESPONSE",
|
|
185
|
-
"new thread response is missing a safe identity or creation time",
|
|
186
|
-
);
|
|
187
|
-
return { uid, createdAt };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function normalizeCut(response) {
|
|
191
|
-
if (!isRecord(response) || !Array.isArray(response.tokens))
|
|
192
|
-
fail("INVALID_CUT_RESPONSE", "cut response is invalid");
|
|
193
|
-
const identity = safeOpaqueIdentifier(response.identity);
|
|
194
|
-
const momentIdentity = safeOpaqueIdentifier(response.momentIdentity);
|
|
195
|
-
if (!identity || !momentIdentity)
|
|
196
|
-
fail("INVALID_CUT_RESPONSE", "cut response is missing safe identities");
|
|
197
|
-
|
|
198
|
-
const pairs = [];
|
|
199
|
-
for (const token of response.tokens) {
|
|
200
|
-
if (!isRecord(token))
|
|
201
|
-
fail("INVALID_CUT_RESPONSE", "cut response contains an invalid token");
|
|
202
|
-
const word = requiredTrimmedString(token.word);
|
|
203
|
-
const prop = requiredTrimmedString(token.prop);
|
|
204
|
-
if (word && prop) pairs.push({ word, prop });
|
|
205
|
-
}
|
|
206
|
-
return { identity, momentIdentity, pairs: uniquePairs(pairs) };
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function normalizeCreatedMessage(response) {
|
|
210
|
-
if (!isRecord(response))
|
|
211
|
-
fail("INVALID_MESSAGE_RESPONSE", "new message response is invalid");
|
|
212
|
-
const id = safeMessageId(response.id);
|
|
213
|
-
const createdAt = safeTimestamp(response.createdAt ?? response.created_at);
|
|
214
|
-
if (id === null || !createdAt)
|
|
215
|
-
fail(
|
|
216
|
-
"INVALID_MESSAGE_RESPONSE",
|
|
217
|
-
"new message response is missing a safe identity or creation time",
|
|
218
|
-
);
|
|
219
|
-
return { id, createdAt };
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function inspectPolledMessage(response, expectedId) {
|
|
223
|
-
if (!isRecord(response))
|
|
224
|
-
fail("INVALID_MESSAGE_RESPONSE", "polled message response is invalid");
|
|
225
|
-
const id = safeMessageId(response.id);
|
|
226
|
-
if (id === null)
|
|
227
|
-
fail(
|
|
228
|
-
"INVALID_MESSAGE_RESPONSE",
|
|
229
|
-
"polled message response is missing its identity",
|
|
230
|
-
);
|
|
231
|
-
if (String(id) !== String(expectedId))
|
|
232
|
-
fail(
|
|
233
|
-
"MESSAGE_IDENTITY_MISMATCH",
|
|
234
|
-
"polled message belongs to another message",
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
if (
|
|
238
|
-
response.current !== undefined &&
|
|
239
|
-
response.current !== null &&
|
|
240
|
-
!isRecord(response.current)
|
|
241
|
-
)
|
|
242
|
-
fail("INVALID_MESSAGE_RESPONSE", "polled message current state is invalid");
|
|
243
|
-
const current = response.current ?? {};
|
|
244
|
-
const failed =
|
|
245
|
-
Object.hasOwn(current, "error") &&
|
|
246
|
-
current.error !== null &&
|
|
247
|
-
current.error !== undefined;
|
|
248
|
-
if (
|
|
249
|
-
Object.hasOwn(current, "result") &&
|
|
250
|
-
current.result !== null &&
|
|
251
|
-
current.result !== undefined &&
|
|
252
|
-
!Array.isArray(current.result)
|
|
253
|
-
)
|
|
254
|
-
fail("INVALID_MESSAGE_RESPONSE", "polled message result is invalid");
|
|
255
|
-
const actions = Array.isArray(current.result) ? current.result : [];
|
|
256
|
-
if (failed) return { state: "failed", actions };
|
|
257
|
-
return actions.length ? { state: "completed", actions } : null;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function safeActionType(value) {
|
|
261
|
-
const normalized = requiredTrimmedString(value);
|
|
262
|
-
return normalized && SAFE_ACTION_TYPE.test(normalized) ? normalized : null;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function safeRouteIdentifier(value) {
|
|
266
|
-
const normalized = requiredTrimmedString(value);
|
|
267
|
-
return normalized &&
|
|
268
|
-
normalized.length <= 255 &&
|
|
269
|
-
SAFE_ROUTE_ID.test(normalized)
|
|
270
|
-
? normalized
|
|
271
|
-
: null;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function safeMessageId(value) {
|
|
275
|
-
if (Number.isSafeInteger(value) && value > 0) return value;
|
|
276
|
-
if (typeof value === "string" && /^[1-9]\d*$/u.test(value)) return value;
|
|
277
|
-
return null;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function safeTimestamp(value) {
|
|
281
|
-
if (typeof value !== "string" || !value.trim()) return null;
|
|
282
|
-
const milliseconds = Date.parse(value);
|
|
283
|
-
return Number.isFinite(milliseconds)
|
|
284
|
-
? new Date(milliseconds).toISOString()
|
|
285
|
-
: null;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function normalizePollOptions(options) {
|
|
289
|
-
if (!isRecord(options))
|
|
290
|
-
fail("INVALID_POLL_OPTIONS", "poll options must be an object");
|
|
291
|
-
const now = options.now ?? Date.now;
|
|
292
|
-
const sleep =
|
|
293
|
-
options.sleep ??
|
|
294
|
-
((milliseconds) =>
|
|
295
|
-
new Promise((resolve) => setTimeout(resolve, milliseconds)));
|
|
296
|
-
if (typeof now !== "function" || typeof sleep !== "function")
|
|
297
|
-
fail(
|
|
298
|
-
"INVALID_POLL_OPTIONS",
|
|
299
|
-
"polling clock and sleep implementations must be functions",
|
|
300
|
-
);
|
|
301
|
-
return {
|
|
302
|
-
now,
|
|
303
|
-
sleep,
|
|
304
|
-
pollIntervalMs: nonNegativeNumber(options.pollIntervalMs, 2000),
|
|
305
|
-
maxWaitMs: nonNegativeNumber(options.maxWaitMs, 600000),
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function readClock(now) {
|
|
310
|
-
const value = now();
|
|
311
|
-
const milliseconds =
|
|
312
|
-
value instanceof Date
|
|
313
|
-
? value.getTime()
|
|
314
|
-
: typeof value === "string"
|
|
315
|
-
? Date.parse(value)
|
|
316
|
-
: Number(value);
|
|
317
|
-
if (!Number.isFinite(milliseconds))
|
|
318
|
-
fail("INVALID_POLL_OPTIONS", "polling clock returned an invalid time");
|
|
319
|
-
return { milliseconds, iso: new Date(milliseconds).toISOString() };
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function nonNegativeNumber(value, fallback) {
|
|
323
|
-
const number = Number(value ?? fallback);
|
|
324
|
-
if (!Number.isFinite(number) || number < 0)
|
|
325
|
-
fail(
|
|
326
|
-
"INVALID_POLL_OPTIONS",
|
|
327
|
-
"polling bounds must be finite non-negative numbers",
|
|
328
|
-
);
|
|
329
|
-
return number;
|
|
330
|
-
}
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { actionInterpretations } from "./interpretation-pairs.mjs";
|
|
4
|
+
import {
|
|
5
|
+
canonicalPositiveInteger,
|
|
6
|
+
fail,
|
|
7
|
+
isRecord,
|
|
8
|
+
requireClient,
|
|
9
|
+
requiredTrimmedString,
|
|
10
|
+
safeOpaqueIdentifier,
|
|
11
|
+
uniquePairs,
|
|
12
|
+
} from "./shared.mjs";
|
|
13
|
+
|
|
14
|
+
const ALLOWED_INPUT_KEYS = new Set([
|
|
15
|
+
"caseId",
|
|
16
|
+
"question",
|
|
17
|
+
"sourceType",
|
|
18
|
+
"workspaceId",
|
|
19
|
+
]);
|
|
20
|
+
const ALLOWED_SOURCES = new Set([
|
|
21
|
+
"current_search_correction",
|
|
22
|
+
"reported_search_correction",
|
|
23
|
+
]);
|
|
24
|
+
const SAFE_OPAQUE_ID = /^[0-9A-Za-z._:+/-]+$/u;
|
|
25
|
+
const SAFE_ROUTE_ID = /^[0-9A-Za-z._:+-]+$/u;
|
|
26
|
+
const SAFE_ACTION_TYPE = /^[A-Za-z][0-9A-Za-z_.:-]{0,63}$/u;
|
|
27
|
+
|
|
28
|
+
export function normalizeReproductionInput(input) {
|
|
29
|
+
if (!isRecord(input))
|
|
30
|
+
fail("INVALID_REPRODUCTION_INPUT", "reproduction input must be an object");
|
|
31
|
+
const unknownKeys = Object.keys(input).filter(
|
|
32
|
+
(key) => !ALLOWED_INPUT_KEYS.has(key),
|
|
33
|
+
);
|
|
34
|
+
if (unknownKeys.length)
|
|
35
|
+
fail(
|
|
36
|
+
"INVALID_REPRODUCTION_INPUT",
|
|
37
|
+
"reproduction input contains unsupported fields",
|
|
38
|
+
);
|
|
39
|
+
if (!Number.isInteger(input.workspaceId) || input.workspaceId <= 0)
|
|
40
|
+
fail(
|
|
41
|
+
"INVALID_REPRODUCTION_INPUT",
|
|
42
|
+
"workspaceId must be a positive integer",
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const caseId = requiredTrimmedString(input.caseId);
|
|
46
|
+
const question = requiredTrimmedString(input.question);
|
|
47
|
+
const sourceType = requiredTrimmedString(input.sourceType);
|
|
48
|
+
if (!caseId || !question || !ALLOWED_SOURCES.has(sourceType))
|
|
49
|
+
fail(
|
|
50
|
+
"INVALID_REPRODUCTION_INPUT",
|
|
51
|
+
"caseId, question, and a supported correction source are required",
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
workspaceId: input.workspaceId,
|
|
56
|
+
caseId,
|
|
57
|
+
sourceType,
|
|
58
|
+
question,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function reproduceCase(client, input, options = {}) {
|
|
63
|
+
const normalized = normalizeReproductionInput(input);
|
|
64
|
+
requireClient(client);
|
|
65
|
+
const poll = normalizePollOptions(options);
|
|
66
|
+
const started = readClock(poll.now);
|
|
67
|
+
const deadline = started.milliseconds + poll.maxWaitMs;
|
|
68
|
+
const workspacePath = `/api/v3/account-projects/${normalized.workspaceId}`;
|
|
69
|
+
|
|
70
|
+
const admin = await client.request(
|
|
71
|
+
"GET",
|
|
72
|
+
`/api/v2/admin-projects/${normalized.workspaceId}/`,
|
|
73
|
+
);
|
|
74
|
+
validateAdminIdentity(admin, normalized.workspaceId);
|
|
75
|
+
|
|
76
|
+
const createdThread = await client.request(
|
|
77
|
+
"POST",
|
|
78
|
+
`${workspacePath}/threads/`,
|
|
79
|
+
{ body: {} },
|
|
80
|
+
);
|
|
81
|
+
const thread = normalizeThread(createdThread);
|
|
82
|
+
|
|
83
|
+
const cutResponse = await client.request("POST", `${workspacePath}/cut/`, {
|
|
84
|
+
body: { tokens: normalized.question },
|
|
85
|
+
});
|
|
86
|
+
const cut = normalizeCut(cutResponse);
|
|
87
|
+
|
|
88
|
+
const messageCollection = `${workspacePath}/threads/${thread.uid}/messages/`;
|
|
89
|
+
const createdMessage = await client.request("POST", messageCollection, {
|
|
90
|
+
body: { message: normalized.question },
|
|
91
|
+
});
|
|
92
|
+
const message = normalizeCreatedMessage(createdMessage);
|
|
93
|
+
const messagePath = `${messageCollection}${message.id}/`;
|
|
94
|
+
|
|
95
|
+
while (true) {
|
|
96
|
+
const response = await client.request("GET", messagePath);
|
|
97
|
+
const terminal = inspectPolledMessage(response, message.id);
|
|
98
|
+
if (terminal) {
|
|
99
|
+
const observed = readClock(poll.now);
|
|
100
|
+
return buildResult(
|
|
101
|
+
normalized,
|
|
102
|
+
started.iso,
|
|
103
|
+
observed.iso,
|
|
104
|
+
thread,
|
|
105
|
+
message,
|
|
106
|
+
cut,
|
|
107
|
+
terminal,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const current = readClock(poll.now);
|
|
112
|
+
const remainingMs = deadline - current.milliseconds;
|
|
113
|
+
if (remainingMs <= 0)
|
|
114
|
+
fail(
|
|
115
|
+
"REPRODUCTION_TIMEOUT",
|
|
116
|
+
"DataSeek reproduction did not reach a terminal result",
|
|
117
|
+
{
|
|
118
|
+
workspaceId: normalized.workspaceId,
|
|
119
|
+
state: "pending",
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
await poll.sleep(Math.min(poll.pollIntervalMs, remainingMs));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function buildResult(
|
|
127
|
+
input,
|
|
128
|
+
startedAt,
|
|
129
|
+
observedAt,
|
|
130
|
+
thread,
|
|
131
|
+
message,
|
|
132
|
+
cut,
|
|
133
|
+
terminal,
|
|
134
|
+
) {
|
|
135
|
+
const actions = terminal.actions;
|
|
136
|
+
return {
|
|
137
|
+
workspaceId: input.workspaceId,
|
|
138
|
+
caseId: input.caseId,
|
|
139
|
+
sourceType: input.sourceType,
|
|
140
|
+
questionHash: createHash("sha256").update(input.question).digest("hex"),
|
|
141
|
+
startedAt,
|
|
142
|
+
observedAt,
|
|
143
|
+
thread,
|
|
144
|
+
message: {
|
|
145
|
+
...message,
|
|
146
|
+
state: terminal.state,
|
|
147
|
+
actionTypes: actions
|
|
148
|
+
.map((action) => safeActionType(action?.type))
|
|
149
|
+
.filter(Boolean),
|
|
150
|
+
resultCount: actions.length,
|
|
151
|
+
interpretations:
|
|
152
|
+
terminal.state === "failed" ? [] : actionInterpretations(actions),
|
|
153
|
+
},
|
|
154
|
+
cut,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function validateAdminIdentity(response, workspaceId) {
|
|
159
|
+
if (!isRecord(response))
|
|
160
|
+
fail(
|
|
161
|
+
"INVALID_ADMIN_RESPONSE",
|
|
162
|
+
"administrator workspace response is invalid",
|
|
163
|
+
);
|
|
164
|
+
const responseId = canonicalPositiveInteger(response.id);
|
|
165
|
+
if (responseId === null)
|
|
166
|
+
fail(
|
|
167
|
+
"INVALID_ADMIN_RESPONSE",
|
|
168
|
+
"administrator workspace response is missing its identity",
|
|
169
|
+
);
|
|
170
|
+
if (responseId !== workspaceId)
|
|
171
|
+
fail(
|
|
172
|
+
"WORKSPACE_IDENTITY_MISMATCH",
|
|
173
|
+
"administrator response belongs to another workspace",
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function normalizeThread(response) {
|
|
178
|
+
if (!isRecord(response))
|
|
179
|
+
fail("INVALID_THREAD_RESPONSE", "new thread response is invalid");
|
|
180
|
+
const uid = safeRouteIdentifier(response.uid);
|
|
181
|
+
const createdAt = safeTimestamp(response.createdAt ?? response.created_at);
|
|
182
|
+
if (!uid || !createdAt)
|
|
183
|
+
fail(
|
|
184
|
+
"INVALID_THREAD_RESPONSE",
|
|
185
|
+
"new thread response is missing a safe identity or creation time",
|
|
186
|
+
);
|
|
187
|
+
return { uid, createdAt };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function normalizeCut(response) {
|
|
191
|
+
if (!isRecord(response) || !Array.isArray(response.tokens))
|
|
192
|
+
fail("INVALID_CUT_RESPONSE", "cut response is invalid");
|
|
193
|
+
const identity = safeOpaqueIdentifier(response.identity);
|
|
194
|
+
const momentIdentity = safeOpaqueIdentifier(response.momentIdentity);
|
|
195
|
+
if (!identity || !momentIdentity)
|
|
196
|
+
fail("INVALID_CUT_RESPONSE", "cut response is missing safe identities");
|
|
197
|
+
|
|
198
|
+
const pairs = [];
|
|
199
|
+
for (const token of response.tokens) {
|
|
200
|
+
if (!isRecord(token))
|
|
201
|
+
fail("INVALID_CUT_RESPONSE", "cut response contains an invalid token");
|
|
202
|
+
const word = requiredTrimmedString(token.word);
|
|
203
|
+
const prop = requiredTrimmedString(token.prop);
|
|
204
|
+
if (word && prop) pairs.push({ word, prop });
|
|
205
|
+
}
|
|
206
|
+
return { identity, momentIdentity, pairs: uniquePairs(pairs) };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function normalizeCreatedMessage(response) {
|
|
210
|
+
if (!isRecord(response))
|
|
211
|
+
fail("INVALID_MESSAGE_RESPONSE", "new message response is invalid");
|
|
212
|
+
const id = safeMessageId(response.id);
|
|
213
|
+
const createdAt = safeTimestamp(response.createdAt ?? response.created_at);
|
|
214
|
+
if (id === null || !createdAt)
|
|
215
|
+
fail(
|
|
216
|
+
"INVALID_MESSAGE_RESPONSE",
|
|
217
|
+
"new message response is missing a safe identity or creation time",
|
|
218
|
+
);
|
|
219
|
+
return { id, createdAt };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function inspectPolledMessage(response, expectedId) {
|
|
223
|
+
if (!isRecord(response))
|
|
224
|
+
fail("INVALID_MESSAGE_RESPONSE", "polled message response is invalid");
|
|
225
|
+
const id = safeMessageId(response.id);
|
|
226
|
+
if (id === null)
|
|
227
|
+
fail(
|
|
228
|
+
"INVALID_MESSAGE_RESPONSE",
|
|
229
|
+
"polled message response is missing its identity",
|
|
230
|
+
);
|
|
231
|
+
if (String(id) !== String(expectedId))
|
|
232
|
+
fail(
|
|
233
|
+
"MESSAGE_IDENTITY_MISMATCH",
|
|
234
|
+
"polled message belongs to another message",
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
if (
|
|
238
|
+
response.current !== undefined &&
|
|
239
|
+
response.current !== null &&
|
|
240
|
+
!isRecord(response.current)
|
|
241
|
+
)
|
|
242
|
+
fail("INVALID_MESSAGE_RESPONSE", "polled message current state is invalid");
|
|
243
|
+
const current = response.current ?? {};
|
|
244
|
+
const failed =
|
|
245
|
+
Object.hasOwn(current, "error") &&
|
|
246
|
+
current.error !== null &&
|
|
247
|
+
current.error !== undefined;
|
|
248
|
+
if (
|
|
249
|
+
Object.hasOwn(current, "result") &&
|
|
250
|
+
current.result !== null &&
|
|
251
|
+
current.result !== undefined &&
|
|
252
|
+
!Array.isArray(current.result)
|
|
253
|
+
)
|
|
254
|
+
fail("INVALID_MESSAGE_RESPONSE", "polled message result is invalid");
|
|
255
|
+
const actions = Array.isArray(current.result) ? current.result : [];
|
|
256
|
+
if (failed) return { state: "failed", actions };
|
|
257
|
+
return actions.length ? { state: "completed", actions } : null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function safeActionType(value) {
|
|
261
|
+
const normalized = requiredTrimmedString(value);
|
|
262
|
+
return normalized && SAFE_ACTION_TYPE.test(normalized) ? normalized : null;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function safeRouteIdentifier(value) {
|
|
266
|
+
const normalized = requiredTrimmedString(value);
|
|
267
|
+
return normalized &&
|
|
268
|
+
normalized.length <= 255 &&
|
|
269
|
+
SAFE_ROUTE_ID.test(normalized)
|
|
270
|
+
? normalized
|
|
271
|
+
: null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function safeMessageId(value) {
|
|
275
|
+
if (Number.isSafeInteger(value) && value > 0) return value;
|
|
276
|
+
if (typeof value === "string" && /^[1-9]\d*$/u.test(value)) return value;
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function safeTimestamp(value) {
|
|
281
|
+
if (typeof value !== "string" || !value.trim()) return null;
|
|
282
|
+
const milliseconds = Date.parse(value);
|
|
283
|
+
return Number.isFinite(milliseconds)
|
|
284
|
+
? new Date(milliseconds).toISOString()
|
|
285
|
+
: null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function normalizePollOptions(options) {
|
|
289
|
+
if (!isRecord(options))
|
|
290
|
+
fail("INVALID_POLL_OPTIONS", "poll options must be an object");
|
|
291
|
+
const now = options.now ?? Date.now;
|
|
292
|
+
const sleep =
|
|
293
|
+
options.sleep ??
|
|
294
|
+
((milliseconds) =>
|
|
295
|
+
new Promise((resolve) => setTimeout(resolve, milliseconds)));
|
|
296
|
+
if (typeof now !== "function" || typeof sleep !== "function")
|
|
297
|
+
fail(
|
|
298
|
+
"INVALID_POLL_OPTIONS",
|
|
299
|
+
"polling clock and sleep implementations must be functions",
|
|
300
|
+
);
|
|
301
|
+
return {
|
|
302
|
+
now,
|
|
303
|
+
sleep,
|
|
304
|
+
pollIntervalMs: nonNegativeNumber(options.pollIntervalMs, 2000),
|
|
305
|
+
maxWaitMs: nonNegativeNumber(options.maxWaitMs, 600000),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function readClock(now) {
|
|
310
|
+
const value = now();
|
|
311
|
+
const milliseconds =
|
|
312
|
+
value instanceof Date
|
|
313
|
+
? value.getTime()
|
|
314
|
+
: typeof value === "string"
|
|
315
|
+
? Date.parse(value)
|
|
316
|
+
: Number(value);
|
|
317
|
+
if (!Number.isFinite(milliseconds))
|
|
318
|
+
fail("INVALID_POLL_OPTIONS", "polling clock returned an invalid time");
|
|
319
|
+
return { milliseconds, iso: new Date(milliseconds).toISOString() };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function nonNegativeNumber(value, fallback) {
|
|
323
|
+
const number = Number(value ?? fallback);
|
|
324
|
+
if (!Number.isFinite(number) || number < 0)
|
|
325
|
+
fail(
|
|
326
|
+
"INVALID_POLL_OPTIONS",
|
|
327
|
+
"polling bounds must be finite non-negative numbers",
|
|
328
|
+
);
|
|
329
|
+
return number;
|
|
330
|
+
}
|