@tangle-network/create-agent-app 0.50.29 → 0.50.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -78
- package/package.json +1 -1
- package/template-chat/AGENTS.md +14 -1
- package/template-chat/CUSTOMIZE.md +83 -102
- package/template-chat/README.md +83 -41
- package/template-chat/_package.json +18 -3
- package/template-chat/_wrangler.toml +10 -3
- package/template-chat/tailwind.config.mjs +3 -0
- package/template-chat/vite.config.mjs +11 -0
- package/template-chat/web/App.tsx +104 -0
- package/template-chat/web/Conversation.tsx +210 -0
- package/template-chat/web/api.ts +63 -0
- package/template-chat/web/env.d.ts +1 -0
- package/template-chat/web/index.html +12 -0
- package/template-chat/web/main.tsx +7 -0
- package/template-chat/web/styles.css +17 -0
- package/template-chat/web/tsconfig.json +16 -0
- package/template-chat/web/uploads.ts +55 -0
- package/template-chat/public/index.html +0 -255
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
@import 'tailwindcss';
|
|
2
|
+
@import '@tangle-network/sandbox-ui/styles';
|
|
3
|
+
@import '@tangle-network/agent-app/styles';
|
|
4
|
+
@config '../tailwind.config.mjs';
|
|
5
|
+
|
|
6
|
+
/* Standalone consumer: scan installed public JS, never a monorepo src alias. */
|
|
7
|
+
@source '../node_modules/@tangle-network/agent-app/dist';
|
|
8
|
+
@source '../node_modules/@tangle-network/sandbox-ui/dist';
|
|
9
|
+
@source '../node_modules/@tangle-network/ui/dist';
|
|
10
|
+
|
|
11
|
+
body {
|
|
12
|
+
margin: 0;
|
|
13
|
+
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
|
|
14
|
+
background: hsl(var(--background));
|
|
15
|
+
color: hsl(var(--foreground));
|
|
16
|
+
}
|
|
17
|
+
html, body, #root { min-height: 100%; }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"noUncheckedIndexedAccess": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"types": ["react", "react-dom"]
|
|
14
|
+
},
|
|
15
|
+
"include": ["./**/*.ts", "./**/*.tsx"]
|
|
16
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
import type { ComposerFile, ComposerFilePart } from '@tangle-network/agent-app/web-react'
|
|
3
|
+
import { json } from './api'
|
|
4
|
+
|
|
5
|
+
// Product adapter for createUploadRoute's inline/sandbox parts contract.
|
|
6
|
+
// useComposerAttachments is for a DIFFERENT, durable-store upload contract.
|
|
7
|
+
// Keep the server response verbatim; do not turn a data: URI into a store path.
|
|
8
|
+
export function useInlineUploads() {
|
|
9
|
+
const [files, setFiles] = useState<ComposerFile[]>([])
|
|
10
|
+
const requests = useRef(new Map<string, { file: File; controller: AbortController }>())
|
|
11
|
+
const mounted = useRef(true)
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
mounted.current = true
|
|
14
|
+
return () => { mounted.current = false; for (const { controller } of requests.current.values()) controller.abort() }
|
|
15
|
+
}, [])
|
|
16
|
+
|
|
17
|
+
async function upload(id: string, file: File) {
|
|
18
|
+
const controller = new AbortController()
|
|
19
|
+
requests.current.set(id, { file, controller })
|
|
20
|
+
setFiles((items) => items.map((item) => item.id === id ? { ...item, status: 'uploading', errorMessage: undefined } : item))
|
|
21
|
+
try {
|
|
22
|
+
const body = new FormData()
|
|
23
|
+
body.append('files', file)
|
|
24
|
+
const result = await json<{ files: Array<{ part: ComposerFilePart }> }>('/api/chat/upload', { method: 'POST', body, signal: controller.signal })
|
|
25
|
+
const part = result.files[0]?.part
|
|
26
|
+
if (!part || !['image', 'file'].includes(part.type) || (!part.url && !part.path)) throw new Error('Upload returned no usable file part')
|
|
27
|
+
if (!controller.signal.aborted && mounted.current) {
|
|
28
|
+
setFiles((items) => items.map((item) => item.id === id ? { ...item, status: 'ready', part } : item))
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (!controller.signal.aborted && mounted.current) {
|
|
32
|
+
setFiles((items) => items.map((item) => item.id === id ? { ...item, status: 'error', errorMessage: error instanceof Error ? error.message : 'Upload failed' } : item))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
files,
|
|
38
|
+
add(selected: FileList | File[]) {
|
|
39
|
+
for (const file of selected) {
|
|
40
|
+
const id = crypto.randomUUID()
|
|
41
|
+
setFiles((items) => [...items, { id, name: file.name, size: file.size, kind: 'file', status: 'uploading' }])
|
|
42
|
+
void upload(id, file)
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
retry(id: string) { const entry = requests.current.get(id); if (entry) void upload(id, entry.file) },
|
|
46
|
+
remove(id: string) {
|
|
47
|
+
requests.current.get(id)?.controller.abort(); requests.current.delete(id)
|
|
48
|
+
setFiles((items) => items.filter((item) => item.id !== id))
|
|
49
|
+
},
|
|
50
|
+
clear() {
|
|
51
|
+
for (const { controller } of requests.current.values()) controller.abort()
|
|
52
|
+
requests.current.clear(); setFiles([])
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8" />
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
-
<title>__PROJECT_NAME__ — dev chat</title>
|
|
7
|
-
<!--
|
|
8
|
-
The DEV chat page: a dependency-free client for the API this worker ships,
|
|
9
|
-
good enough to exercise auth, threads, multimodal turns, and streaming from
|
|
10
|
-
a browser today. It is NOT the product surface — build that in React with
|
|
11
|
-
@tangle-network/agent-app/web-react (ChatComposer, ChatMessages,
|
|
12
|
-
streamChatTurn, useChatInteractions), which owns reconnect/replay, question
|
|
13
|
-
cards, and the typed part renderers. See CUSTOMIZE.md step 5.
|
|
14
|
-
-->
|
|
15
|
-
<style>
|
|
16
|
-
:root { color-scheme: light dark; --border: #d0d3da; --muted: #6b7280; --accent: #4f46e5; }
|
|
17
|
-
@media (prefers-color-scheme: dark) { :root { --border: #333842; --muted: #9aa1ad; --accent: #818cf8; } }
|
|
18
|
-
* { box-sizing: border-box; }
|
|
19
|
-
body { margin: 0; font: 15px/1.5 system-ui, sans-serif; display: flex; height: 100dvh; }
|
|
20
|
-
aside { width: 240px; border-right: 1px solid var(--border); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
|
|
21
|
-
main { flex: 1; display: flex; flex-direction: column; }
|
|
22
|
-
header { padding: 10px 16px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
|
|
23
|
-
#transcript { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
|
|
24
|
-
.msg { max-width: 52rem; white-space: pre-wrap; }
|
|
25
|
-
.msg.user { align-self: flex-end; background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 10px; padding: 8px 12px; }
|
|
26
|
-
.msg .meta { font-size: 12px; color: var(--muted); }
|
|
27
|
-
.chip { display: inline-block; font-size: 12px; border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; margin: 2px 4px 2px 0; color: var(--muted); }
|
|
28
|
-
.reasoning { color: var(--muted); font-size: 13px; }
|
|
29
|
-
form.composer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
|
|
30
|
-
textarea { flex: 1; resize: none; font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: transparent; color: inherit; }
|
|
31
|
-
button { font: inherit; border: 1px solid var(--border); background: transparent; color: inherit; border-radius: 8px; padding: 6px 12px; cursor: pointer; }
|
|
32
|
-
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
33
|
-
.thread { text-align: left; border: none; padding: 6px 8px; border-radius: 6px; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
34
|
-
.thread.active { background: color-mix(in srgb, var(--accent) 15%, transparent); }
|
|
35
|
-
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 20px; min-width: 320px; }
|
|
36
|
-
dialog form { display: flex; flex-direction: column; gap: 10px; }
|
|
37
|
-
dialog input { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: transparent; color: inherit; }
|
|
38
|
-
#files { padding: 0 16px; }
|
|
39
|
-
.error { color: #dc2626; font-size: 13px; padding: 0 16px; }
|
|
40
|
-
</style>
|
|
41
|
-
</head>
|
|
42
|
-
<body>
|
|
43
|
-
<aside>
|
|
44
|
-
<button id="new-thread" class="primary">New thread</button>
|
|
45
|
-
<nav id="threads" style="overflow-y:auto"></nav>
|
|
46
|
-
</aside>
|
|
47
|
-
<main>
|
|
48
|
-
<header>
|
|
49
|
-
<strong>__PROJECT_NAME__ <span style="color:var(--muted);font-weight:400">dev chat</span></strong>
|
|
50
|
-
<span id="who" style="color:var(--muted);font-size:13px"></span>
|
|
51
|
-
</header>
|
|
52
|
-
<div id="transcript"></div>
|
|
53
|
-
<div id="files"></div>
|
|
54
|
-
<div id="error" class="error"></div>
|
|
55
|
-
<form class="composer" id="composer">
|
|
56
|
-
<button type="button" id="attach" title="Attach files">+</button>
|
|
57
|
-
<input type="file" id="file-input" multiple hidden />
|
|
58
|
-
<textarea id="input" rows="2" placeholder="Message the agent…"></textarea>
|
|
59
|
-
<button type="submit" class="primary">Send</button>
|
|
60
|
-
</form>
|
|
61
|
-
</main>
|
|
62
|
-
|
|
63
|
-
<dialog id="auth">
|
|
64
|
-
<form method="dialog" id="auth-form">
|
|
65
|
-
<strong id="auth-title">Sign in</strong>
|
|
66
|
-
<input id="email" type="email" placeholder="email" required autocomplete="email" />
|
|
67
|
-
<input id="password" type="password" placeholder="password" required minlength="8" autocomplete="current-password" />
|
|
68
|
-
<button class="primary" value="submit">Continue</button>
|
|
69
|
-
<button type="button" id="auth-toggle" style="border:none;color:var(--muted)">No account? Sign up</button>
|
|
70
|
-
<span id="auth-error" class="error" style="padding:0"></span>
|
|
71
|
-
</form>
|
|
72
|
-
</dialog>
|
|
73
|
-
|
|
74
|
-
<script type="module">
|
|
75
|
-
const $ = (id) => document.getElementById(id)
|
|
76
|
-
const initialThreadId = new URL(location.href).searchParams.get('threadId')
|
|
77
|
-
const state = { threadId: null, parts: [], signUp: false }
|
|
78
|
-
|
|
79
|
-
const api = async (path, init) => {
|
|
80
|
-
const res = await fetch(path, { headers: { 'content-type': 'application/json' }, ...init })
|
|
81
|
-
if (!res.ok) throw new Error((await res.json().catch(() => ({}))).error ?? `${res.status} on ${path}`)
|
|
82
|
-
return res.json()
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ── auth ────────────────────────────────────────────────────────────────────
|
|
86
|
-
async function ensureSession() {
|
|
87
|
-
const session = await fetch('/api/auth/get-session').then((r) => r.json()).catch(() => null)
|
|
88
|
-
if (session?.user) { $('who').textContent = session.user.email; return }
|
|
89
|
-
$('auth').showModal()
|
|
90
|
-
await new Promise((resolve) => {
|
|
91
|
-
$('auth-toggle').onclick = () => {
|
|
92
|
-
state.signUp = !state.signUp
|
|
93
|
-
$('auth-title').textContent = state.signUp ? 'Sign up' : 'Sign in'
|
|
94
|
-
$('auth-toggle').textContent = state.signUp ? 'Have an account? Sign in' : 'No account? Sign up'
|
|
95
|
-
}
|
|
96
|
-
$('auth-form').onsubmit = async (e) => {
|
|
97
|
-
e.preventDefault()
|
|
98
|
-
const email = $('email').value, password = $('password').value
|
|
99
|
-
try {
|
|
100
|
-
await api(`/api/auth/sign-${state.signUp ? 'up' : 'in'}/email`, {
|
|
101
|
-
method: 'POST',
|
|
102
|
-
body: JSON.stringify({ email, password, ...(state.signUp ? { name: email.split('@')[0] } : {}) }),
|
|
103
|
-
})
|
|
104
|
-
$('who').textContent = email
|
|
105
|
-
$('auth').close(); resolve()
|
|
106
|
-
} catch (err) { $('auth-error').textContent = err.message }
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// ── threads + transcript ────────────────────────────────────────────────────
|
|
112
|
-
async function loadThreads() {
|
|
113
|
-
const { threads } = await api('/api/threads')
|
|
114
|
-
$('threads').replaceChildren(...threads.map((t) => {
|
|
115
|
-
const b = document.createElement('button')
|
|
116
|
-
b.className = `thread${t.id === state.threadId ? ' active' : ''}`
|
|
117
|
-
b.textContent = t.title
|
|
118
|
-
b.onclick = () => openThread(t.id)
|
|
119
|
-
return b
|
|
120
|
-
}))
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function row(role, text = '') {
|
|
124
|
-
const div = document.createElement('div')
|
|
125
|
-
div.className = `msg ${role}`
|
|
126
|
-
div.textContent = text
|
|
127
|
-
$('transcript').append(div)
|
|
128
|
-
div.scrollIntoView()
|
|
129
|
-
return div
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Chips are built with textContent — part fields are agent-produced strings
|
|
133
|
-
// and must never be interpreted as markup.
|
|
134
|
-
function chip(container, text) {
|
|
135
|
-
const wrap = document.createElement('div')
|
|
136
|
-
const span = document.createElement('span')
|
|
137
|
-
span.className = 'chip'
|
|
138
|
-
span.textContent = text
|
|
139
|
-
wrap.append(span)
|
|
140
|
-
container.append(wrap)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function renderParts(container, parts = []) {
|
|
144
|
-
for (const p of parts) {
|
|
145
|
-
if (p.type === 'tool') chip(container, `${p.tool ?? 'tool'} · ${p.state?.status ?? ''}`)
|
|
146
|
-
if (p.type === 'file' || p.type === 'image') chip(container, `📎 ${p.filename ?? p.path ?? p.type}`)
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async function openThread(id) {
|
|
151
|
-
state.threadId = id
|
|
152
|
-
const url = new URL(location.href)
|
|
153
|
-
url.searchParams.set('threadId', id)
|
|
154
|
-
history.replaceState(null, '', url)
|
|
155
|
-
const { messages } = await api(`/api/threads/${id}/messages`)
|
|
156
|
-
$('transcript').replaceChildren()
|
|
157
|
-
for (const m of messages) {
|
|
158
|
-
const div = row(m.role, m.content)
|
|
159
|
-
renderParts(div, m.parts)
|
|
160
|
-
if (m.role === 'assistant' && m.inputTokens != null) {
|
|
161
|
-
const meta = document.createElement('div')
|
|
162
|
-
meta.className = 'meta'
|
|
163
|
-
meta.textContent = `${m.model ?? ''} · ${m.inputTokens}→${m.outputTokens} tokens`
|
|
164
|
-
div.append(meta)
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
loadThreads()
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// ── uploads → prompt parts ──────────────────────────────────────────────────
|
|
171
|
-
$('attach').onclick = () => $('file-input').click()
|
|
172
|
-
$('file-input').onchange = async () => {
|
|
173
|
-
const form = new FormData()
|
|
174
|
-
for (const f of $('file-input').files) form.append('files', f)
|
|
175
|
-
const res = await fetch('/api/chat/upload', { method: 'POST', body: form })
|
|
176
|
-
if (!res.ok) { $('error').textContent = (await res.json()).error; return }
|
|
177
|
-
const { files } = await res.json()
|
|
178
|
-
state.parts.push(...files.map((f) => f.part))
|
|
179
|
-
$('files').replaceChildren(...state.parts.map((p) => Object.assign(document.createElement('span'), { className: 'chip', textContent: p.filename ?? p.path ?? p.type })))
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// ── one streamed turn ───────────────────────────────────────────────────────
|
|
183
|
-
// Minimal NDJSON consumer for the dev page. The production client is
|
|
184
|
-
// streamChatTurn from @tangle-network/agent-app/web-react — it also resumes
|
|
185
|
-
// via /api/chat/replay/:turnId after a dropped connection.
|
|
186
|
-
async function runTurn(content, parts) {
|
|
187
|
-
const res = await fetch('/api/chat', {
|
|
188
|
-
method: 'POST',
|
|
189
|
-
headers: { 'content-type': 'application/json' },
|
|
190
|
-
body: JSON.stringify({ threadId: state.threadId, content, parts }),
|
|
191
|
-
})
|
|
192
|
-
if (!res.ok) { $('error').textContent = (await res.json()).error; return }
|
|
193
|
-
const assistant = row('assistant')
|
|
194
|
-
let text = ''
|
|
195
|
-
const reader = res.body.getReader()
|
|
196
|
-
const decoder = new TextDecoder()
|
|
197
|
-
let buffer = ''
|
|
198
|
-
const handle = (line) => {
|
|
199
|
-
if (!line.trim()) return
|
|
200
|
-
let parsed; try { parsed = JSON.parse(line) } catch { return }
|
|
201
|
-
const evt = parsed.kind === 'event' ? parsed.event : parsed
|
|
202
|
-
if (evt.type === 'text') { text += evt.text; assistant.textContent = text }
|
|
203
|
-
else if (evt.type === 'reasoning') { /* keep the dev page quiet; web-react renders these */ }
|
|
204
|
-
else if (evt.type === 'tool_call') chip(assistant, `${evt.call?.toolName ?? 'tool'}…`)
|
|
205
|
-
else if (evt.type === 'error') $('error').textContent = String(evt.details ?? evt.error)
|
|
206
|
-
assistant.scrollIntoView()
|
|
207
|
-
}
|
|
208
|
-
for (;;) {
|
|
209
|
-
const { done, value } = await reader.read()
|
|
210
|
-
if (done) { if (buffer.trim()) handle(buffer); break }
|
|
211
|
-
buffer += decoder.decode(value, { stream: true })
|
|
212
|
-
const lines = buffer.split('\n'); buffer = lines.pop() ?? ''
|
|
213
|
-
lines.forEach(handle)
|
|
214
|
-
}
|
|
215
|
-
openThread(state.threadId)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
$('composer').onsubmit = async (e) => {
|
|
219
|
-
e.preventDefault()
|
|
220
|
-
const content = $('input').value.trim()
|
|
221
|
-
const parts = state.parts.splice(0)
|
|
222
|
-
if (!content && parts.length === 0) return
|
|
223
|
-
$('error').textContent = ''
|
|
224
|
-
$('files').replaceChildren()
|
|
225
|
-
$('input').value = ''
|
|
226
|
-
if (!state.threadId) {
|
|
227
|
-
const { thread } = await api('/api/threads', { method: 'POST', body: JSON.stringify({ firstMessage: content || 'New thread' }) })
|
|
228
|
-
state.threadId = thread.id
|
|
229
|
-
}
|
|
230
|
-
row('user', content)
|
|
231
|
-
await runTurn(content, parts).catch((err) => { $('error').textContent = err.message })
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
$('new-thread').onclick = () => {
|
|
235
|
-
state.threadId = null
|
|
236
|
-
const url = new URL(location.href)
|
|
237
|
-
url.searchParams.delete('threadId')
|
|
238
|
-
history.replaceState(null, '', url)
|
|
239
|
-
$('transcript').replaceChildren()
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
await ensureSession()
|
|
243
|
-
await loadThreads()
|
|
244
|
-
if (initialThreadId) {
|
|
245
|
-
await openThread(initialThreadId).catch((err) => {
|
|
246
|
-
state.threadId = null
|
|
247
|
-
$('error').textContent = err.message
|
|
248
|
-
const url = new URL(location.href)
|
|
249
|
-
url.searchParams.delete('threadId')
|
|
250
|
-
history.replaceState(null, '', url)
|
|
251
|
-
})
|
|
252
|
-
}
|
|
253
|
-
</script>
|
|
254
|
-
</body>
|
|
255
|
-
</html>
|