@paddockai/react 0.0.4 → 0.0.5
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/index.cjs +499 -10
- package/dist/index.d.cts +186 -5
- package/dist/index.d.ts +186 -5
- package/dist/index.js +490 -7
- package/dist/ui.cjs +586 -0
- package/dist/ui.d.cts +187 -0
- package/dist/ui.d.ts +187 -0
- package/dist/ui.js +572 -0
- package/package.json +15 -3
package/dist/ui.js
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
// src/ui/context.tsx
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var noTools = {};
|
|
5
|
+
function defaultResolver(participant) {
|
|
6
|
+
return (author) => ({
|
|
7
|
+
name: author.name ?? author.id,
|
|
8
|
+
role: "person",
|
|
9
|
+
self: participant !== void 0 && author.kind === "participant" && author.id === participant.id
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
var unknownAuthor = { name: "Unknown", role: "person", self: false };
|
|
13
|
+
function value(props) {
|
|
14
|
+
const resolve = props.resolveAuthor ?? defaultResolver(props.participant);
|
|
15
|
+
const agent = props.agent ?? { name: "Agent" };
|
|
16
|
+
return {
|
|
17
|
+
markdown: props.markdown,
|
|
18
|
+
tools: props.tools ?? noTools,
|
|
19
|
+
resolveAuthor: (author) => author ? resolve(author) : unknownAuthor,
|
|
20
|
+
agent: { ...agent, role: "agent", self: false }
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
var UI = createContext(value({}));
|
|
24
|
+
function UIProvider({ markdown, tools, resolveAuthor, participant, agent, children }) {
|
|
25
|
+
const participantId = participant?.id;
|
|
26
|
+
const agentName = agent?.name;
|
|
27
|
+
const agentAvatar = agent?.avatarUrl;
|
|
28
|
+
const v = useMemo(
|
|
29
|
+
() => value({
|
|
30
|
+
markdown,
|
|
31
|
+
tools,
|
|
32
|
+
resolveAuthor,
|
|
33
|
+
participant: participantId === void 0 ? void 0 : { id: participantId },
|
|
34
|
+
agent: agentName === void 0 ? void 0 : { name: agentName, ...agentAvatar ? { avatarUrl: agentAvatar } : {} }
|
|
35
|
+
}),
|
|
36
|
+
[markdown, tools, resolveAuthor, participantId, agentName, agentAvatar]
|
|
37
|
+
);
|
|
38
|
+
return /* @__PURE__ */ jsx(UI.Provider, { value: v, children });
|
|
39
|
+
}
|
|
40
|
+
var useUI = () => useContext(UI);
|
|
41
|
+
var SessionContext = createContext(null);
|
|
42
|
+
function useSessionContext(component) {
|
|
43
|
+
const v = useContext(SessionContext);
|
|
44
|
+
if (!v) throw new Error(`paddock: ${component} must be inside Session.Root`);
|
|
45
|
+
return v;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/ui/session.tsx
|
|
49
|
+
import {
|
|
50
|
+
useConversation,
|
|
51
|
+
useSession,
|
|
52
|
+
useSessionStatus,
|
|
53
|
+
useToolCall
|
|
54
|
+
} from "@paddockai/react";
|
|
55
|
+
|
|
56
|
+
// src/ui/slot.tsx
|
|
57
|
+
import {
|
|
58
|
+
cloneElement,
|
|
59
|
+
createElement,
|
|
60
|
+
Fragment,
|
|
61
|
+
isValidElement
|
|
62
|
+
} from "react";
|
|
63
|
+
function mergeRefs(...refs) {
|
|
64
|
+
return (node) => {
|
|
65
|
+
for (const r of refs) {
|
|
66
|
+
if (typeof r === "function") r(node);
|
|
67
|
+
else if (r && typeof r === "object") r.current = node;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function merge(slot, child) {
|
|
72
|
+
const out = { ...slot, ...child };
|
|
73
|
+
for (const key of Object.keys(slot)) {
|
|
74
|
+
const a = slot[key];
|
|
75
|
+
const b = child[key];
|
|
76
|
+
if (key === "className" && a && b) out[key] = `${String(a)} ${String(b)}`;
|
|
77
|
+
else if (key === "style" && a && b) out[key] = { ...a, ...b };
|
|
78
|
+
else if (key === "ref" && a && b) out[key] = mergeRefs(a, b);
|
|
79
|
+
else if (/^on[A-Z]/.test(key) && typeof a === "function" && typeof b === "function")
|
|
80
|
+
out[key] = (...args) => {
|
|
81
|
+
b(...args);
|
|
82
|
+
a(...args);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
function render(tag, asChild, props, children, content) {
|
|
88
|
+
if (asChild && isValidElement(children) && children.type !== Fragment) {
|
|
89
|
+
const childProps = children.props;
|
|
90
|
+
const childRef = children.ref ?? childProps.ref;
|
|
91
|
+
const merged = merge(props, childRef ? { ...childProps, ref: childRef } : childProps);
|
|
92
|
+
if (childProps.children === void 0 && content !== void 0) merged.children = content;
|
|
93
|
+
return cloneElement(children, merged);
|
|
94
|
+
}
|
|
95
|
+
return createElement(tag, props, children ?? content);
|
|
96
|
+
}
|
|
97
|
+
function flag(on) {
|
|
98
|
+
return on ? "" : void 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/ui/session.tsx
|
|
102
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
103
|
+
function SessionRoot({ session, mode = null, asChild, children, ...rest }) {
|
|
104
|
+
return /* @__PURE__ */ jsx2(SessionContext.Provider, { value: { session, mode }, children: render(
|
|
105
|
+
"div",
|
|
106
|
+
asChild,
|
|
107
|
+
{ "data-status": session.status, "data-connected": flag(session.connection === "open"), ...rest },
|
|
108
|
+
children
|
|
109
|
+
) });
|
|
110
|
+
}
|
|
111
|
+
var Session = { Root: SessionRoot };
|
|
112
|
+
function SessionStatus({ children, asChild, ...rest }) {
|
|
113
|
+
const { session, mode } = useSessionContext("SessionStatus");
|
|
114
|
+
const info = useSessionStatus(session, { mode });
|
|
115
|
+
const detail = {
|
|
116
|
+
...info,
|
|
117
|
+
connection: session.connection,
|
|
118
|
+
connectionError: session.connectionError,
|
|
119
|
+
error: session.error,
|
|
120
|
+
retryAt: session.session?.retryAt ?? null
|
|
121
|
+
};
|
|
122
|
+
const text = info.connected ? info.status : `${info.status} \xB7 ${session.connection}`;
|
|
123
|
+
return render(
|
|
124
|
+
"div",
|
|
125
|
+
asChild,
|
|
126
|
+
{ "data-status": info.status, "data-connected": flag(info.connected), ...rest },
|
|
127
|
+
children ? children(detail) : text
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
function runningTool(parts) {
|
|
131
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
132
|
+
const p = parts[i];
|
|
133
|
+
if (!p) continue;
|
|
134
|
+
const inner = runningTool(p.children.filter((c) => c.kind === "tool"));
|
|
135
|
+
if (inner) return inner;
|
|
136
|
+
if (p.status === "running") return p;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
function Peek({
|
|
141
|
+
part,
|
|
142
|
+
latest,
|
|
143
|
+
...props
|
|
144
|
+
}) {
|
|
145
|
+
const summary = useToolCall(part ?? emptyTool);
|
|
146
|
+
const { session, children, asChild, ...rest } = props;
|
|
147
|
+
const status = useSessionStatus(session);
|
|
148
|
+
const detail = {
|
|
149
|
+
status: status.status,
|
|
150
|
+
connected: status.connected,
|
|
151
|
+
activity: part ? summary.title : latest
|
|
152
|
+
};
|
|
153
|
+
return render(
|
|
154
|
+
"div",
|
|
155
|
+
asChild,
|
|
156
|
+
{ "data-status": status.status, "data-connected": flag(status.connected), ...rest },
|
|
157
|
+
children ? children(detail) : detail.activity ? `${detail.status}: ${detail.activity}` : detail.status
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
var emptyTool = {
|
|
161
|
+
kind: "tool",
|
|
162
|
+
id: "",
|
|
163
|
+
callId: "",
|
|
164
|
+
call: null,
|
|
165
|
+
result: null,
|
|
166
|
+
status: "done",
|
|
167
|
+
children: []
|
|
168
|
+
};
|
|
169
|
+
function SessionPeek({ sessionId, token, baseUrl, ...rest }) {
|
|
170
|
+
const session = useSession(sessionId, baseUrl === void 0 ? { token } : { token, baseUrl });
|
|
171
|
+
const items = useConversation(session);
|
|
172
|
+
const turn = [...items].reverse().find((i) => i.kind === "turn");
|
|
173
|
+
const parts = turn?.kind === "turn" ? turn.parts : [];
|
|
174
|
+
const part = turn?.kind === "turn" && turn.status === "running" ? runningTool(parts.filter((p) => p.kind === "tool")) : null;
|
|
175
|
+
const text = [...parts].reverse().find((p) => p.kind === "text");
|
|
176
|
+
return /* @__PURE__ */ jsx2(Peek, { ...rest, session, part, latest: text?.kind === "text" ? text.text : null });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/ui/conversation.tsx
|
|
180
|
+
import {
|
|
181
|
+
createContext as createContext2,
|
|
182
|
+
useCallback,
|
|
183
|
+
useContext as useContext2,
|
|
184
|
+
useLayoutEffect,
|
|
185
|
+
useRef,
|
|
186
|
+
useState
|
|
187
|
+
} from "react";
|
|
188
|
+
import { initialState } from "@paddockai/client";
|
|
189
|
+
import {
|
|
190
|
+
useConversation as useConversation2,
|
|
191
|
+
useToolCall as useToolCall2
|
|
192
|
+
} from "@paddockai/react";
|
|
193
|
+
import { Fragment as Fragment2, jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
194
|
+
var noAction = () => Promise.reject(new Error("paddock: no session"));
|
|
195
|
+
var noSession = { ...initialState, send: noAction, stop: noAction };
|
|
196
|
+
var ConversationContext = createContext2({ items: [] });
|
|
197
|
+
var bottomSlack = 8;
|
|
198
|
+
function ConversationRoot({ items, notices, asChild, children, onScroll, ...rest }) {
|
|
199
|
+
const session = useContext2(SessionContext);
|
|
200
|
+
const own = useConversation2(session?.session ?? noSession, { notices: notices ?? [] });
|
|
201
|
+
const shown = items ?? own;
|
|
202
|
+
const ref = useRef(null);
|
|
203
|
+
const [atBottom, setAtBottom] = useState(true);
|
|
204
|
+
const stick = useRef(true);
|
|
205
|
+
const handleScroll = useCallback(
|
|
206
|
+
(e) => {
|
|
207
|
+
const el = e.currentTarget;
|
|
208
|
+
const bottom = el.scrollHeight - el.scrollTop - el.clientHeight <= bottomSlack;
|
|
209
|
+
stick.current = bottom;
|
|
210
|
+
setAtBottom(bottom);
|
|
211
|
+
onScroll?.(e);
|
|
212
|
+
},
|
|
213
|
+
[onScroll]
|
|
214
|
+
);
|
|
215
|
+
useLayoutEffect(() => {
|
|
216
|
+
const el = ref.current;
|
|
217
|
+
if (el && stick.current) el.scrollTop = el.scrollHeight;
|
|
218
|
+
}, [shown]);
|
|
219
|
+
return /* @__PURE__ */ jsx3(ConversationContext.Provider, { value: { items: shown }, children: render(
|
|
220
|
+
"div",
|
|
221
|
+
asChild,
|
|
222
|
+
{
|
|
223
|
+
role: "log",
|
|
224
|
+
"aria-live": "polite",
|
|
225
|
+
"data-at-bottom": flag(atBottom),
|
|
226
|
+
...rest,
|
|
227
|
+
ref,
|
|
228
|
+
onScroll: handleScroll
|
|
229
|
+
},
|
|
230
|
+
children,
|
|
231
|
+
/* @__PURE__ */ jsx3(ConversationItems, {})
|
|
232
|
+
) });
|
|
233
|
+
}
|
|
234
|
+
function keyOf(item, index) {
|
|
235
|
+
const id = typeof item === "object" && item !== null ? item.id : void 0;
|
|
236
|
+
return typeof id === "string" || typeof id === "number" ? id : index;
|
|
237
|
+
}
|
|
238
|
+
function ConversationItems({ children, asChild, ...rest }) {
|
|
239
|
+
const { items } = useContext2(ConversationContext);
|
|
240
|
+
const renderItem = children ?? ((item) => /* @__PURE__ */ jsx3(DefaultItem, { item }));
|
|
241
|
+
return render(
|
|
242
|
+
"div",
|
|
243
|
+
asChild,
|
|
244
|
+
rest,
|
|
245
|
+
items.map((item, i) => /* @__PURE__ */ jsx3(ItemSlot, { children: renderItem(item, i) }, keyOf(item, i)))
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
function ItemSlot({ children }) {
|
|
249
|
+
return /* @__PURE__ */ jsx3(Fragment2, { children });
|
|
250
|
+
}
|
|
251
|
+
function ConversationEmpty({ asChild, children, ...rest }) {
|
|
252
|
+
const { items } = useContext2(ConversationContext);
|
|
253
|
+
if (items.length > 0) return null;
|
|
254
|
+
return render("div", asChild, rest, children);
|
|
255
|
+
}
|
|
256
|
+
var Conversation = { Root: ConversationRoot, Items: ConversationItems, Empty: ConversationEmpty };
|
|
257
|
+
function DefaultItem({ item }) {
|
|
258
|
+
const ui = useUI();
|
|
259
|
+
switch (item.kind) {
|
|
260
|
+
case "user": {
|
|
261
|
+
const author = ui.resolveAuthor(item.author);
|
|
262
|
+
return /* @__PURE__ */ jsxs(MessageRoot, { role: author.role, author, delivery: item.delivery, children: [
|
|
263
|
+
/* @__PURE__ */ jsx3(Author, {}),
|
|
264
|
+
/* @__PURE__ */ jsx3(MessageContent, { children: item.text })
|
|
265
|
+
] });
|
|
266
|
+
}
|
|
267
|
+
case "turn":
|
|
268
|
+
return /* @__PURE__ */ jsxs(TurnRoot, { turn: item, children: [
|
|
269
|
+
/* @__PURE__ */ jsx3(Author, { author: ui.agent }),
|
|
270
|
+
/* @__PURE__ */ jsx3(TurnParts, {}),
|
|
271
|
+
/* @__PURE__ */ jsx3(TurnStatus, {})
|
|
272
|
+
] });
|
|
273
|
+
case "notice":
|
|
274
|
+
return /* @__PURE__ */ jsx3(MessageRoot, { role: "system", "data-notice": item.event.type, children: /* @__PURE__ */ jsx3(MessageContent, { children: item.event.type }) });
|
|
275
|
+
default:
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
var MessageContext = createContext2(null);
|
|
280
|
+
function MessageRoot({ role, author = null, delivery, asChild, children, ...rest }) {
|
|
281
|
+
return /* @__PURE__ */ jsx3(MessageContext.Provider, { value: { role, author }, children: render(
|
|
282
|
+
"div",
|
|
283
|
+
asChild,
|
|
284
|
+
{ "data-role": role, "data-self": flag(author?.self ?? false), "data-delivery": delivery, ...rest },
|
|
285
|
+
children
|
|
286
|
+
) });
|
|
287
|
+
}
|
|
288
|
+
function MessageContent({ asChild, children, ...rest }) {
|
|
289
|
+
return render("div", asChild, rest, children);
|
|
290
|
+
}
|
|
291
|
+
var Message = { Root: MessageRoot, Content: MessageContent };
|
|
292
|
+
function Author({ author, asChild, children, ...rest }) {
|
|
293
|
+
const message = useContext2(MessageContext);
|
|
294
|
+
const shown = author ?? message?.author ?? null;
|
|
295
|
+
if (!shown && children === void 0) return null;
|
|
296
|
+
return render(
|
|
297
|
+
"span",
|
|
298
|
+
asChild,
|
|
299
|
+
{ "data-role": shown?.role, "data-self": flag(shown?.self ?? false), ...rest },
|
|
300
|
+
children,
|
|
301
|
+
shown?.name
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
var TurnContext = createContext2(null);
|
|
305
|
+
function useTurn(component) {
|
|
306
|
+
const t = useContext2(TurnContext);
|
|
307
|
+
if (!t) throw new Error(`paddock: ${component} must be inside Turn.Root`);
|
|
308
|
+
return t;
|
|
309
|
+
}
|
|
310
|
+
function TurnRoot({ turn, asChild, children, ...rest }) {
|
|
311
|
+
return /* @__PURE__ */ jsx3(TurnContext.Provider, { value: turn, children: render("div", asChild, { "data-status": turn.status, "data-origin": turn.origin, ...rest }, children) });
|
|
312
|
+
}
|
|
313
|
+
function TurnParts({ children, asChild, ...rest }) {
|
|
314
|
+
const turn = useTurn("Turn.Parts");
|
|
315
|
+
const renderPart = children ?? ((p) => /* @__PURE__ */ jsx3(Part, { part: p }));
|
|
316
|
+
return render(
|
|
317
|
+
"div",
|
|
318
|
+
asChild,
|
|
319
|
+
rest,
|
|
320
|
+
turn.parts.map((p) => /* @__PURE__ */ jsx3(ItemSlot, { children: renderPart(p) }, p.id))
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
function turnEnd(turn) {
|
|
324
|
+
switch (turn.status) {
|
|
325
|
+
case "running":
|
|
326
|
+
case "completed":
|
|
327
|
+
return null;
|
|
328
|
+
case "stopped":
|
|
329
|
+
return "Stopped";
|
|
330
|
+
case "discarded":
|
|
331
|
+
return "Discarded";
|
|
332
|
+
case "failed": {
|
|
333
|
+
const what = turn.error ? `Failed: ${turn.error.message}` : "Failed";
|
|
334
|
+
if (turn.retryAt) return `${what}. Retrying at ${turn.retryAt}`;
|
|
335
|
+
if (turn.gaveUp) return `${what}. Gave up`;
|
|
336
|
+
return what;
|
|
337
|
+
}
|
|
338
|
+
default:
|
|
339
|
+
return turn.status;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function TurnStatus({ children, asChild, ...rest }) {
|
|
343
|
+
const turn = useTurn("Turn.Status");
|
|
344
|
+
const content = children ? children(turn) : turnEnd(turn);
|
|
345
|
+
if (content === null || content === void 0) return null;
|
|
346
|
+
return render(
|
|
347
|
+
"div",
|
|
348
|
+
asChild,
|
|
349
|
+
{
|
|
350
|
+
"data-status": turn.status,
|
|
351
|
+
"data-gave-up": flag(turn.gaveUp),
|
|
352
|
+
"data-retry-at": turn.retryAt ?? void 0,
|
|
353
|
+
...rest
|
|
354
|
+
},
|
|
355
|
+
content
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
var Turn = { Root: TurnRoot, Parts: TurnParts, Status: TurnStatus };
|
|
359
|
+
function lookupTool(tools, part, summary) {
|
|
360
|
+
const keys = [];
|
|
361
|
+
if (summary.mcp) keys.push(`mcp:${summary.mcp.server}/${summary.mcp.tool}`, `mcp:${summary.mcp.server}`);
|
|
362
|
+
if (part.call) keys.push(`raw:${part.call.rawName}`);
|
|
363
|
+
keys.push(summary.kind);
|
|
364
|
+
for (const k of keys) if (Object.hasOwn(tools, k)) return tools[k];
|
|
365
|
+
return void 0;
|
|
366
|
+
}
|
|
367
|
+
function ToolPartView({ part }) {
|
|
368
|
+
const summary = useToolCall2(part);
|
|
369
|
+
const { tools } = useUI();
|
|
370
|
+
const Card = lookupTool(tools, part, summary);
|
|
371
|
+
return Card ? /* @__PURE__ */ jsx3(Card, { part, summary }) : /* @__PURE__ */ jsx3(ToolCallRoot, { part });
|
|
372
|
+
}
|
|
373
|
+
function Part({ part, asChild, children, ...rest }) {
|
|
374
|
+
const { markdown: Markdown } = useUI();
|
|
375
|
+
const attrs = { "data-kind": part.kind, ...rest };
|
|
376
|
+
switch (part.kind) {
|
|
377
|
+
case "text":
|
|
378
|
+
return render(
|
|
379
|
+
"div",
|
|
380
|
+
asChild,
|
|
381
|
+
{ ...attrs, "data-streaming": flag(part.streaming) },
|
|
382
|
+
children,
|
|
383
|
+
Markdown ? /* @__PURE__ */ jsx3(Markdown, { children: part.text }) : part.text
|
|
384
|
+
);
|
|
385
|
+
case "reasoning":
|
|
386
|
+
return render(
|
|
387
|
+
"details",
|
|
388
|
+
asChild,
|
|
389
|
+
attrs,
|
|
390
|
+
children,
|
|
391
|
+
/* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
392
|
+
/* @__PURE__ */ jsx3("summary", { children: "Reasoning" }),
|
|
393
|
+
part.text
|
|
394
|
+
] })
|
|
395
|
+
);
|
|
396
|
+
case "tool":
|
|
397
|
+
return render("div", asChild, attrs, children, /* @__PURE__ */ jsx3(ToolPartView, { part }));
|
|
398
|
+
default:
|
|
399
|
+
return render(
|
|
400
|
+
"details",
|
|
401
|
+
asChild,
|
|
402
|
+
{ ...attrs, "data-harness": part.harness ?? void 0, "data-type": part.type },
|
|
403
|
+
children,
|
|
404
|
+
/* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
405
|
+
/* @__PURE__ */ jsx3("summary", { children: `${part.harness ?? "unknown"} \xB7 ${part.type}` }),
|
|
406
|
+
/* @__PURE__ */ jsx3("pre", { children: JSON.stringify(part.raw, null, 2) })
|
|
407
|
+
] })
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
var ToolContext = createContext2(null);
|
|
412
|
+
function useTool(component) {
|
|
413
|
+
const t = useContext2(ToolContext);
|
|
414
|
+
if (!t) throw new Error(`paddock: ${component} must be inside ToolCall.Root`);
|
|
415
|
+
return t;
|
|
416
|
+
}
|
|
417
|
+
function ToolCallRoot({ part, asChild, children, ...rest }) {
|
|
418
|
+
const summary = useToolCall2(part);
|
|
419
|
+
return /* @__PURE__ */ jsx3(ToolContext.Provider, { value: { part, summary }, children: render(
|
|
420
|
+
"div",
|
|
421
|
+
asChild,
|
|
422
|
+
{ "data-tool": summary.kind, "data-status": summary.status, ...rest },
|
|
423
|
+
children,
|
|
424
|
+
/* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
425
|
+
/* @__PURE__ */ jsx3(ToolCallTitle, {}),
|
|
426
|
+
/* @__PURE__ */ jsx3(ToolCallDetail, {}),
|
|
427
|
+
/* @__PURE__ */ jsx3(ToolCallChildren, {})
|
|
428
|
+
] })
|
|
429
|
+
) });
|
|
430
|
+
}
|
|
431
|
+
function ToolCallTitle({ asChild, children, ...rest }) {
|
|
432
|
+
const { summary } = useTool("ToolCall.Title");
|
|
433
|
+
return render("span", asChild, rest, children, summary.title);
|
|
434
|
+
}
|
|
435
|
+
function detailOf(s) {
|
|
436
|
+
if (s.shell) {
|
|
437
|
+
const lines = [`$ ${s.shell.command}`];
|
|
438
|
+
if (s.shell.output) lines.push(s.shell.output);
|
|
439
|
+
if (s.shell.exitCode !== null) lines.push(`exit ${s.shell.exitCode}`);
|
|
440
|
+
return lines.join("\n");
|
|
441
|
+
}
|
|
442
|
+
if (s.diff)
|
|
443
|
+
return s.diff.map(
|
|
444
|
+
(d) => d.unified ?? [d.before === null ? null : `- ${d.before}`, d.after === null ? null : `+ ${d.after}`].filter((l) => l !== null).join("\n")
|
|
445
|
+
).join("\n");
|
|
446
|
+
if (s.mcp) {
|
|
447
|
+
const content = s.mcp.content?.map(
|
|
448
|
+
(c) => typeof c === "object" && c !== null && c.type === "text" ? String(c.text) : JSON.stringify(c)
|
|
449
|
+
).join("\n");
|
|
450
|
+
return [JSON.stringify(s.mcp.arguments, null, 2), content].filter((x) => x).join("\n");
|
|
451
|
+
}
|
|
452
|
+
if ("text" in s) return s.text ?? null;
|
|
453
|
+
return JSON.stringify({ input: s.call?.input ?? null, output: s.result?.output ?? null }, null, 2);
|
|
454
|
+
}
|
|
455
|
+
function ToolCallDetail({ asChild, children, ...rest }) {
|
|
456
|
+
const { summary } = useTool("ToolCall.Detail");
|
|
457
|
+
if (children) return render("div", asChild, rest, children(summary));
|
|
458
|
+
const text = detailOf(summary);
|
|
459
|
+
if (text === null || text === "") return null;
|
|
460
|
+
return render("pre", asChild, rest, text);
|
|
461
|
+
}
|
|
462
|
+
function ToolCallChildren({ asChild, children, ...rest }) {
|
|
463
|
+
const { part } = useTool("ToolCall.Children");
|
|
464
|
+
if (part.children.length === 0 && children === void 0) return null;
|
|
465
|
+
return render(
|
|
466
|
+
"div",
|
|
467
|
+
asChild,
|
|
468
|
+
rest,
|
|
469
|
+
children,
|
|
470
|
+
part.children.map((p) => /* @__PURE__ */ jsx3(Part, { part: p }, p.id))
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
var ToolCall = {
|
|
474
|
+
Root: ToolCallRoot,
|
|
475
|
+
Title: ToolCallTitle,
|
|
476
|
+
Detail: ToolCallDetail,
|
|
477
|
+
Children: ToolCallChildren
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
// src/ui/composer.tsx
|
|
481
|
+
import {
|
|
482
|
+
createContext as createContext3,
|
|
483
|
+
useContext as useContext3
|
|
484
|
+
} from "react";
|
|
485
|
+
import { useComposer, useSessionStatus as useSessionStatus2 } from "@paddockai/react";
|
|
486
|
+
import { Fragment as Fragment3, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
487
|
+
var ComposerContext = createContext3(null);
|
|
488
|
+
function useComposerContext(component) {
|
|
489
|
+
const v = useContext3(ComposerContext);
|
|
490
|
+
if (!v) throw new Error(`paddock: ${component} must be inside Composer.Root`);
|
|
491
|
+
return v;
|
|
492
|
+
}
|
|
493
|
+
function ComposerRoot({ onError, asChild, children, ...rest }) {
|
|
494
|
+
const { session, mode } = useSessionContext("Composer.Root");
|
|
495
|
+
const composer = useComposer(session, onError ? { mode, onError } : { mode });
|
|
496
|
+
const status = useSessionStatus2(session, { mode });
|
|
497
|
+
const disabled = !status.canSend;
|
|
498
|
+
const onSubmit = (e) => {
|
|
499
|
+
e.preventDefault();
|
|
500
|
+
void composer.submit();
|
|
501
|
+
};
|
|
502
|
+
return /* @__PURE__ */ jsx4(ComposerContext.Provider, { value: { ...composer, disabled }, children: render(
|
|
503
|
+
"form",
|
|
504
|
+
asChild,
|
|
505
|
+
{ "data-pending": flag(composer.pending), "data-disabled": flag(disabled), ...rest, onSubmit },
|
|
506
|
+
children,
|
|
507
|
+
/* @__PURE__ */ jsxs2(Fragment3, { children: [
|
|
508
|
+
/* @__PURE__ */ jsx4(ComposerInput, {}),
|
|
509
|
+
/* @__PURE__ */ jsx4(ComposerSend, {}),
|
|
510
|
+
/* @__PURE__ */ jsx4(ComposerStop, {})
|
|
511
|
+
] })
|
|
512
|
+
) });
|
|
513
|
+
}
|
|
514
|
+
function ComposerInput({ asChild, children, onKeyDown, ...rest }) {
|
|
515
|
+
const c = useComposerContext("Composer.Input");
|
|
516
|
+
return render(
|
|
517
|
+
"textarea",
|
|
518
|
+
asChild,
|
|
519
|
+
{
|
|
520
|
+
...rest,
|
|
521
|
+
...c.inputProps,
|
|
522
|
+
onKeyDown: (e) => {
|
|
523
|
+
onKeyDown?.(e);
|
|
524
|
+
c.inputProps.onKeyDown(e);
|
|
525
|
+
},
|
|
526
|
+
disabled: c.disabled || rest.disabled
|
|
527
|
+
},
|
|
528
|
+
children
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
function ComposerSend({ asChild, children, ...rest }) {
|
|
532
|
+
const c = useComposerContext("Composer.Send");
|
|
533
|
+
return render(
|
|
534
|
+
"button",
|
|
535
|
+
asChild,
|
|
536
|
+
{ type: "submit", ...rest, disabled: !c.canSend || rest.disabled },
|
|
537
|
+
children,
|
|
538
|
+
"Send"
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
function ComposerStop({ asChild, children, onClick, ...rest }) {
|
|
542
|
+
const c = useComposerContext("Composer.Stop");
|
|
543
|
+
return render(
|
|
544
|
+
"button",
|
|
545
|
+
asChild,
|
|
546
|
+
{
|
|
547
|
+
type: "button",
|
|
548
|
+
...rest,
|
|
549
|
+
disabled: !c.canStop || rest.disabled,
|
|
550
|
+
onClick: (e) => {
|
|
551
|
+
onClick?.(e);
|
|
552
|
+
void c.stop();
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
children,
|
|
556
|
+
"Stop"
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
var Composer = { Root: ComposerRoot, Input: ComposerInput, Send: ComposerSend, Stop: ComposerStop };
|
|
560
|
+
export {
|
|
561
|
+
Author,
|
|
562
|
+
Composer,
|
|
563
|
+
Conversation,
|
|
564
|
+
Message,
|
|
565
|
+
Part,
|
|
566
|
+
Session,
|
|
567
|
+
SessionPeek,
|
|
568
|
+
SessionStatus,
|
|
569
|
+
ToolCall,
|
|
570
|
+
Turn,
|
|
571
|
+
UIProvider
|
|
572
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paddockai/react",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "React bindings of the paddock client SDK: PaddockProvider and
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "React bindings of the paddock client SDK: PaddockProvider, useSession, the headless hooks, and unstyled components at @paddockai/react/ui.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.cjs",
|
|
@@ -17,13 +17,23 @@
|
|
|
17
17
|
"types": "./dist/index.d.cts",
|
|
18
18
|
"default": "./dist/index.cjs"
|
|
19
19
|
}
|
|
20
|
+
},
|
|
21
|
+
"./ui": {
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/ui.d.ts",
|
|
24
|
+
"default": "./dist/ui.js"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/ui.d.cts",
|
|
28
|
+
"default": "./dist/ui.cjs"
|
|
29
|
+
}
|
|
20
30
|
}
|
|
21
31
|
},
|
|
22
32
|
"files": [
|
|
23
33
|
"dist"
|
|
24
34
|
],
|
|
25
35
|
"dependencies": {
|
|
26
|
-
"@paddockai/client": "0.0.
|
|
36
|
+
"@paddockai/client": "0.0.5"
|
|
27
37
|
},
|
|
28
38
|
"peerDependencies": {
|
|
29
39
|
"react": "^18.0.0 || ^19.0.0"
|
|
@@ -32,6 +42,8 @@
|
|
|
32
42
|
"@testing-library/react": "16.3.3",
|
|
33
43
|
"@types/react": "19.3.0",
|
|
34
44
|
"@types/react-dom": "19.3.0",
|
|
45
|
+
"ink": "6.8.0",
|
|
46
|
+
"ink-testing-library": "4.0.0",
|
|
35
47
|
"jsdom": "31.0.1",
|
|
36
48
|
"react": "19.3.1",
|
|
37
49
|
"react-dom": "19.3.1"
|