feat(import): complete OpenAPI onboarding and verify delivery
This commit is contained in:
1 parent
bb3e376767
commit
e93de7015a
244 files changed
+57020
-5139
No files matched your search
+243
-1
@@ -12,6 +12,7 @@
|
||||
refreshPromise: null,
|
||||
refreshQueued: false,
|
||||
startedRequested: false,
|
||||
importError: null,
|
||||
};
|
||||
var channel = null;
|
||||
var trigger = null;
|
||||
@@ -79,7 +80,7 @@
|
||||
function actionLabel(id) { return tKey('onboarding.action.' + id); }
|
||||
function deepLink(step) {
|
||||
var snapshot = state.snapshot || {};
|
||||
var returnTo = encodeURIComponent(window.location.pathname + window.location.search);
|
||||
var returnTo = encodeURIComponent(boundedReturnContext());
|
||||
if (step.id === 'operation') return '/wizard/?onboarding=1&return=' + returnTo;
|
||||
if (step.id === 'test' || step.id === 'publish_operation') {
|
||||
if (!snapshot.operation_id) return '/wizard/?onboarding=1&return=' + returnTo;
|
||||
@@ -100,6 +101,220 @@
|
||||
return '/logs?agent_id=' + encodeURIComponent(snapshot.agent_id || '');
|
||||
}
|
||||
function navigate(step) { window.location.href = deepLink(step); }
|
||||
function safeReturnContext(raw) {
|
||||
if (typeof raw !== 'string' || raw.charAt(0) !== '/' || raw.indexOf('//') === 0
|
||||
|| /[\u0000-\u001f\\]/.test(raw) || raw.length > 2048) return null;
|
||||
var url;
|
||||
try { url = new URL(raw, window.location.origin); } catch (_error) { return null; }
|
||||
if (url.origin !== window.location.origin) return null;
|
||||
var allowed = {
|
||||
'/': [],
|
||||
'/agents': ['onboarding', 'action', 'operationId', 'operationVersion'],
|
||||
'/api-keys': ['onboarding', 'action', 'agentId', 'agentRevision'],
|
||||
'/logs': ['log_id', 'agent_id', 'operation_id'],
|
||||
'/secrets': [],
|
||||
'/settings': [],
|
||||
'/usage': [],
|
||||
'/workspace-setup': [],
|
||||
'/wizard/': ['mode', 'operationId', 'step', 'onboarding'],
|
||||
};
|
||||
var keys = allowed[url.pathname];
|
||||
if (!keys) return null;
|
||||
var safe = new URLSearchParams();
|
||||
url.searchParams.forEach(function(value, key) {
|
||||
if (keys.indexOf(key) < 0) return;
|
||||
if (key === 'onboarding' && value === '1') safe.set(key, value);
|
||||
else if (key === 'action' && value === 'create') safe.set(key, value);
|
||||
else if (key === 'mode' && (value === 'create' || value === 'edit')) safe.set(key, value);
|
||||
else if ((key === 'operationVersion' || key === 'agentRevision' || key === 'step')
|
||||
&& /^[0-9]{1,16}$/.test(value)) safe.set(key, value);
|
||||
else if (/^(?:operationId|agentId|log_id|agent_id|operation_id)$/.test(key)
|
||||
&& /^[A-Za-z0-9_-]{1,128}$/.test(value)) safe.set(key, value);
|
||||
});
|
||||
var context = url.pathname + (safe.size ? '?' + safe.toString() : '');
|
||||
return context.length <= 256 ? context : url.pathname;
|
||||
}
|
||||
function boundedReturnContext() {
|
||||
return safeReturnContext(window.location.pathname + window.location.search) || '/';
|
||||
}
|
||||
function returnContextFromQuery() {
|
||||
var candidate = new URLSearchParams(window.location.search).get('return');
|
||||
return safeReturnContext(candidate) || boundedReturnContext();
|
||||
}
|
||||
function openOpenApiPath() {
|
||||
if (!window.CrankOpenApiImport || typeof window.CrankOpenApiImport.open !== 'function') {
|
||||
window.location.href = '/?onboarding_openapi=1&return=' + encodeURIComponent(boundedReturnContext());
|
||||
return;
|
||||
}
|
||||
window.CrankOpenApiImport.open({
|
||||
workspaceId: state.workspaceId,
|
||||
onboarding: true,
|
||||
returnContext: returnContextFromQuery(),
|
||||
importContext: state.snapshot && state.snapshot.import_context,
|
||||
onImported: function(identity) { return registerImportedApplication(identity); },
|
||||
onSelectDraft: function(operationId, version, identity) { return selectImportedDraft(operationId, version, identity); },
|
||||
});
|
||||
}
|
||||
async function registerImportedApplication(identity) {
|
||||
var workspaceId = state.workspaceId;
|
||||
var epoch = state.epoch;
|
||||
if (!identity || !identity.applicationIdempotencyKey || !identity.planDigest
|
||||
|| !window.CrankApi || typeof window.CrankApi.registerOnboardingImportContext !== 'function') {
|
||||
throw new Error(tKey('onboarding.import.registration_failed'));
|
||||
}
|
||||
try {
|
||||
var authoritative = await window.CrankApi.getOnboarding(workspaceId);
|
||||
if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) {
|
||||
throw new Error(tKey('onboarding.import.registration_failed'));
|
||||
}
|
||||
state.snapshot = authoritative;
|
||||
render();
|
||||
var response = await window.CrankApi.registerOnboardingImportContext(workspaceId, {
|
||||
application_idempotency_key: identity.applicationIdempotencyKey,
|
||||
plan_digest: identity.planDigest,
|
||||
expected_revision: Number(authoritative && authoritative.revision || 0),
|
||||
});
|
||||
if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) {
|
||||
throw new Error(tKey('onboarding.import.registration_failed'));
|
||||
}
|
||||
var current = response && response.onboarding ? response.onboarding : response;
|
||||
if (!current || !current.import_context) throw new Error(tKey('onboarding.import.registration_failed'));
|
||||
state.snapshot = current;
|
||||
render();
|
||||
return current;
|
||||
} catch (error) {
|
||||
if (epoch === state.epoch && workspaceId === state.workspaceId) {
|
||||
await refresh();
|
||||
var recovered = state.snapshot && state.snapshot.import_context;
|
||||
if (recovered
|
||||
&& recovered.application_idempotency_key === identity.applicationIdempotencyKey
|
||||
&& recovered.plan_digest === identity.planDigest) return state.snapshot;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async function selectImportedDraft(operationId, version, identity) {
|
||||
if (!operationId || !window.CrankApi || typeof window.CrankApi.selectOnboardingImportDraft !== 'function') return null;
|
||||
var workspaceId = state.workspaceId;
|
||||
var epoch = state.epoch;
|
||||
var context = state.snapshot && state.snapshot.import_context;
|
||||
var applicationKey = identity && identity.applicationIdempotencyKey || (context && context.application_idempotency_key);
|
||||
var digest = identity && identity.planDigest || (context && context.plan_digest);
|
||||
if (!applicationKey || !digest || !Number.isInteger(version) || version < 1) {
|
||||
throw new Error(tKey('onboarding.import.selection_failed'));
|
||||
}
|
||||
try {
|
||||
var response = await window.CrankApi.selectOnboardingImportDraft(workspaceId, {
|
||||
application_idempotency_key: applicationKey,
|
||||
plan_digest: digest,
|
||||
operation_id: operationId,
|
||||
operation_version: version,
|
||||
expected_revision: Number(state.snapshot && state.snapshot.revision || 0),
|
||||
});
|
||||
var current = response && response.onboarding ? response.onboarding : response;
|
||||
if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) {
|
||||
throw new Error(tKey('onboarding.import.selection_failed'));
|
||||
}
|
||||
if (!current || !current.import_context
|
||||
|| current.import_context.selected_operation_id !== operationId
|
||||
|| Number(current.import_context.selected_operation_version) !== version) {
|
||||
throw new Error(tKey('onboarding.import.selection_failed'));
|
||||
}
|
||||
if (current && typeof current === 'object' && 'revision' in current) {
|
||||
state.snapshot = current;
|
||||
render();
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error && error.status === 409 && epoch === state.epoch && workspaceId === state.workspaceId) {
|
||||
await refresh();
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
function renderOpenApiPath() {
|
||||
var section = document.createElement('div');
|
||||
section.className = 'onboarding-openapi-path';
|
||||
var hint = document.createElement('p');
|
||||
hint.className = 'onboarding-openapi-hint';
|
||||
hint.textContent = tKey('onboarding.openapi_hint');
|
||||
var openButton = button(tKey('onboarding.openapi_path'), 'onboarding-openapi-path', openOpenApiPath, 'onboarding-step-action');
|
||||
section.appendChild(hint);
|
||||
section.appendChild(openButton);
|
||||
return section;
|
||||
}
|
||||
async function continueWithImportedDraft(choice) {
|
||||
var context = state.snapshot && state.snapshot.import_context;
|
||||
if (!context || !choice || choice.selectable !== true) return;
|
||||
state.importError = null;
|
||||
render();
|
||||
try {
|
||||
await selectImportedDraft(choice.operation_id, choice.version, null);
|
||||
window.location.href = '/wizard/?mode=edit&operationId=' + encodeURIComponent(choice.operation_id)
|
||||
+ '&operationVersion=' + encodeURIComponent(choice.version) + '&onboarding=1'
|
||||
+ '&return=' + encodeURIComponent(boundedReturnContext());
|
||||
} catch (_error) {
|
||||
state.importError = tKey('onboarding.import.selection_failed');
|
||||
render();
|
||||
}
|
||||
}
|
||||
function renderImportContext() {
|
||||
var context = state.snapshot && state.snapshot.import_context;
|
||||
if (!context) return null;
|
||||
var section = document.createElement('section');
|
||||
section.className = 'onboarding-import-report';
|
||||
section.dataset.testid = 'onboarding-import-context';
|
||||
var title = document.createElement('h3');
|
||||
title.textContent = tKey('onboarding.import.report_title');
|
||||
section.appendChild(title);
|
||||
if (context.drift) {
|
||||
var drift = document.createElement('p');
|
||||
drift.className = 'onboarding-import-drift';
|
||||
drift.setAttribute('role', 'alert');
|
||||
drift.textContent = tKey('onboarding.import.drift');
|
||||
section.appendChild(drift);
|
||||
}
|
||||
if (state.importError) {
|
||||
var error = document.createElement('p');
|
||||
error.className = 'onboarding-import-error';
|
||||
error.setAttribute('role', 'alert');
|
||||
error.textContent = state.importError;
|
||||
section.appendChild(error);
|
||||
}
|
||||
var choices = Array.isArray(context.choices) ? context.choices : [];
|
||||
function outcomeLabel(outcome) {
|
||||
var key = 'openapi.plan.outcome.' + String(outcome || '');
|
||||
return tKey(key) === key ? tKey('openapi.plan.outcome.rejected') : tKey(key);
|
||||
}
|
||||
function reasonLabel(reason) {
|
||||
if (!reason) return '';
|
||||
return window.CrankOpenApiImport && typeof window.CrankOpenApiImport.describeFinding === 'function'
|
||||
? window.CrankOpenApiImport.describeFinding(reason)
|
||||
: tKey('openapi.finding.generic');
|
||||
}
|
||||
var list = document.createElement('ul');
|
||||
list.className = 'onboarding-import-report-list';
|
||||
choices.forEach(function(choice) {
|
||||
var item = document.createElement('li');
|
||||
item.className = 'onboarding-import-report-item';
|
||||
var summary = document.createElement('span');
|
||||
summary.textContent = [choice.name || choice.key || '', outcomeLabel(choice.outcome), choice.version ? 'v' + choice.version : '', reasonLabel(choice.reason)]
|
||||
.filter(Boolean).join(' · ');
|
||||
item.appendChild(summary);
|
||||
if (choice.selectable === true && (!context.selected_operation_id || context.drift)) {
|
||||
item.appendChild(button(tKey('onboarding.import.continue'), null, function() { continueWithImportedDraft(choice); }, 'onboarding-step-action'));
|
||||
} else if (choice.operation_id === context.selected_operation_id && Number(choice.version) === Number(context.selected_operation_version)) {
|
||||
var selected = document.createElement('span');
|
||||
selected.className = 'onboarding-import-selected';
|
||||
selected.textContent = tKey('onboarding.import.selected');
|
||||
item.appendChild(selected);
|
||||
}
|
||||
list.appendChild(item);
|
||||
});
|
||||
section.appendChild(list);
|
||||
section.appendChild(button(tKey('onboarding.import.view_report'), null, openOpenApiPath, 'onboarding-link-button'));
|
||||
return section;
|
||||
}
|
||||
function recordStarted() {
|
||||
if (state.startedRequested || !state.snapshot || state.snapshot.completed) return;
|
||||
state.startedRequested = true;
|
||||
@@ -240,6 +455,17 @@
|
||||
});
|
||||
body.appendChild(list);
|
||||
}
|
||||
var returnContext = document.createElement('span');
|
||||
returnContext.className = 'onboarding-return-context';
|
||||
returnContext.dataset.testid = 'onboarding-return-context';
|
||||
returnContext.hidden = true;
|
||||
returnContext.textContent = returnContextFromQuery();
|
||||
body.appendChild(returnContext);
|
||||
if (!state.snapshot.completed) {
|
||||
body.appendChild(renderOpenApiPath());
|
||||
}
|
||||
var importReport = renderImportContext();
|
||||
if (importReport) body.appendChild(importReport);
|
||||
var footer = document.createElement('div');
|
||||
footer.className = 'onboarding-footer';
|
||||
footer.appendChild(button(tKey('onboarding.refresh'), 'onboarding-refresh', refresh, 'onboarding-link-button'));
|
||||
@@ -295,6 +521,7 @@
|
||||
var snapshot = await window.CrankApi.getOnboarding(workspaceId);
|
||||
if (epoch !== state.epoch || workspaceId !== (currentWorkspace() && currentWorkspace().id)) return;
|
||||
state.snapshot = snapshot;
|
||||
state.importError = null;
|
||||
var preference = safePreference();
|
||||
if (window.location.pathname === '/'
|
||||
&& !preference.opened
|
||||
@@ -304,6 +531,21 @@
|
||||
setOpen(true);
|
||||
}
|
||||
renderDeepLinkRecovery(snapshot);
|
||||
if (window.location.pathname === '/' && new URLSearchParams(window.location.search).get('onboarding_openapi') === '1'
|
||||
&& window.CrankOpenApiImport && typeof window.CrankOpenApiImport.open === 'function') {
|
||||
var openUrl = new URL(window.location.href);
|
||||
openUrl.searchParams.delete('onboarding_openapi');
|
||||
window.history.replaceState(window.history.state, '', openUrl.pathname + openUrl.search + openUrl.hash);
|
||||
window.CrankOpenApiImport.open({
|
||||
workspaceId: workspaceId,
|
||||
onboarding: true,
|
||||
returnContext: returnContextFromQuery(),
|
||||
importContext: snapshot.import_context || null,
|
||||
onImported: function(identity) { return registerImportedApplication(identity); },
|
||||
onSelectDraft: function(operationId, version, identity) { return selectImportedDraft(operationId, version, identity); },
|
||||
});
|
||||
}
|
||||
return snapshot;
|
||||
} catch (error) {
|
||||
if (epoch !== state.epoch) return;
|
||||
state.error = error;
|
||||
|
||||
Reference in new issue
Block a user