@promptbook/cli 0.114.0-50 → 0.114.0-51
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/apps/agents-server/README.md +2 -0
- package/apps/agents-server/src/app/AddAgentButton.tsx +4 -1
- package/apps/agents-server/src/app/dashboard/page.tsx +1 -0
- package/apps/agents-server/src/app/page.tsx +6 -7
- package/apps/agents-server/src/components/Header/Header.tsx +1 -0
- package/apps/agents-server/src/components/Header/useHeaderAgentMenus.tsx +5 -7
- package/apps/agents-server/src/components/Homepage/AgentsList.tsx +4 -1
- package/apps/agents-server/src/components/Homepage/AgentsListListView.tsx +4 -1
- package/apps/agents-server/src/components/Homepage/HomepagePrimarySections.tsx +4 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/ManGoNewAgentWizard.tsx +15 -7
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/README.md +36 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/EmailTestRun.tsx +25 -53
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/TestChat.tsx +23 -6
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/steps/BookStep.tsx +16 -36
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/steps/DoneStep.tsx +16 -55
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/steps/KnowledgeStep.tsx +19 -48
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/steps/TestStep.tsx +23 -88
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/components/steps/ZadaniStep.tsx +11 -2
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/services/agentEvalService.ts +9 -6
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/services/agentTestService.ts +11 -7
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/services/bookService.ts +15 -8
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/services/postManGoOnboardingJson.ts +2 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/services/uploadService.ts +2 -2
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/OnboardingProvider.tsx +24 -97
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/createInitialOnboardingState.ts +28 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/createOnboardingActions.ts +262 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/createOnboardingSession.ts +168 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/retireLegacyOnboardingSnapshot.ts +14 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/state/runOnboardingOperation.ts +29 -0
- package/apps/agents-server/src/components/NewAgentDialog/ManGoNewAgentWizard/types.ts +27 -2
- package/apps/agents-server/src/components/NewAgentDialog/useNewAgentDialog.tsx +178 -58
- package/apps/agents-server/tests/e2e/e2eEnvironment.cjs +3 -0
- package/esm/index.es.js +36 -11
- package/esm/index.es.js.map +1 -1
- package/esm/scripts/verify-prompts/verify-prompts.d.ts +5 -0
- package/esm/src/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/other/templates/getTemplatesPipelineCollection.ts +1133 -1530
- package/src/version.ts +2 -2
- package/src/versions.txt +1 -0
- package/umd/index.umd.js +36 -11
- package/umd/index.umd.js.map +1 -1
- package/umd/scripts/verify-prompts/verify-prompts.d.ts +5 -0
- package/umd/src/version.d.ts +1 -1
|
@@ -1,121 +1,48 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { createContext,
|
|
3
|
+
import { createContext, useContext, useMemo, useSyncExternalStore, type ReactNode } from 'react';
|
|
4
4
|
import { NotAllowed } from '../../../../../../../src/errors/NotAllowed';
|
|
5
|
+
import { createOnboardingActions } from './createOnboardingActions';
|
|
6
|
+
import type { OnboardingSession } from './createOnboardingSession';
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** sessionStorage key for the wizard session — exported so callers (e.g. the dashboard's
|
|
9
|
-
* "Nový onboarding") can clear a stale Book before starting fresh. */
|
|
10
|
-
export const ONBOARDING_STORAGE_KEY = 'onboarding:v2';
|
|
11
|
-
const STORAGE_KEY = ONBOARDING_STORAGE_KEY;
|
|
12
|
-
|
|
13
|
-
const INITIAL_STATE: OnboardingState = {
|
|
14
|
-
agentName: '',
|
|
15
|
-
agentBrief: '',
|
|
16
|
-
bookSource: '',
|
|
17
|
-
knowledge: [],
|
|
18
|
-
testMessages: [],
|
|
19
|
-
savedAgentId: null,
|
|
20
|
-
savedAgentTargetPath: null,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
type OnboardingPatch = Partial<OnboardingState> | ((previous: OnboardingState) => Partial<OnboardingState>);
|
|
24
|
-
|
|
8
|
+
/** Session-bound state/actions exposed to the wizard's step UI. */
|
|
25
9
|
type OnboardingContextValue = {
|
|
26
|
-
readonly state:
|
|
27
|
-
|
|
28
|
-
readonly
|
|
29
|
-
/**
|
|
30
|
-
* Shallow-merges a patch into the state and writes it through to `sessionStorage`.
|
|
31
|
-
* Accepts a function form for updates that depend on the latest state (e.g. mutating
|
|
32
|
-
* the knowledge list from several concurrent uploads).
|
|
33
|
-
*/
|
|
34
|
-
readonly update: (patch: OnboardingPatch) => void;
|
|
35
|
-
/** Clears the session (state + storage). */
|
|
10
|
+
readonly state: ReturnType<OnboardingSession['getState']>;
|
|
11
|
+
readonly update: OnboardingSession['update'];
|
|
12
|
+
readonly actions: ReturnType<typeof createOnboardingActions>;
|
|
36
13
|
readonly reset: () => void;
|
|
37
14
|
};
|
|
38
15
|
|
|
16
|
+
/** Context is mounted once per host-owned creation identity. */
|
|
39
17
|
const OnboardingContext = createContext<OnboardingContextValue | null>(null);
|
|
40
18
|
|
|
41
|
-
function readPersistedState(): OnboardingState | null {
|
|
42
|
-
if (typeof window === 'undefined') {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
const raw = window.sessionStorage.getItem(STORAGE_KEY);
|
|
48
|
-
return raw ? { ...INITIAL_STATE, ...(JSON.parse(raw) as Partial<OnboardingState>) } : null;
|
|
49
|
-
} catch {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function writePersistedState(state: OnboardingState): void {
|
|
55
|
-
if (typeof window === 'undefined') {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
61
|
-
} catch {
|
|
62
|
-
// Ignore storage failures (private mode, quota) — state still lives in memory.
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
19
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* Mounted by the `/onboarding` layout route so the state survives navigation between
|
|
70
|
-
* steps. Hydration from `sessionStorage` happens after the first client render to avoid
|
|
71
|
-
* an SSR/CSR mismatch; subsequent writes are write-through.
|
|
20
|
+
* Exposes only the explicitly supplied draft; there is no implicit last-draft hydration.
|
|
21
|
+
* Ownership and async work outlive step/provider remounts and end only through the host.
|
|
72
22
|
*/
|
|
73
|
-
export function OnboardingProvider({
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const update = useCallback((patch: OnboardingPatch) => {
|
|
86
|
-
setState((previous) => {
|
|
87
|
-
const resolved = typeof patch === 'function' ? patch(previous) : patch;
|
|
88
|
-
const next = { ...previous, ...resolved };
|
|
89
|
-
writePersistedState(next);
|
|
90
|
-
return next;
|
|
91
|
-
});
|
|
92
|
-
}, []);
|
|
93
|
-
|
|
94
|
-
const reset = useCallback(() => {
|
|
95
|
-
setState(INITIAL_STATE);
|
|
96
|
-
if (typeof window !== 'undefined') {
|
|
97
|
-
try {
|
|
98
|
-
window.sessionStorage.removeItem(STORAGE_KEY);
|
|
99
|
-
} catch {
|
|
100
|
-
// Ignore.
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}, []);
|
|
104
|
-
|
|
23
|
+
export function OnboardingProvider({
|
|
24
|
+
children,
|
|
25
|
+
session,
|
|
26
|
+
onRestart,
|
|
27
|
+
}: {
|
|
28
|
+
readonly children: ReactNode;
|
|
29
|
+
readonly session: OnboardingSession;
|
|
30
|
+
readonly onRestart: () => void;
|
|
31
|
+
}) {
|
|
32
|
+
const state = useSyncExternalStore(session.subscribe, session.getState, session.getState);
|
|
33
|
+
const actions = useMemo(() => createOnboardingActions(session), [session]);
|
|
105
34
|
const value = useMemo<OnboardingContextValue>(
|
|
106
|
-
() => ({ state,
|
|
107
|
-
[state,
|
|
35
|
+
() => ({ state, update: session.update, actions, reset: onRestart }),
|
|
36
|
+
[state, session, actions, onRestart],
|
|
108
37
|
);
|
|
109
|
-
|
|
110
38
|
return <OnboardingContext.Provider value={value}>{children}</OnboardingContext.Provider>;
|
|
111
39
|
}
|
|
112
40
|
|
|
41
|
+
/** Reads the current creation session, never a browser's previously saved onboarding. */
|
|
113
42
|
export function useOnboarding(): OnboardingContextValue {
|
|
114
43
|
const context = useContext(OnboardingContext);
|
|
115
|
-
|
|
116
44
|
if (!context) {
|
|
117
45
|
throw new NotAllowed('useOnboarding must be used within an <OnboardingProvider>.');
|
|
118
46
|
}
|
|
119
|
-
|
|
120
47
|
return context;
|
|
121
48
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { EMAIL_SCENARIOS } from '../config/emailScenarios';
|
|
2
|
+
import { ONBOARDING_ENTRY_PATH } from '../config/steps';
|
|
3
|
+
import type { OnboardingDraftState } from '../types';
|
|
4
|
+
|
|
5
|
+
/** Creates independent collections and UI state for each explicit new-agent request. */
|
|
6
|
+
export function createInitialOnboardingState(): OnboardingDraftState {
|
|
7
|
+
return {
|
|
8
|
+
agentName: '',
|
|
9
|
+
agentBrief: '',
|
|
10
|
+
bookSource: '',
|
|
11
|
+
bookAssignment: null,
|
|
12
|
+
bookGeneration: { phase: 'init', error: null },
|
|
13
|
+
knowledge: [],
|
|
14
|
+
knowledgeUrlInput: '',
|
|
15
|
+
knowledgeUrlError: null,
|
|
16
|
+
testMessages: [],
|
|
17
|
+
testMode: 'email',
|
|
18
|
+
testEmail: EMAIL_SCENARIOS[1].email,
|
|
19
|
+
testChatInput: '',
|
|
20
|
+
isSendingTestMessage: false,
|
|
21
|
+
emailTest: { phase: 'idle', reply: '', error: null, checks: null, isEvaluating: false },
|
|
22
|
+
isCreatingAgent: false,
|
|
23
|
+
creationError: null,
|
|
24
|
+
savedAgentId: null,
|
|
25
|
+
savedAgentTargetPath: null,
|
|
26
|
+
currentPath: ONBOARDING_ENTRY_PATH,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type { ManGoNewAgentWizardCreateRequest } from '../ManGoNewAgentWizard';
|
|
2
|
+
import type { ManGoCreatedAgentPayload } from '../components/steps/DoneStep';
|
|
3
|
+
import { createId } from '../lib/id';
|
|
4
|
+
import { evaluateReply } from '../services/agentEvalService';
|
|
5
|
+
import { agentTestService } from '../services/agentTestService';
|
|
6
|
+
import { generateBook } from '../services/bookService';
|
|
7
|
+
import { createManGoAgentSource } from '../services/createManGoAgentSource';
|
|
8
|
+
import { uploadKnowledgeFile } from '../services/uploadService';
|
|
9
|
+
import type { ChatMessage, KnowledgeFileItem } from '../types';
|
|
10
|
+
import type { OnboardingSession } from './createOnboardingSession';
|
|
11
|
+
import { runOnboardingOperation } from './runOnboardingOperation';
|
|
12
|
+
|
|
13
|
+
/** Creation service supplied by the existing shared new-agent controller. */
|
|
14
|
+
type CreateAgent = (request: ManGoNewAgentWizardCreateRequest) => Promise<ManGoCreatedAgentPayload>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Session-owned actions shared by the step UI. They read the latest draft at invocation,
|
|
18
|
+
* and no async callback retains permission to update a replacement draft or superseded request.
|
|
19
|
+
*/
|
|
20
|
+
export function createOnboardingActions(session: OnboardingSession) {
|
|
21
|
+
/** Generates through the existing model service; manual edits/input changes supersede its result. */
|
|
22
|
+
async function generate(): Promise<void> {
|
|
23
|
+
const state = session.getState();
|
|
24
|
+
if (!state.agentName.trim() || !state.agentBrief.trim()) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const operation = session.beginOperation('book');
|
|
28
|
+
if (!operation) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const assignment = { agentName: state.agentName, agentBrief: state.agentBrief };
|
|
32
|
+
session.update({ bookGeneration: { phase: 'generating', error: null } });
|
|
33
|
+
await runOnboardingOperation(
|
|
34
|
+
operation,
|
|
35
|
+
(signal) => generateBook(assignment, signal),
|
|
36
|
+
(bookSource) =>
|
|
37
|
+
session.update({
|
|
38
|
+
bookSource,
|
|
39
|
+
bookAssignment: assignment,
|
|
40
|
+
bookGeneration: { phase: 'ready', error: null },
|
|
41
|
+
}),
|
|
42
|
+
(error) =>
|
|
43
|
+
session.update({
|
|
44
|
+
bookGeneration: {
|
|
45
|
+
phase: 'error',
|
|
46
|
+
error: error instanceof Error ? error.message : 'Generování booku selhalo.',
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Keeps concurrent uploads independent, including uploads finishing after step navigation. */
|
|
53
|
+
async function uploadFile(file: File): Promise<void> {
|
|
54
|
+
const id = createId();
|
|
55
|
+
const operation = session.beginOperation(`upload:${id}`);
|
|
56
|
+
if (!operation) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const item: KnowledgeFileItem = {
|
|
60
|
+
kind: 'file',
|
|
61
|
+
id,
|
|
62
|
+
name: file.name,
|
|
63
|
+
size: file.size,
|
|
64
|
+
publicUrl: '',
|
|
65
|
+
objectKey: '',
|
|
66
|
+
status: 'uploading',
|
|
67
|
+
};
|
|
68
|
+
session.update((previous) => ({ knowledge: [...previous.knowledge, item] }));
|
|
69
|
+
await runOnboardingOperation(
|
|
70
|
+
operation,
|
|
71
|
+
(signal) => uploadKnowledgeFile(file, signal),
|
|
72
|
+
({ publicUrl, objectKey }) =>
|
|
73
|
+
session.update((previous) => ({
|
|
74
|
+
knowledge: previous.knowledge.map((entry) =>
|
|
75
|
+
entry.id === id ? { ...entry, publicUrl, objectKey, status: 'ready' } : entry,
|
|
76
|
+
),
|
|
77
|
+
})),
|
|
78
|
+
() =>
|
|
79
|
+
session.update((previous) => ({
|
|
80
|
+
knowledge: previous.knowledge.map((entry) =>
|
|
81
|
+
entry.id === id ? { ...entry, status: 'error' } : entry,
|
|
82
|
+
),
|
|
83
|
+
})),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Removes the association and cancels its pending upload; saved agents' objects are untouched. */
|
|
88
|
+
function removeKnowledge(id: string): void {
|
|
89
|
+
session.cancelOperation(`upload:${id}`);
|
|
90
|
+
session.update((previous) => ({ knowledge: previous.knowledge.filter((item) => item.id !== id) }));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Runs a conversation revision with only this draft's source, ready knowledge and messages. */
|
|
94
|
+
async function runChat(history: readonly ChatMessage[]): Promise<void> {
|
|
95
|
+
const operation = session.beginOperation('chat');
|
|
96
|
+
if (!operation) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const state = session.getState();
|
|
100
|
+
session.update({ testMessages: history, isSendingTestMessage: true });
|
|
101
|
+
await runOnboardingOperation(
|
|
102
|
+
operation,
|
|
103
|
+
(signal) =>
|
|
104
|
+
agentTestService.send(
|
|
105
|
+
{ bookSource: state.bookSource, knowledge: state.knowledge, messages: history },
|
|
106
|
+
signal,
|
|
107
|
+
),
|
|
108
|
+
(reply) =>
|
|
109
|
+
session.update((previous) => ({
|
|
110
|
+
testMessages: [...previous.testMessages, { id: createId(), role: 'agent', content: reply.content }],
|
|
111
|
+
})),
|
|
112
|
+
() =>
|
|
113
|
+
session.update((previous) => ({
|
|
114
|
+
testMessages: [
|
|
115
|
+
...previous.testMessages,
|
|
116
|
+
{
|
|
117
|
+
id: createId(),
|
|
118
|
+
role: 'agent',
|
|
119
|
+
content: 'Omlouvám se, něco se pokazilo. Zkuste to prosím znovu.',
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
})),
|
|
123
|
+
() => session.update({ isSendingTestMessage: false }),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Sends one deliberate test message, guarding duplicate clicks synchronously. */
|
|
128
|
+
function sendTestMessage(text: string): void {
|
|
129
|
+
const state = session.getState();
|
|
130
|
+
if (!text.trim() || state.isSendingTestMessage) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
void runChat([...state.testMessages, { id: createId(), role: 'user', content: text.trim() }]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Retries the last user turn while retaining the current draft's conversation. */
|
|
137
|
+
function retryTestMessage(): void {
|
|
138
|
+
const state = session.getState();
|
|
139
|
+
if (state.isSendingTestMessage) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const lastUserIndex = state.testMessages.map((message) => message.role).lastIndexOf('user');
|
|
143
|
+
if (lastUserIndex >= 0) {
|
|
144
|
+
void runChat(state.testMessages.slice(0, lastUserIndex + 1));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Stops both network work and acceptance of an already running response. */
|
|
149
|
+
function stopTestMessage(): void {
|
|
150
|
+
session.cancelOperation('chat');
|
|
151
|
+
session.update({ isSendingTestMessage: false });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Runs email generation and evaluation as one revision, preserving results across step navigation. */
|
|
155
|
+
async function runEmailTest(): Promise<void> {
|
|
156
|
+
const state = session.getState();
|
|
157
|
+
if (!state.testEmail.trim() || state.emailTest.phase === 'running') {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const operation = session.beginOperation('email');
|
|
161
|
+
if (!operation) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const customerEmail = state.testEmail.trim();
|
|
165
|
+
session.update({ emailTest: { phase: 'running', reply: '', error: null, checks: null, isEvaluating: false } });
|
|
166
|
+
await runOnboardingOperation(
|
|
167
|
+
operation,
|
|
168
|
+
async (signal) => {
|
|
169
|
+
const reply = await agentTestService.send(
|
|
170
|
+
{
|
|
171
|
+
bookSource: state.bookSource,
|
|
172
|
+
knowledge: state.knowledge,
|
|
173
|
+
messages: [{ id: createId(), role: 'user', content: customerEmail }],
|
|
174
|
+
},
|
|
175
|
+
signal,
|
|
176
|
+
);
|
|
177
|
+
if (!operation.isCurrent()) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
session.update({
|
|
181
|
+
emailTest: { phase: 'done', reply: reply.content, error: null, checks: null, isEvaluating: true },
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
const checks = await evaluateReply(
|
|
185
|
+
{ bookSource: state.bookSource, customerEmail, reply: reply.content },
|
|
186
|
+
signal,
|
|
187
|
+
);
|
|
188
|
+
if (operation.isCurrent()) {
|
|
189
|
+
session.update((previous) => ({ emailTest: { ...previous.emailTest, checks } }));
|
|
190
|
+
}
|
|
191
|
+
} catch {
|
|
192
|
+
if (operation.isCurrent()) {
|
|
193
|
+
session.update((previous) => ({ emailTest: { ...previous.emailTest, checks: [] } }));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
() => undefined,
|
|
198
|
+
(error) =>
|
|
199
|
+
session.update((previous) => ({
|
|
200
|
+
emailTest: {
|
|
201
|
+
...previous.emailTest,
|
|
202
|
+
phase: 'error',
|
|
203
|
+
error: error instanceof Error ? error.message : 'Testovací běh selhal.',
|
|
204
|
+
},
|
|
205
|
+
})),
|
|
206
|
+
() => session.update((previous) => ({ emailTest: { ...previous.emailTest, isEvaluating: false } })),
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Saves once per draft even across remounts/effect re-execution. A failed draft requires an
|
|
212
|
+
* explicit retry. Server success remains associated with this operation even after cancellation.
|
|
213
|
+
*/
|
|
214
|
+
async function createAgent(
|
|
215
|
+
onCreate: CreateAgent,
|
|
216
|
+
visibility: ManGoNewAgentWizardCreateRequest['visibility'],
|
|
217
|
+
isRetry = false,
|
|
218
|
+
): Promise<void> {
|
|
219
|
+
const state = session.getState();
|
|
220
|
+
if (
|
|
221
|
+
state.savedAgentId ||
|
|
222
|
+
session.isOperationPending('creation') ||
|
|
223
|
+
(state.creationError && !isRetry) ||
|
|
224
|
+
(!state.agentName.trim() && !state.bookSource.trim())
|
|
225
|
+
) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const operation = session.beginOperation('creation');
|
|
229
|
+
if (!operation) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
session.update({ isCreatingAgent: true, creationError: null });
|
|
233
|
+
try {
|
|
234
|
+
const agent = await onCreate({
|
|
235
|
+
agentSource: createManGoAgentSource(state),
|
|
236
|
+
visibility,
|
|
237
|
+
knowledgeCount: state.knowledge.filter((item) => item.status === 'ready').length,
|
|
238
|
+
});
|
|
239
|
+
session.complete(agent);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
if (operation.isCurrent()) {
|
|
242
|
+
session.update({ creationError: error instanceof Error ? error.message : 'Vytvoření agenta selhalo.' });
|
|
243
|
+
}
|
|
244
|
+
} finally {
|
|
245
|
+
if (operation.isCurrent()) {
|
|
246
|
+
session.update({ isCreatingAgent: false });
|
|
247
|
+
}
|
|
248
|
+
operation.finish();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
generate,
|
|
254
|
+
uploadFile,
|
|
255
|
+
removeKnowledge,
|
|
256
|
+
sendTestMessage,
|
|
257
|
+
retryTestMessage,
|
|
258
|
+
stopTestMessage,
|
|
259
|
+
runEmailTest,
|
|
260
|
+
createAgent,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { createId } from '../lib/id';
|
|
2
|
+
import type { OnboardingDraftState } from '../types';
|
|
3
|
+
import { createInitialOnboardingState } from './createInitialOnboardingState';
|
|
4
|
+
|
|
5
|
+
/** Atomic update owned by one draft, including concurrent upload updates. */
|
|
6
|
+
export type OnboardingPatch =
|
|
7
|
+
| Partial<OnboardingDraftState>
|
|
8
|
+
| ((previous: OnboardingDraftState) => Partial<OnboardingDraftState>);
|
|
9
|
+
|
|
10
|
+
/** Cancellation and revision guard for one operation within one session. */
|
|
11
|
+
export type OnboardingOperation = {
|
|
12
|
+
readonly signal: AbortSignal;
|
|
13
|
+
readonly isCurrent: () => boolean;
|
|
14
|
+
readonly cancel: () => void;
|
|
15
|
+
readonly finish: () => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** A controller-owned lifetime and provider-owned draft; never shared across new-agent requests. */
|
|
19
|
+
export type OnboardingSession = ReturnType<typeof createOnboardingSession>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Starts a fresh session before mounting the provider. The store survives provider/step remounts,
|
|
23
|
+
* so Strict Mode and revisiting completion cannot submit an in-flight creation twice.
|
|
24
|
+
* Host exits invalidate synchronously, before React unmounts or late callbacks can run.
|
|
25
|
+
*/
|
|
26
|
+
export function createOnboardingSession(scope?: string) {
|
|
27
|
+
const id = createId();
|
|
28
|
+
let state = createInitialOnboardingState();
|
|
29
|
+
let isActive = true;
|
|
30
|
+
let isDraftActive = true;
|
|
31
|
+
let isCompleted = false;
|
|
32
|
+
const listeners = new Set<() => void>();
|
|
33
|
+
const operations = new Map<string, OnboardingOperation>();
|
|
34
|
+
|
|
35
|
+
/** Cancels only this session's named operation, leaving concurrent uploads intact. */
|
|
36
|
+
function cancelOperation(key: string): void {
|
|
37
|
+
operations.get(key)?.cancel();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Publishes one synchronous state transition to mounted providers. */
|
|
41
|
+
function publish(nextState: OnboardingDraftState): void {
|
|
42
|
+
state = nextState;
|
|
43
|
+
if (isActive) {
|
|
44
|
+
listeners.forEach((listener) => listener());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Accepts changes only while this draft is still owned by its host. */
|
|
49
|
+
function update(patch: OnboardingPatch): void {
|
|
50
|
+
if (!isActive || !isDraftActive) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const resolved = typeof patch === 'function' ? patch(state) : patch;
|
|
54
|
+
if (isCompleted) {
|
|
55
|
+
// Completion remains navigable, but cannot accept further draft edits or results.
|
|
56
|
+
if (resolved.currentPath) publish({ ...state, currentPath: resolved.currentPath });
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
let nextState = { ...state, ...resolved };
|
|
60
|
+
const isAssignmentChanged =
|
|
61
|
+
nextState.agentName !== state.agentName || nextState.agentBrief !== state.agentBrief;
|
|
62
|
+
const isBookChanged = nextState.bookSource !== state.bookSource;
|
|
63
|
+
if (isAssignmentChanged || isBookChanged) {
|
|
64
|
+
cancelOperation('book');
|
|
65
|
+
if (nextState.bookGeneration.phase === 'generating') {
|
|
66
|
+
nextState = { ...nextState, bookGeneration: { phase: 'ready', error: null } };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (isBookChanged || nextState.knowledge !== state.knowledge) {
|
|
70
|
+
cancelOperation('chat');
|
|
71
|
+
cancelOperation('email');
|
|
72
|
+
nextState = {
|
|
73
|
+
...nextState,
|
|
74
|
+
isSendingTestMessage: false,
|
|
75
|
+
emailTest: { ...nextState.emailTest, isEvaluating: false, phase: 'idle' },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (nextState.testEmail !== state.testEmail) {
|
|
79
|
+
cancelOperation('email');
|
|
80
|
+
nextState = { ...nextState, emailTest: { ...nextState.emailTest, phase: 'idle', isEvaluating: false } };
|
|
81
|
+
}
|
|
82
|
+
publish(nextState);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Starts a request revision; acceptance remains guarded even if abort cannot stop the server. */
|
|
86
|
+
function beginOperation(key: string): OnboardingOperation | null {
|
|
87
|
+
if (!isActive || isCompleted || (!isDraftActive && key !== 'creation')) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
cancelOperation(key);
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
const operation: OnboardingOperation = {
|
|
93
|
+
signal: controller.signal,
|
|
94
|
+
isCurrent: () => isActive && !isCompleted && operations.get(key) === operation,
|
|
95
|
+
cancel: () => {
|
|
96
|
+
controller.abort();
|
|
97
|
+
operation.finish();
|
|
98
|
+
},
|
|
99
|
+
finish: () => {
|
|
100
|
+
if (operations.get(key) === operation) {
|
|
101
|
+
operations.delete(key);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
operations.set(key, operation);
|
|
106
|
+
return operation;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Ends eligibility for updates, without deleting uploaded objects or already created agents. */
|
|
110
|
+
function end(): void {
|
|
111
|
+
isActive = false;
|
|
112
|
+
isDraftActive = false;
|
|
113
|
+
operations.forEach((operation) => operation.cancel());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Transfers the composed Book to the classic editor within the same creation lifetime.
|
|
118
|
+
* Pending draft work loses ownership, but an already submitted save must not be duplicated.
|
|
119
|
+
*/
|
|
120
|
+
function handoff(): void {
|
|
121
|
+
isDraftActive = false;
|
|
122
|
+
operations.forEach((operation, key) => {
|
|
123
|
+
if (key !== 'creation') operation.cancel();
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Associates even a late server success with its original operation. The ended store never
|
|
129
|
+
* notifies a replacement dialog, and this identity prevents retrying a known successful save.
|
|
130
|
+
*/
|
|
131
|
+
function complete(agent: { readonly permanentId: string; readonly targetPath: string }): void {
|
|
132
|
+
if (isCompleted) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
isCompleted = true;
|
|
136
|
+
operations.forEach((operation) => operation.cancel());
|
|
137
|
+
publish({
|
|
138
|
+
...state,
|
|
139
|
+
savedAgentId: agent.permanentId,
|
|
140
|
+
savedAgentTargetPath: agent.targetPath,
|
|
141
|
+
isCreatingAgent: false,
|
|
142
|
+
creationError: null,
|
|
143
|
+
isSendingTestMessage: false,
|
|
144
|
+
bookGeneration: { phase: 'ready', error: null },
|
|
145
|
+
emailTest: { ...state.emailTest, isEvaluating: false },
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
id,
|
|
151
|
+
scope,
|
|
152
|
+
getState: () => state,
|
|
153
|
+
isActive: () => isActive,
|
|
154
|
+
isOperationPending: (key: string) => operations.has(key),
|
|
155
|
+
subscribe: (listener: () => void) => {
|
|
156
|
+
listeners.add(listener);
|
|
157
|
+
return () => {
|
|
158
|
+
listeners.delete(listener);
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
update,
|
|
162
|
+
beginOperation,
|
|
163
|
+
cancelOperation,
|
|
164
|
+
complete,
|
|
165
|
+
handoff,
|
|
166
|
+
end,
|
|
167
|
+
} as const;
|
|
168
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Obsolete, unscoped snapshot owned exclusively by the imported manGo wizard. */
|
|
2
|
+
const LEGACY_ONBOARDING_STORAGE_KEY = 'onboarding:v2';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Retires only the obsolete wizard entry. It is never read or imported into a draft.
|
|
6
|
+
* No storage access is required for creation, including private mode or exhausted quotas.
|
|
7
|
+
*/
|
|
8
|
+
export function retireLegacyOnboardingSnapshot(): void {
|
|
9
|
+
try {
|
|
10
|
+
window.sessionStorage.removeItem(LEGACY_ONBOARDING_STORAGE_KEY);
|
|
11
|
+
} catch {
|
|
12
|
+
// An unavailable store cannot compromise the independent in-memory session.
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { OnboardingOperation } from './createOnboardingSession';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accepts success, failure and settlement only for the current request revision.
|
|
5
|
+
* Abort is an optimization; the ownership check also handles uncancellable responses.
|
|
6
|
+
*/
|
|
7
|
+
export async function runOnboardingOperation<Result>(
|
|
8
|
+
operation: OnboardingOperation,
|
|
9
|
+
work: (signal: AbortSignal) => Promise<Result>,
|
|
10
|
+
onSuccess: (result: Result) => void,
|
|
11
|
+
onError: (error: unknown) => void,
|
|
12
|
+
onSettled?: () => void,
|
|
13
|
+
): Promise<void> {
|
|
14
|
+
try {
|
|
15
|
+
const result = await work(operation.signal);
|
|
16
|
+
if (operation.isCurrent()) {
|
|
17
|
+
onSuccess(result);
|
|
18
|
+
}
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (operation.isCurrent()) {
|
|
21
|
+
onError(error);
|
|
22
|
+
}
|
|
23
|
+
} finally {
|
|
24
|
+
if (operation.isCurrent()) {
|
|
25
|
+
onSettled?.();
|
|
26
|
+
}
|
|
27
|
+
operation.finish();
|
|
28
|
+
}
|
|
29
|
+
}
|