@carlonicora/nextjs-jsonapi 1.112.1 → 1.114.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{AssistantInterface-B1c8FhGA.d.mts → AssistantInterface-B56qznuH.d.mts} +2 -0
- package/dist/{AssistantInterface-BBUHxOCd.d.ts → AssistantInterface-CHiJIYDY.d.ts} +2 -0
- package/dist/{AssistantMessageInterface-DrgHDjZ5.d.mts → AssistantMessageInterface-BuUZFkE5.d.mts} +16 -1
- package/dist/{AssistantMessageInterface-BNzf1etU.d.ts → AssistantMessageInterface-CQwh5tEb.d.ts} +16 -1
- package/dist/{BlockNoteEditor-HNQNKCFP.js → BlockNoteEditor-DT5L5AAB.js} +19 -19
- package/dist/{BlockNoteEditor-HNQNKCFP.js.map → BlockNoteEditor-DT5L5AAB.js.map} +1 -1
- package/dist/{BlockNoteEditor-A5LNFWHW.mjs → BlockNoteEditor-HIEAHXR4.mjs} +4 -4
- package/dist/billing/index.js +346 -346
- package/dist/billing/index.mjs +3 -3
- package/dist/{chunk-6XWW67F2.js → chunk-4BUZUKER.js} +7 -7
- package/dist/{chunk-6XWW67F2.js.map → chunk-4BUZUKER.js.map} +1 -1
- package/dist/{chunk-U2HR57MY.js → chunk-7EQ46GUL.js} +76 -25
- package/dist/chunk-7EQ46GUL.js.map +1 -0
- package/dist/{chunk-CWVYU4YZ.mjs → chunk-DJSH6VHF.mjs} +2 -2
- package/dist/{chunk-ZYPNVEHB.mjs → chunk-FBTTIGLL.mjs} +53 -2
- package/dist/{chunk-ZYPNVEHB.mjs.map → chunk-FBTTIGLL.mjs.map} +1 -1
- package/dist/{chunk-LYLGU2BK.mjs → chunk-TVOW47AP.mjs} +3011 -2853
- package/dist/chunk-TVOW47AP.mjs.map +1 -0
- package/dist/{chunk-DRJUN4R6.js → chunk-VA2KFKHD.js} +1080 -922
- package/dist/chunk-VA2KFKHD.js.map +1 -0
- package/dist/client/index.js +4 -4
- package/dist/client/index.mjs +3 -3
- package/dist/components/index.d.mts +56 -8
- package/dist/components/index.d.ts +56 -8
- package/dist/components/index.js +12 -4
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +11 -3
- package/dist/contexts/index.d.mts +7 -2
- package/dist/contexts/index.d.ts +7 -2
- package/dist/contexts/index.js +4 -4
- package/dist/contexts/index.mjs +3 -3
- package/dist/core/index.d.mts +26 -4
- package/dist/core/index.d.ts +26 -4
- package/dist/core/index.js +2 -2
- package/dist/core/index.mjs +1 -1
- package/dist/features/help/index.js +36 -36
- package/dist/features/help/index.mjs +3 -3
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/index.mjs +2 -2
- package/dist/server/index.js +3 -3
- package/dist/server/index.mjs +1 -1
- package/package.json +1 -1
- package/src/components/tables/ContentListTable.tsx +2 -1
- package/src/features/assistant/components/containers/AssistantContainer.tsx +18 -2
- package/src/features/assistant/components/parts/AssistantEmptyState.tsx +22 -1
- package/src/features/assistant/components/parts/AssistantThread.tsx +12 -1
- package/src/features/assistant/contexts/AssistantContext.tsx +42 -9
- package/src/features/assistant/contexts/__tests__/AssistantContext.spec.tsx +94 -3
- package/src/features/assistant/data/Assistant.ts +6 -0
- package/src/features/assistant/data/AssistantInterface.ts +2 -0
- package/src/features/assistant/data/AssistantService.ts +38 -0
- package/src/features/assistant/data/__tests__/Assistant.spec.ts +30 -0
- package/src/features/assistant-message/AssistantMessageModule.ts +2 -0
- package/src/features/assistant-message/components/MessageItem.tsx +26 -4
- package/src/features/assistant-message/components/MessageList.tsx +4 -2
- package/src/features/assistant-message/data/AssistantMessage.ts +21 -1
- package/src/features/assistant-message/data/AssistantMessageInterface.ts +16 -0
- package/src/shadcnui/index.ts +3 -0
- package/src/shadcnui/ui/__tests__/confirm-dialog.test.tsx +46 -0
- package/src/shadcnui/ui/__tests__/empty-state.test.tsx +26 -0
- package/src/shadcnui/ui/__tests__/spinner.test.tsx +18 -0
- package/src/shadcnui/ui/confirm-dialog.tsx +79 -0
- package/src/shadcnui/ui/empty-state.tsx +32 -0
- package/src/shadcnui/ui/spinner.tsx +36 -0
- package/dist/chunk-DRJUN4R6.js.map +0 -1
- package/dist/chunk-LYLGU2BK.mjs.map +0 -1
- package/dist/chunk-U2HR57MY.js.map +0 -1
- /package/dist/{BlockNoteEditor-A5LNFWHW.mjs.map → BlockNoteEditor-HIEAHXR4.mjs.map} +0 -0
- /package/dist/{chunk-CWVYU4YZ.mjs.map → chunk-DJSH6VHF.mjs.map} +0 -0
|
@@ -26,6 +26,11 @@ interface AssistantContextValue {
|
|
|
26
26
|
startNew(): void;
|
|
27
27
|
renameThread(id: string, title: string): Promise<void>;
|
|
28
28
|
deleteThread(id: string): Promise<void>;
|
|
29
|
+
/** When true, create/append route to the operator-engine endpoints. */
|
|
30
|
+
operatorMode: boolean;
|
|
31
|
+
setOperatorMode(value: boolean): void;
|
|
32
|
+
/** Appends a message returned outside the send flow (e.g. an approve/deny resume). */
|
|
33
|
+
appendResolvedMessage(message: AssistantMessageInterface): void;
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
const AssistantContext = createContext<AssistantContextValue | undefined>(undefined);
|
|
@@ -77,6 +82,12 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
77
82
|
const [sending, setSending] = useState<boolean>(false);
|
|
78
83
|
const [status, setStatus] = useState<string | undefined>(undefined);
|
|
79
84
|
const [failedMessageIds, setFailedMessageIds] = useState<Set<string>>(() => new Set());
|
|
85
|
+
// The engine is persisted on the Assistant resource: an existing operator
|
|
86
|
+
// thread must keep routing to the operator endpoints after a reload, so the
|
|
87
|
+
// initial mode is derived from the hydrated thread instead of defaulting off.
|
|
88
|
+
const [operatorMode, setOperatorMode] = useState<boolean>(
|
|
89
|
+
() => dehydratedAssistant?.jsonApi?.attributes?.engine === "operator",
|
|
90
|
+
);
|
|
80
91
|
const { socket } = useSocketContext();
|
|
81
92
|
|
|
82
93
|
const sendMessage = useCallback(
|
|
@@ -101,11 +112,14 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
101
112
|
|
|
102
113
|
try {
|
|
103
114
|
if (!assistant) {
|
|
104
|
-
const
|
|
115
|
+
const input = {
|
|
105
116
|
firstMessage: trimmed,
|
|
106
117
|
howToMode: opts?.howToMode,
|
|
107
118
|
limitToHowToId: opts?.limitToHowToId,
|
|
108
|
-
}
|
|
119
|
+
};
|
|
120
|
+
const created = operatorMode
|
|
121
|
+
? await AssistantService.createOperator(input)
|
|
122
|
+
: await AssistantService.create(input);
|
|
109
123
|
const msgs = await AssistantMessageService.findByAssistant({ assistantId: created.id });
|
|
110
124
|
setAssistant(created);
|
|
111
125
|
setMessages(msgs);
|
|
@@ -114,12 +128,17 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
114
128
|
window.history.replaceState(null, "", `/assistants/${created.id}`);
|
|
115
129
|
}
|
|
116
130
|
} else {
|
|
117
|
-
const result =
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
131
|
+
const result = operatorMode
|
|
132
|
+
? await AssistantService.appendMessageOperator({
|
|
133
|
+
assistantId: assistant.id,
|
|
134
|
+
content: trimmed,
|
|
135
|
+
})
|
|
136
|
+
: await AssistantService.appendMessage({
|
|
137
|
+
assistantId: assistant.id,
|
|
138
|
+
content: trimmed,
|
|
139
|
+
howToMode: opts?.howToMode,
|
|
140
|
+
limitToHowToId: opts?.limitToHowToId,
|
|
141
|
+
});
|
|
123
142
|
setMessages((prev) => [...stripOptimistic(prev), ...result]);
|
|
124
143
|
}
|
|
125
144
|
} catch {
|
|
@@ -134,9 +153,13 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
134
153
|
setStatus(undefined);
|
|
135
154
|
}
|
|
136
155
|
},
|
|
137
|
-
[assistant, messages, socket],
|
|
156
|
+
[assistant, messages, socket, operatorMode],
|
|
138
157
|
);
|
|
139
158
|
|
|
159
|
+
const appendResolvedMessage = useCallback((message: AssistantMessageInterface) => {
|
|
160
|
+
setMessages((prev) => (prev.some((m) => m.id === message.id) ? prev : [...prev, message]));
|
|
161
|
+
}, []);
|
|
162
|
+
|
|
140
163
|
const retrySend = useCallback(
|
|
141
164
|
async (tempId: string) => {
|
|
142
165
|
const failed = messages.find((m) => m.id === tempId);
|
|
@@ -163,6 +186,10 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
163
186
|
]);
|
|
164
187
|
setAssistant(target);
|
|
165
188
|
setMessages(msgs);
|
|
189
|
+
// Route follow-up sends by the thread's persisted engine: operator
|
|
190
|
+
// threads stay on the operator endpoints, everything else (engine
|
|
191
|
+
// absent) keeps the default responder flow.
|
|
192
|
+
setOperatorMode(target.engine === "operator");
|
|
166
193
|
if (manageUrl && typeof window !== "undefined") {
|
|
167
194
|
window.history.replaceState(null, "", `/assistants/${id}`);
|
|
168
195
|
}
|
|
@@ -180,6 +207,7 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
180
207
|
setAssistant(undefined);
|
|
181
208
|
setMessages([]);
|
|
182
209
|
setFailedMessageIds(new Set());
|
|
210
|
+
setOperatorMode(false);
|
|
183
211
|
if (manageUrl && typeof window !== "undefined") {
|
|
184
212
|
window.history.replaceState(null, "", "/assistants");
|
|
185
213
|
}
|
|
@@ -228,6 +256,9 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
228
256
|
startNew,
|
|
229
257
|
renameThread,
|
|
230
258
|
deleteThread,
|
|
259
|
+
operatorMode,
|
|
260
|
+
setOperatorMode,
|
|
261
|
+
appendResolvedMessage,
|
|
231
262
|
}),
|
|
232
263
|
[
|
|
233
264
|
assistant,
|
|
@@ -243,6 +274,8 @@ export function AssistantProvider({ children, dehydratedAssistant, dehydratedMes
|
|
|
243
274
|
startNew,
|
|
244
275
|
renameThread,
|
|
245
276
|
deleteThread,
|
|
277
|
+
operatorMode,
|
|
278
|
+
appendResolvedMessage,
|
|
246
279
|
],
|
|
247
280
|
);
|
|
248
281
|
|
|
@@ -18,22 +18,32 @@ function wrapper({ children }: { children: React.ReactNode }) {
|
|
|
18
18
|
return <AssistantProvider>{children}</AssistantProvider>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function buildAssistantStub({ id, title = "Stub" }: { id: string; title?: string }) {
|
|
22
|
-
return {
|
|
21
|
+
function buildAssistantStub({ id, title = "Stub", engine }: { id: string; title?: string; engine?: string }) {
|
|
22
|
+
return {
|
|
23
|
+
id,
|
|
24
|
+
title,
|
|
25
|
+
engine,
|
|
26
|
+
messageCount: 0,
|
|
27
|
+
type: "assistants",
|
|
28
|
+
createdAt: new Date(),
|
|
29
|
+
updatedAt: new Date(),
|
|
30
|
+
} as any;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
function buildAssistantDehydrated({
|
|
26
34
|
id,
|
|
27
35
|
title = "Stub",
|
|
36
|
+
engine,
|
|
28
37
|
}: {
|
|
29
38
|
id: string;
|
|
30
39
|
title?: string;
|
|
40
|
+
engine?: string;
|
|
31
41
|
}): JsonApiHydratedDataInterface {
|
|
32
42
|
return {
|
|
33
43
|
jsonApi: {
|
|
34
44
|
type: "assistants",
|
|
35
45
|
id,
|
|
36
|
-
attributes: { title, messageCount: 0 },
|
|
46
|
+
attributes: { title, messageCount: 0, ...(engine !== undefined ? { engine } : {}) },
|
|
37
47
|
},
|
|
38
48
|
included: [],
|
|
39
49
|
};
|
|
@@ -123,6 +133,87 @@ describe("AssistantContext", () => {
|
|
|
123
133
|
expect(replaceState).toHaveBeenCalledWith(null, "", "/assistants/a-3");
|
|
124
134
|
});
|
|
125
135
|
|
|
136
|
+
it("selectThread on an operator thread enables operatorMode and routes follow-ups to the operator endpoint", async () => {
|
|
137
|
+
const target = buildAssistantStub({ id: "a-op", title: "Operator", engine: "operator" });
|
|
138
|
+
AssistantService.findOne = vi.fn().mockResolvedValue(target);
|
|
139
|
+
AssistantMessageService.findByAssistant = vi.fn().mockResolvedValue([]);
|
|
140
|
+
AssistantService.appendMessage = vi.fn().mockResolvedValue([]);
|
|
141
|
+
AssistantService.appendMessageOperator = vi.fn().mockResolvedValue([]);
|
|
142
|
+
vi.spyOn(window.history, "replaceState").mockImplementation(() => {});
|
|
143
|
+
|
|
144
|
+
const { result } = renderHook(() => useAssistantContext(), {
|
|
145
|
+
wrapper: ({ children }) => <AssistantProvider>{children}</AssistantProvider>,
|
|
146
|
+
});
|
|
147
|
+
await act(async () => {
|
|
148
|
+
await result.current.selectThread("a-op");
|
|
149
|
+
});
|
|
150
|
+
expect(result.current.operatorMode).toBe(true);
|
|
151
|
+
|
|
152
|
+
await act(async () => {
|
|
153
|
+
await result.current.sendMessage("continue");
|
|
154
|
+
});
|
|
155
|
+
expect(AssistantService.appendMessageOperator).toHaveBeenCalledWith({ assistantId: "a-op", content: "continue" });
|
|
156
|
+
expect(AssistantService.appendMessage).not.toHaveBeenCalled();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("selectThread on a responder thread (engine absent) resets operatorMode and keeps the responder endpoint", async () => {
|
|
160
|
+
const operatorThread = buildAssistantStub({ id: "a-op", engine: "operator" });
|
|
161
|
+
const responderThread = buildAssistantStub({ id: "a-resp" });
|
|
162
|
+
AssistantService.findOne = vi.fn().mockResolvedValueOnce(operatorThread).mockResolvedValueOnce(responderThread);
|
|
163
|
+
AssistantMessageService.findByAssistant = vi.fn().mockResolvedValue([]);
|
|
164
|
+
AssistantService.appendMessage = vi.fn().mockResolvedValue([]);
|
|
165
|
+
AssistantService.appendMessageOperator = vi.fn().mockResolvedValue([]);
|
|
166
|
+
vi.spyOn(window.history, "replaceState").mockImplementation(() => {});
|
|
167
|
+
|
|
168
|
+
const { result } = renderHook(() => useAssistantContext(), {
|
|
169
|
+
wrapper: ({ children }) => <AssistantProvider>{children}</AssistantProvider>,
|
|
170
|
+
});
|
|
171
|
+
await act(async () => {
|
|
172
|
+
await result.current.selectThread("a-op");
|
|
173
|
+
});
|
|
174
|
+
expect(result.current.operatorMode).toBe(true);
|
|
175
|
+
await act(async () => {
|
|
176
|
+
await result.current.selectThread("a-resp");
|
|
177
|
+
});
|
|
178
|
+
expect(result.current.operatorMode).toBe(false);
|
|
179
|
+
|
|
180
|
+
await act(async () => {
|
|
181
|
+
await result.current.sendMessage("hello");
|
|
182
|
+
});
|
|
183
|
+
expect(AssistantService.appendMessage).toHaveBeenCalledWith(
|
|
184
|
+
expect.objectContaining({ assistantId: "a-resp", content: "hello" }),
|
|
185
|
+
);
|
|
186
|
+
expect(AssistantService.appendMessageOperator).not.toHaveBeenCalled();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("hydrating an operator thread (reload) initialises operatorMode and routes sends to the operator endpoint", async () => {
|
|
190
|
+
const existing = buildAssistantDehydrated({ id: "a-op", title: "Operator", engine: "operator" });
|
|
191
|
+
AssistantService.appendMessage = vi.fn().mockResolvedValue([]);
|
|
192
|
+
AssistantService.appendMessageOperator = vi.fn().mockResolvedValue([]);
|
|
193
|
+
|
|
194
|
+
const { result } = renderHook(() => useAssistantContext(), {
|
|
195
|
+
wrapper: ({ children }) => <AssistantProvider dehydratedAssistant={existing}>{children}</AssistantProvider>,
|
|
196
|
+
});
|
|
197
|
+
expect(result.current.operatorMode).toBe(true);
|
|
198
|
+
|
|
199
|
+
await act(async () => {
|
|
200
|
+
await result.current.sendMessage("after reload");
|
|
201
|
+
});
|
|
202
|
+
expect(AssistantService.appendMessageOperator).toHaveBeenCalledWith({
|
|
203
|
+
assistantId: "a-op",
|
|
204
|
+
content: "after reload",
|
|
205
|
+
});
|
|
206
|
+
expect(AssistantService.appendMessage).not.toHaveBeenCalled();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("hydrating a responder thread (engine absent) keeps operatorMode off", () => {
|
|
210
|
+
const existing = buildAssistantDehydrated({ id: "a-resp", title: "Responder" });
|
|
211
|
+
const { result } = renderHook(() => useAssistantContext(), {
|
|
212
|
+
wrapper: ({ children }) => <AssistantProvider dehydratedAssistant={existing}>{children}</AssistantProvider>,
|
|
213
|
+
});
|
|
214
|
+
expect(result.current.operatorMode).toBe(false);
|
|
215
|
+
});
|
|
216
|
+
|
|
126
217
|
it("renameThread calls the service + updates active assistant title", async () => {
|
|
127
218
|
AssistantService.rename = vi.fn().mockResolvedValue(undefined);
|
|
128
219
|
const active = buildAssistantDehydrated({ id: "a-1", title: "Old" });
|
|
@@ -4,6 +4,7 @@ import { AssistantInput, AssistantInterface } from "./AssistantInterface";
|
|
|
4
4
|
export class Assistant extends AbstractApiData implements AssistantInterface {
|
|
5
5
|
private _title?: string;
|
|
6
6
|
private _messageCount?: number;
|
|
7
|
+
private _engine?: string;
|
|
7
8
|
|
|
8
9
|
get title(): string {
|
|
9
10
|
return this._title ?? "";
|
|
@@ -13,9 +14,14 @@ export class Assistant extends AbstractApiData implements AssistantInterface {
|
|
|
13
14
|
return this._messageCount ?? 0;
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
get engine(): string | undefined {
|
|
18
|
+
return this._engine;
|
|
19
|
+
}
|
|
20
|
+
|
|
16
21
|
rehydrate(data: JsonApiHydratedDataInterface): this {
|
|
17
22
|
super.rehydrate(data);
|
|
18
23
|
this._title = data.jsonApi.attributes?.title;
|
|
24
|
+
this._engine = data.jsonApi.attributes?.engine;
|
|
19
25
|
const fromMeta = data.jsonApi.meta?.messageCount;
|
|
20
26
|
const fromAttrs = data.jsonApi.attributes?.messageCount;
|
|
21
27
|
this._messageCount = typeof fromMeta === "number" ? fromMeta : typeof fromAttrs === "number" ? fromAttrs : 0;
|
|
@@ -10,4 +10,6 @@ export type AssistantInput = {
|
|
|
10
10
|
export interface AssistantInterface extends ApiDataInterface {
|
|
11
11
|
get title(): string;
|
|
12
12
|
get messageCount(): number;
|
|
13
|
+
/** Agent engine the thread runs on — `"operator"` or undefined (responder). */
|
|
14
|
+
get engine(): string | undefined;
|
|
13
15
|
}
|
|
@@ -31,6 +31,20 @@ export class AssistantService extends AbstractService {
|
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Creates an assistant whose first turn runs on the operator engine
|
|
36
|
+
* (durable checkpointing + approval gates). Mirrors `create()` against the
|
|
37
|
+
* standalone operator module's create route (`POST operator`).
|
|
38
|
+
*/
|
|
39
|
+
static async createOperator(params: AssistantInput): Promise<AssistantInterface> {
|
|
40
|
+
return this.callApi({
|
|
41
|
+
type: Modules.Assistant,
|
|
42
|
+
method: HttpMethod.POST,
|
|
43
|
+
endpoint: new EndpointCreator({ endpoint: "operator" }).generate(),
|
|
44
|
+
input: params,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
34
48
|
/**
|
|
35
49
|
* Sends a new user message to an existing assistant thread. The agent turn
|
|
36
50
|
* runs synchronously; the response is a two-element list: [user, assistant].
|
|
@@ -63,6 +77,30 @@ export class AssistantService extends AbstractService {
|
|
|
63
77
|
});
|
|
64
78
|
}
|
|
65
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Operator-engine variant of `appendMessage()`. Targets
|
|
82
|
+
* `POST operator/:assistantId/assistant-messages`; the turn may freeze on a
|
|
83
|
+
* destructive tool call, in which case the returned list ends with an
|
|
84
|
+
* `approval-request` assistant message linked to a pending AssistantAction.
|
|
85
|
+
*/
|
|
86
|
+
static async appendMessageOperator(params: {
|
|
87
|
+
assistantId: string;
|
|
88
|
+
content: string;
|
|
89
|
+
}): Promise<AssistantMessageInterface[]> {
|
|
90
|
+
const message = new AssistantMessage();
|
|
91
|
+
return this.callApi<AssistantMessageInterface[]>({
|
|
92
|
+
type: Modules.AssistantMessage,
|
|
93
|
+
method: HttpMethod.POST,
|
|
94
|
+
endpoint: new EndpointCreator({
|
|
95
|
+
endpoint: "operator",
|
|
96
|
+
id: params.assistantId,
|
|
97
|
+
childEndpoint: Modules.AssistantMessage,
|
|
98
|
+
}).generate(),
|
|
99
|
+
input: message.createAppendMessageJsonApi({ content: params.content }),
|
|
100
|
+
overridesJsonApiCreation: true,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
66
104
|
static async rename(params: { id: string; title: string }): Promise<void> {
|
|
67
105
|
await this.callApi({
|
|
68
106
|
type: Modules.Assistant,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Assistant } from "../Assistant";
|
|
3
|
+
|
|
4
|
+
describe("Assistant model", () => {
|
|
5
|
+
it("rehydrates engine from attributes (operator thread)", () => {
|
|
6
|
+
const assistant = new Assistant().rehydrate({
|
|
7
|
+
jsonApi: { type: "assistants", id: "a-1", attributes: { title: "T", engine: "operator" } },
|
|
8
|
+
included: [],
|
|
9
|
+
});
|
|
10
|
+
expect(assistant.engine).toBe("operator");
|
|
11
|
+
expect(assistant.title).toBe("T");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("leaves engine undefined when the attribute is absent (responder thread)", () => {
|
|
15
|
+
const assistant = new Assistant().rehydrate({
|
|
16
|
+
jsonApi: { type: "assistants", id: "a-2", attributes: { title: "T" } },
|
|
17
|
+
included: [],
|
|
18
|
+
});
|
|
19
|
+
expect(assistant.engine).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("preserves engine through dehydrate/rehydrate round-trips", () => {
|
|
23
|
+
const original = new Assistant().rehydrate({
|
|
24
|
+
jsonApi: { type: "assistants", id: "a-3", attributes: { title: "T", engine: "operator" } },
|
|
25
|
+
included: [],
|
|
26
|
+
});
|
|
27
|
+
const roundTripped = new Assistant().rehydrate(original.dehydrate());
|
|
28
|
+
expect(roundTripped.engine).toBe("operator");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -21,8 +21,10 @@ export const AssistantMessageModule = (factory: ModuleFactory) =>
|
|
|
21
21
|
`suggestedQuestions`,
|
|
22
22
|
`inputTokens`,
|
|
23
23
|
`outputTokens`,
|
|
24
|
+
`messageType`,
|
|
24
25
|
`references`,
|
|
25
26
|
`citations`,
|
|
27
|
+
`action`,
|
|
26
28
|
]),
|
|
27
29
|
createJsonApiInclusion("chunks", [`content`, `nodeId`, `nodeType`, `imagePath`, `source`]),
|
|
28
30
|
createJsonApiInclusion("users", [`name`, `avatar`]),
|
|
@@ -2,20 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import { useTranslations } from "next-intl";
|
|
4
4
|
import { Sparkles, AlertCircle } from "lucide-react";
|
|
5
|
+
import type { ReactNode } from "react";
|
|
5
6
|
import ReactMarkdown from "react-markdown";
|
|
6
7
|
import remarkGfm from "remark-gfm";
|
|
7
8
|
import type { AssistantMessageInterface } from "../data/AssistantMessageInterface";
|
|
8
9
|
import { MessageSourcesContainer } from "./parts/MessageSourcesContainer";
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Render slot for `approval-request` messages. The approval card lives in the
|
|
13
|
+
* consuming app (it depends on the app-registered AssistantAction module), so
|
|
14
|
+
* the chat renderer receives it as a function instead of importing it.
|
|
15
|
+
*/
|
|
16
|
+
export type ApprovalActionRenderer = (message: AssistantMessageInterface) => ReactNode;
|
|
17
|
+
|
|
10
18
|
interface Props {
|
|
11
19
|
message: AssistantMessageInterface;
|
|
12
20
|
isLatestAssistant: boolean;
|
|
13
21
|
onSelectFollowUp: (q: string) => void;
|
|
14
22
|
failedMessageIds?: Set<string>;
|
|
15
23
|
onRetry?: (tempId: string) => void;
|
|
24
|
+
renderApprovalAction?: ApprovalActionRenderer;
|
|
16
25
|
}
|
|
17
26
|
|
|
18
|
-
export function MessageItem({
|
|
27
|
+
export function MessageItem({
|
|
28
|
+
message,
|
|
29
|
+
isLatestAssistant,
|
|
30
|
+
onSelectFollowUp,
|
|
31
|
+
failedMessageIds,
|
|
32
|
+
onRetry,
|
|
33
|
+
renderApprovalAction,
|
|
34
|
+
}: Props) {
|
|
19
35
|
const t = useTranslations();
|
|
20
36
|
const isUser = message.role === "user";
|
|
21
37
|
const isFailed = isUser && !!failedMessageIds?.has(message.id);
|
|
@@ -39,6 +55,8 @@ export function MessageItem({ message, isLatestAssistant, onSelectFollowUp, fail
|
|
|
39
55
|
);
|
|
40
56
|
}
|
|
41
57
|
|
|
58
|
+
const isApprovalRequest = message.messageType === "approval-request" && !!renderApprovalAction;
|
|
59
|
+
|
|
42
60
|
return (
|
|
43
61
|
<div className="flex min-w-0 max-w-[78%] flex-col gap-1.5">
|
|
44
62
|
<div className="text-muted-foreground flex items-center gap-2 pl-1 text-xs">
|
|
@@ -47,9 +65,13 @@ export function MessageItem({ message, isLatestAssistant, onSelectFollowUp, fail
|
|
|
47
65
|
</span>
|
|
48
66
|
<span>{t("features.assistant.agent_name")}</span>
|
|
49
67
|
</div>
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
68
|
+
{isApprovalRequest ? (
|
|
69
|
+
renderApprovalAction(message)
|
|
70
|
+
) : (
|
|
71
|
+
<div className="bg-muted text-foreground rounded-2xl rounded-bl-sm px-3.5 py-2.5 text-sm leading-relaxed">
|
|
72
|
+
<ReactMarkdown remarkPlugins={[remarkGfm]}>{message.content}</ReactMarkdown>
|
|
73
|
+
</div>
|
|
74
|
+
)}
|
|
53
75
|
<MessageSourcesContainer
|
|
54
76
|
message={message}
|
|
55
77
|
isLatestAssistant={isLatestAssistant}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import { AssistantMessageInterface } from "../data/AssistantMessageInterface";
|
|
4
|
-
import { MessageItem } from "./MessageItem";
|
|
4
|
+
import { MessageItem, type ApprovalActionRenderer } from "./MessageItem";
|
|
5
5
|
|
|
6
6
|
interface Props {
|
|
7
7
|
messages: AssistantMessageInterface[];
|
|
8
8
|
onSelectFollowUp: (q: string) => void;
|
|
9
9
|
failedMessageIds?: Set<string>;
|
|
10
10
|
onRetry?: (tempId: string) => void;
|
|
11
|
+
renderApprovalAction?: ApprovalActionRenderer;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
export function MessageList({ messages, onSelectFollowUp, failedMessageIds, onRetry }: Props) {
|
|
14
|
+
export function MessageList({ messages, onSelectFollowUp, failedMessageIds, onRetry, renderApprovalAction }: Props) {
|
|
14
15
|
const ordered = [...messages].sort((a, b) => a.position - b.position);
|
|
15
16
|
|
|
16
17
|
let lastAssistantIndex = -1;
|
|
@@ -31,6 +32,7 @@ export function MessageList({ messages, onSelectFollowUp, failedMessageIds, onRe
|
|
|
31
32
|
onSelectFollowUp={onSelectFollowUp}
|
|
32
33
|
failedMessageIds={failedMessageIds}
|
|
33
34
|
onRetry={onRetry}
|
|
35
|
+
renderApprovalAction={renderApprovalAction}
|
|
34
36
|
/>
|
|
35
37
|
))}
|
|
36
38
|
</div>
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from "uuid";
|
|
2
2
|
import { AbstractApiData, ApiDataInterface, JsonApiHydratedDataInterface, Modules } from "../../../core";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
AssistantMessageInput,
|
|
5
|
+
AssistantMessageInterface,
|
|
6
|
+
AssistantMessageRole,
|
|
7
|
+
AssistantMessageType,
|
|
8
|
+
} from "./AssistantMessageInterface";
|
|
4
9
|
import { resolveReferenceableModules } from "../../assistant/utils/resolveReferenceableModules";
|
|
5
10
|
import { ChunkInterface, ChunkRelationshipMeta } from "../../chunk/data/ChunkInterface";
|
|
6
11
|
|
|
@@ -14,6 +19,8 @@ export class AssistantMessage extends AbstractApiData implements AssistantMessag
|
|
|
14
19
|
private _references?: ApiDataInterface[];
|
|
15
20
|
private _citations?: (ChunkInterface & ChunkRelationshipMeta)[];
|
|
16
21
|
private _isOptimistic = false;
|
|
22
|
+
private _messageType?: AssistantMessageType;
|
|
23
|
+
private _actionId?: string;
|
|
17
24
|
|
|
18
25
|
get role(): AssistantMessageRole {
|
|
19
26
|
return this._role ?? "assistant";
|
|
@@ -51,6 +58,14 @@ export class AssistantMessage extends AbstractApiData implements AssistantMessag
|
|
|
51
58
|
return this._isOptimistic;
|
|
52
59
|
}
|
|
53
60
|
|
|
61
|
+
get messageType(): AssistantMessageType {
|
|
62
|
+
return this._messageType ?? "text";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get actionId(): string | undefined {
|
|
66
|
+
return this._actionId;
|
|
67
|
+
}
|
|
68
|
+
|
|
54
69
|
rehydrate(data: JsonApiHydratedDataInterface): this {
|
|
55
70
|
super.rehydrate(data);
|
|
56
71
|
const attrs = data.jsonApi.attributes ?? {};
|
|
@@ -60,6 +75,11 @@ export class AssistantMessage extends AbstractApiData implements AssistantMessag
|
|
|
60
75
|
this._suggestedQuestions = Array.isArray(attrs.suggestedQuestions) ? attrs.suggestedQuestions : [];
|
|
61
76
|
this._inputTokens = attrs.inputTokens;
|
|
62
77
|
this._outputTokens = attrs.outputTokens;
|
|
78
|
+
this._messageType = attrs.messageType as AssistantMessageType | undefined;
|
|
79
|
+
// The AssistantAction module is app-registered, so only the raw
|
|
80
|
+
// relationship id is exposed (no hydration of the included resource here).
|
|
81
|
+
const actionRelationship = data.jsonApi.relationships?.action?.data;
|
|
82
|
+
this._actionId = Array.isArray(actionRelationship) ? actionRelationship[0]?.id : actionRelationship?.id;
|
|
63
83
|
const refs = this._readIncludedPolymorphic<ApiDataInterface>(data, "references", resolveReferenceableModules());
|
|
64
84
|
this._references = Array.isArray(refs) ? refs : refs ? [refs] : [];
|
|
65
85
|
if (data.jsonApi.relationships?.citations?.data) {
|
|
@@ -3,6 +3,14 @@ import { ChunkInterface, ChunkRelationshipMeta } from "../../chunk/data/ChunkInt
|
|
|
3
3
|
|
|
4
4
|
export type AssistantMessageRole = "user" | "assistant" | "system";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* "text" is the default chat message; "approval-request" marks the message
|
|
8
|
+
* created while an operator run is frozen on a destructive tool call,
|
|
9
|
+
* awaiting human approval (linked AssistantAction via the `action`
|
|
10
|
+
* relationship).
|
|
11
|
+
*/
|
|
12
|
+
export type AssistantMessageType = "text" | "approval-request";
|
|
13
|
+
|
|
6
14
|
export type AssistantMessageInput = {
|
|
7
15
|
id: string;
|
|
8
16
|
role: AssistantMessageRole;
|
|
@@ -21,4 +29,12 @@ export interface AssistantMessageInterface extends ApiDataInterface {
|
|
|
21
29
|
get references(): ApiDataInterface[];
|
|
22
30
|
get citations(): (ChunkInterface & ChunkRelationshipMeta)[];
|
|
23
31
|
get isOptimistic(): boolean;
|
|
32
|
+
get messageType(): AssistantMessageType;
|
|
33
|
+
/**
|
|
34
|
+
* Id of the AssistantAction linked through the `action` relationship.
|
|
35
|
+
* Only present on `approval-request` messages. Exposed as a raw id (not a
|
|
36
|
+
* hydrated model) because the AssistantAction module is registered by the
|
|
37
|
+
* consuming app, not by this library.
|
|
38
|
+
*/
|
|
39
|
+
get actionId(): string | undefined;
|
|
24
40
|
}
|
package/src/shadcnui/index.ts
CHANGED
|
@@ -16,10 +16,12 @@ export * from "./ui/checkbox";
|
|
|
16
16
|
export * from "./ui/collapsible";
|
|
17
17
|
export * from "./ui/combobox";
|
|
18
18
|
export * from "./ui/command";
|
|
19
|
+
export * from "./ui/confirm-dialog";
|
|
19
20
|
export * from "./ui/context-menu";
|
|
20
21
|
export * from "./ui/dialog";
|
|
21
22
|
export * from "./ui/drawer";
|
|
22
23
|
export * from "./ui/dropdown-menu";
|
|
24
|
+
export * from "./ui/empty-state";
|
|
23
25
|
export * from "./ui/field";
|
|
24
26
|
export * from "./ui/form";
|
|
25
27
|
export * from "./ui/hover-card";
|
|
@@ -40,6 +42,7 @@ export * from "./ui/sidebar";
|
|
|
40
42
|
export * from "./ui/skeleton";
|
|
41
43
|
export * from "./ui/slider";
|
|
42
44
|
export * from "./ui/sonner";
|
|
45
|
+
export * from "./ui/spinner";
|
|
43
46
|
export * from "./ui/switch";
|
|
44
47
|
export * from "./ui/table";
|
|
45
48
|
export * from "./ui/tabs";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// packages/nextjs-jsonapi/src/shadcnui/ui/__tests__/confirm-dialog.test.tsx
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { render, screen } from "@testing-library/react";
|
|
4
|
+
import userEvent from "@testing-library/user-event";
|
|
5
|
+
import { ConfirmDialog } from "../confirm-dialog";
|
|
6
|
+
|
|
7
|
+
describe("ConfirmDialog", () => {
|
|
8
|
+
it("renders title/description and calls onConfirm then closes", async () => {
|
|
9
|
+
const onConfirm = vi.fn();
|
|
10
|
+
const onOpenChange = vi.fn();
|
|
11
|
+
render(
|
|
12
|
+
<ConfirmDialog
|
|
13
|
+
open
|
|
14
|
+
onOpenChange={onOpenChange}
|
|
15
|
+
title="Reveal plot secrets?"
|
|
16
|
+
description="Spoilers ahead."
|
|
17
|
+
confirmLabel="Reveal"
|
|
18
|
+
cancelLabel="Keep hidden"
|
|
19
|
+
destructive
|
|
20
|
+
onConfirm={onConfirm}
|
|
21
|
+
/>,
|
|
22
|
+
);
|
|
23
|
+
expect(screen.getByText("Reveal plot secrets?")).toBeInTheDocument();
|
|
24
|
+
expect(screen.getByText("Spoilers ahead.")).toBeInTheDocument();
|
|
25
|
+
await userEvent.click(screen.getByRole("button", { name: "Reveal" }));
|
|
26
|
+
expect(onConfirm).toHaveBeenCalledTimes(1);
|
|
27
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("cancel closes without confirming", async () => {
|
|
31
|
+
const onConfirm = vi.fn();
|
|
32
|
+
const onOpenChange = vi.fn();
|
|
33
|
+
render(
|
|
34
|
+
<ConfirmDialog
|
|
35
|
+
open
|
|
36
|
+
onOpenChange={onOpenChange}
|
|
37
|
+
title="t"
|
|
38
|
+
confirmLabel="Yes"
|
|
39
|
+
cancelLabel="No"
|
|
40
|
+
onConfirm={onConfirm}
|
|
41
|
+
/>,
|
|
42
|
+
);
|
|
43
|
+
await userEvent.click(screen.getByRole("button", { name: "No" }));
|
|
44
|
+
expect(onConfirm).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// packages/nextjs-jsonapi/src/shadcnui/ui/__tests__/empty-state.test.tsx
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { render, screen } from "@testing-library/react";
|
|
4
|
+
import { InboxIcon } from "lucide-react";
|
|
5
|
+
import { EmptyState } from "../empty-state";
|
|
6
|
+
|
|
7
|
+
describe("EmptyState", () => {
|
|
8
|
+
it("renders title, description and action", () => {
|
|
9
|
+
render(
|
|
10
|
+
<EmptyState
|
|
11
|
+
icon={InboxIcon}
|
|
12
|
+
title="No games yet"
|
|
13
|
+
description="Create your first game."
|
|
14
|
+
action={<button type="button">New game</button>}
|
|
15
|
+
/>,
|
|
16
|
+
);
|
|
17
|
+
expect(screen.getByText("No games yet")).toBeInTheDocument();
|
|
18
|
+
expect(screen.getByText("Create your first game.")).toBeInTheDocument();
|
|
19
|
+
expect(screen.getByRole("button", { name: "New game" })).toBeInTheDocument();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("renders without optional parts", () => {
|
|
23
|
+
render(<EmptyState title="Empty" />);
|
|
24
|
+
expect(screen.getByText("Empty")).toBeInTheDocument();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// packages/nextjs-jsonapi/src/shadcnui/ui/__tests__/spinner.test.tsx
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { render, screen } from "@testing-library/react";
|
|
4
|
+
import { Spinner } from "../spinner";
|
|
5
|
+
|
|
6
|
+
describe("Spinner", () => {
|
|
7
|
+
it("renders a status indicator with animate-spin", () => {
|
|
8
|
+
render(<Spinner />);
|
|
9
|
+
const spinner = screen.getByRole("status");
|
|
10
|
+
expect(spinner).toHaveClass("animate-spin");
|
|
11
|
+
expect(spinner).toHaveClass("size-4");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("applies the size variant", () => {
|
|
15
|
+
render(<Spinner size="lg" />);
|
|
16
|
+
expect(screen.getByRole("status")).toHaveClass("size-8");
|
|
17
|
+
});
|
|
18
|
+
});
|