@tangle-network/create-agent-app 0.60.221 → 0.60.222
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/package.json
CHANGED
package/template-chat/AGENTS.md
CHANGED
|
@@ -39,9 +39,9 @@ Keep browser DOM types separate from Worker types and scan installed UI package
|
|
|
39
39
|
|
|
40
40
|
## Defaults
|
|
41
41
|
|
|
42
|
-
This app inherits agent-app's defaults: a trace per turn sent to Intelligence, limit alarms on D1, and the shared Worker settings.
|
|
42
|
+
This app inherits agent-app's defaults: the hybrid turn (the Router answers first and escalates to the box through `sandbox_run`), the shared prewarm route, a trace per turn sent to Intelligence, limit alarms on D1, and the shared Worker settings.
|
|
43
43
|
`pnpm signoff` runs the checks in `signoff.config.mjs` and reports which defaults the app has (`agent-app-signoff defaults` prints the same report alone).
|
|
44
|
-
Turn a default off only with an exemption and its reason in `signoff.config.mjs`; the
|
|
44
|
+
Turn a default off only with an exemption and its reason in `signoff.config.mjs`; the placement exemption there is temporary.
|
|
45
45
|
|
|
46
46
|
## Verification
|
|
47
47
|
|
|
@@ -14,7 +14,6 @@ export default {
|
|
|
14
14
|
agentApp: {
|
|
15
15
|
// An exemption is a default this app does not have yet, with the reason. Delete each line as it is fixed.
|
|
16
16
|
exemptions: {
|
|
17
|
-
prewarm: 'This scaffold has no workspace-open route yet; agent-app mounts the shared prewarm route by default in a later release.',
|
|
18
17
|
placement: 'The D1 region is unknown until `wrangler d1 create` runs; pin [placement] beside the D1 primary then, and delete this line.',
|
|
19
18
|
},
|
|
20
19
|
},
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* turn `createChatTurnRoutes` — body validation, turn identity,
|
|
9
9
|
* the default turn-buffer tap (replay after a drop), user and
|
|
10
10
|
* assistant rows persisted with typed parts + usage receipt
|
|
11
|
-
* producer the
|
|
11
|
+
* producer the hybrid lane from `src/sandbox.ts`: the Router answers
|
|
12
|
+
* first and escalates to the box through `sandbox_run`
|
|
12
13
|
* uploads `createUploadRoute` — small files inline (`data:` URI),
|
|
13
14
|
* large files into the sandbox workspace by path
|
|
14
15
|
* asks `/interactions` list/answer endpoints over the sidecar
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
import { drizzle } from 'drizzle-orm/d1'
|
|
48
49
|
import { accounts, messages, sessions, threads, users, verifications } from './db/schema'
|
|
49
50
|
import type { AppEnv } from './env'
|
|
50
|
-
import { appSlug,
|
|
51
|
+
import { appSlug, createHybridLane, createPrewarmRoute, resolveSidecarConnection, resolveUploadSink } from './sandbox'
|
|
51
52
|
|
|
52
53
|
export interface ChatAppOverrides {
|
|
53
54
|
/** Test seam: an in-memory drizzle db (the e2e test runs the REAL
|
|
@@ -55,7 +56,7 @@ export interface ChatAppOverrides {
|
|
|
55
56
|
db?: ChatDatabase
|
|
56
57
|
/** Test seam: `createMemoryTurnEventStore()`. Default: D1-backed buffer. */
|
|
57
58
|
turnStore?: TurnEventStore
|
|
58
|
-
/** Test seam: a fake
|
|
59
|
+
/** Test seam: a fake producer. Default: the real hybrid lane. */
|
|
59
60
|
produce?: (args: ChatTurnProduceArgs<void>) => ChatTurnRouteProducer | Promise<ChatTurnRouteProducer>
|
|
60
61
|
/** Test seam: where large uploads land. Default: the workspace box's fs. */
|
|
61
62
|
uploadSink?: (scope: { workspaceId: string; userId: string }) => Promise<SandboxUploadSink | null>
|
|
@@ -79,6 +80,8 @@ export interface ChatApp {
|
|
|
79
80
|
threadMessages(request: Request, params: { threadId: string }): Promise<Response>
|
|
80
81
|
}
|
|
81
82
|
upload(request: Request): Promise<Response>
|
|
83
|
+
/** POST: warm the open workspace's box (`AgentWorkspaceLayout`'s `prewarm`). */
|
|
84
|
+
prewarm(request: Request): Promise<Response>
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
const notFound = () => Response.json({ error: 'Thread not found' }, { status: 404 })
|
|
@@ -142,7 +145,8 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
142
145
|
: null
|
|
143
146
|
interface Admission { handle: unknown; started: boolean; producer?: ChatTurnRouteProducer; userMessageId?: string }
|
|
144
147
|
const admissions = new Map<string, Admission>()
|
|
145
|
-
const
|
|
148
|
+
const lane = overrides.produce ? null : createHybridLane(env)
|
|
149
|
+
const produce = overrides.produce ?? lane!.produce
|
|
146
150
|
const routes = createChatTurnRoutes<void>({
|
|
147
151
|
turnLock: {
|
|
148
152
|
acquire: async args => {
|
|
@@ -169,6 +173,8 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
169
173
|
authorize,
|
|
170
174
|
store,
|
|
171
175
|
turnStore,
|
|
176
|
+
// The box starts the moment the caller is admitted when the ask needs a computer.
|
|
177
|
+
...(lane ? { afterAuthorize: lane.afterAuthorize } : {}),
|
|
172
178
|
beforeTurn: async args => {
|
|
173
179
|
const admission = admissions.get(args.executionId)
|
|
174
180
|
if (args.userMessageId) {
|
|
@@ -254,11 +260,17 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
254
260
|
return Response.json({ thread, messages: await store.listMessages(params.threadId) })
|
|
255
261
|
}
|
|
256
262
|
|
|
263
|
+
const prewarmRoute = createPrewarmRoute(env, async request => {
|
|
264
|
+
const session = await requireUser(request)
|
|
265
|
+
return session.ok ? session.value.user.id : null
|
|
266
|
+
})
|
|
267
|
+
|
|
257
268
|
return {
|
|
258
269
|
auth,
|
|
259
270
|
store,
|
|
260
271
|
turnStore,
|
|
261
272
|
routes: { ...routes, createThread, listThreads, threadMessages },
|
|
262
273
|
upload,
|
|
274
|
+
prewarm: request => prewarmRoute(request),
|
|
263
275
|
}
|
|
264
276
|
}
|
|
@@ -18,8 +18,14 @@
|
|
|
18
18
|
|
|
19
19
|
import { config } from '../agent.config'
|
|
20
20
|
import {
|
|
21
|
+
createHybridTurnProducer,
|
|
21
22
|
createSandboxChatProducer,
|
|
22
23
|
normalizeChatPromptForSandbox,
|
|
24
|
+
routerTurnHistory,
|
|
25
|
+
startHybridBox,
|
|
26
|
+
turnCarriesFiles,
|
|
27
|
+
turnPromptText,
|
|
28
|
+
type ChatTurnAfterAuthorizeArgs,
|
|
23
29
|
type ChatTurnProduceArgs,
|
|
24
30
|
type ChatTurnRouteProducer,
|
|
25
31
|
type SandboxUploadSink,
|
|
@@ -27,13 +33,17 @@ import {
|
|
|
27
33
|
import type { SidecarInteractionsConnection } from '@tangle-network/agent-app/interactions'
|
|
28
34
|
import {
|
|
29
35
|
createD1PrewarmClaimStore,
|
|
36
|
+
createSandboxPrewarmer,
|
|
37
|
+
createWorkspacePrewarmRoute,
|
|
30
38
|
ensureWorkspaceSandbox,
|
|
31
39
|
peekWorkspaceSandbox,
|
|
32
40
|
runForegroundSandboxSingleFlight,
|
|
41
|
+
sandboxPromptRunner,
|
|
33
42
|
streamSandboxPrompt,
|
|
34
43
|
type SandboxRuntimeConfig,
|
|
35
44
|
} from '@tangle-network/agent-app/sandbox'
|
|
36
45
|
import type { TurnTrace } from '@tangle-network/agent-app/turn-trace'
|
|
46
|
+
import type { SandboxInstance } from '@tangle-network/sandbox'
|
|
37
47
|
import type { AppEnv } from './env'
|
|
38
48
|
|
|
39
49
|
/** Lowercased, non-alphanumerics collapsed: box names + projectId. */
|
|
@@ -162,6 +172,100 @@ export function createSandboxProduce(env: AppEnv) {
|
|
|
162
172
|
}
|
|
163
173
|
}
|
|
164
174
|
|
|
175
|
+
/** The Router's OpenAI-compatible base URL (`…/v1`), or undefined for the platform default. */
|
|
176
|
+
function routerBaseUrl(env: AppEnv): string | undefined {
|
|
177
|
+
const url = env.TANGLE_ROUTER_URL?.trim().replace(/\/+$/, '')
|
|
178
|
+
if (!url) return undefined
|
|
179
|
+
return url.endsWith('/v1') ? url : `${url}/v1`
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The default chat lane: the model answers first through the Tangle Router and
|
|
184
|
+
* escalates to the workspace box through one `sandbox_run` call when the work
|
|
185
|
+
* needs a computer (`createHybridTurnProducer`). The box starts at admission
|
|
186
|
+
* when the ask needs it (`afterAuthorize`), or when the model names
|
|
187
|
+
* `sandbox_run`, and that one start serves the call. A turn that carries files,
|
|
188
|
+
* or a deployment with no Router key, runs on the sandbox lane, where the
|
|
189
|
+
* files are.
|
|
190
|
+
*
|
|
191
|
+
* Pass `afterAuthorize` and `produce` to `createChatTurnRoutes`.
|
|
192
|
+
*/
|
|
193
|
+
export function createHybridLane(env: AppEnv) {
|
|
194
|
+
const shell = createSandboxShell(env)
|
|
195
|
+
const sandboxProduce = createSandboxProduce(env)
|
|
196
|
+
// The box a turn's admission started, by thread; `produce` takes it in the same request.
|
|
197
|
+
const started = new Map<string, Promise<SandboxInstance>>()
|
|
198
|
+
const acquire = (scope: { workspaceId: string; userId: string }, trace?: TurnTrace) =>
|
|
199
|
+
ensureForegroundWorkspaceSandbox(env, shell, scope, trace)
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
afterAuthorize({ body, tenantId, userId, threadId, trace }: ChatTurnAfterAuthorizeArgs<void>) {
|
|
203
|
+
if (!env.TANGLE_API_KEY?.trim()) return undefined
|
|
204
|
+
const start = startHybridBox(body, () => acquire({ workspaceId: tenantId, userId }, trace))
|
|
205
|
+
if (!start) return undefined
|
|
206
|
+
started.set(threadId, start)
|
|
207
|
+
// A turn that ends before `produce` leaves the box to idle out.
|
|
208
|
+
return () => { started.delete(threadId) }
|
|
209
|
+
},
|
|
210
|
+
async produce(args: ChatTurnProduceArgs<void>): Promise<ChatTurnRouteProducer> {
|
|
211
|
+
const { body, identity, prompt, priorMessages, trace } = args
|
|
212
|
+
const box = started.get(identity.sessionId)
|
|
213
|
+
started.delete(identity.sessionId)
|
|
214
|
+
const apiKey = env.TANGLE_API_KEY?.trim()
|
|
215
|
+
if (!apiKey || turnCarriesFiles(prompt)) return sandboxProduce(args)
|
|
216
|
+
const model = body.model ?? env.MODEL_NAME ?? config.model.default
|
|
217
|
+
const baseUrl = routerBaseUrl(env)
|
|
218
|
+
const scope = { workspaceId: identity.tenantId, userId: identity.userId }
|
|
219
|
+
return createHybridTurnProducer({
|
|
220
|
+
apiKey,
|
|
221
|
+
model,
|
|
222
|
+
...(baseUrl ? { baseUrl } : {}),
|
|
223
|
+
systemPrompt: config.systemPrompt,
|
|
224
|
+
userMessage: turnPromptText(prompt),
|
|
225
|
+
history: routerTurnHistory(priorMessages),
|
|
226
|
+
...(trace ? { trace } : {}),
|
|
227
|
+
box: {
|
|
228
|
+
acquire: () => acquire(scope, trace),
|
|
229
|
+
...(box ? { started: box } : {}),
|
|
230
|
+
// The box agent keeps its own session per thread, so a sandbox-lane turn on the same thread never collides with it.
|
|
231
|
+
runTask: sandboxPromptRunner(shell, { model, harness: config.harness, sessionId: `sandbox-run-${identity.sessionId}` }),
|
|
232
|
+
},
|
|
233
|
+
})
|
|
234
|
+
},
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Warm the open workspace's box (`POST /api/chat/prewarm`): the shared route
|
|
240
|
+
* behind `AgentWorkspaceLayout`'s `prewarm`. The default lane answers on the
|
|
241
|
+
* Router and starts the box only when a turn needs it, so with a Router key a
|
|
242
|
+
* warm on open is declined and the speculator starts the box instead.
|
|
243
|
+
*/
|
|
244
|
+
export function createPrewarmRoute(env: AppEnv, requireUserId: (request: Request) => Promise<string | null>) {
|
|
245
|
+
return createWorkspacePrewarmRoute<void>({
|
|
246
|
+
authorize: async ({ request }) => {
|
|
247
|
+
const userId = await requireUserId(request)
|
|
248
|
+
if (!userId) return { status: 'denied', response: Response.json({ error: 'Unauthorized' }, { status: 401 }) }
|
|
249
|
+
if (env.TANGLE_API_KEY?.trim()) return { status: 'declined', reason: 'router_lane' }
|
|
250
|
+
if (!env.SANDBOX_API_KEY?.trim() || !env.SANDBOX_GATEWAY_URL?.trim()) return { status: 'declined', reason: 'no_sandbox' }
|
|
251
|
+
const scope = { workspaceId: userId, userId }
|
|
252
|
+
return {
|
|
253
|
+
status: 'allowed',
|
|
254
|
+
key: scope.workspaceId,
|
|
255
|
+
warm: async () => {
|
|
256
|
+
const prewarmer = createSandboxPrewarmer(createSandboxShell(env), {
|
|
257
|
+
claim: createD1PrewarmClaimStore(env.DB),
|
|
258
|
+
mode: 'create-or-resume',
|
|
259
|
+
})
|
|
260
|
+
const decision = await prewarmer.prewarm({ ...scope, harness: config.harness })
|
|
261
|
+
await decision.completion
|
|
262
|
+
return { outcome: decision.outcome }
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
165
269
|
/** Where large uploads land: the workspace box's filesystem. Returns null when
|
|
166
270
|
* no sandbox is configured — the upload route then accepts inline files only
|
|
167
271
|
* and rejects oversized ones with an explicit 413. */
|
|
@@ -132,6 +132,7 @@ export function createWorker(assembly: WorkerAssembly = defaultAssembly): Export
|
|
|
132
132
|
// reloaded mid-turn re-attaches via /replay instead of losing the run.
|
|
133
133
|
if (pathname === '/api/chat/running' && method === 'GET') return app.routes.running(request)
|
|
134
134
|
if (pathname === '/api/chat/upload' && method === 'POST') return app.upload(request)
|
|
135
|
+
if (pathname === '/api/chat/prewarm' && method === 'POST') return app.prewarm(request)
|
|
135
136
|
if (pathname === '/api/chat/interactions' && app.routes.interactions) {
|
|
136
137
|
if (method === 'GET') return app.routes.interactions.list(request)
|
|
137
138
|
if (method === 'POST') return app.routes.interactions.answer(request)
|
|
@@ -85,7 +85,8 @@ function Workspace({ session }: { session: Session }) {
|
|
|
85
85
|
activeId={showApiAccess ? 'api-access' : showHistory ? 'history' : activeId ? undefined : 'new'}
|
|
86
86
|
logo={<span className="flex h-8 w-8 items-center justify-center rounded-lg bg-primary text-primary-foreground" title={productName}>{productName.slice(0, 1)}</span>}
|
|
87
87
|
logoHref="/" user={session.user} settingsHref={null} onLogout={() => void logout()}
|
|
88
|
-
contentClassName="h-dvh min-h-0 overflow-hidden" defaultRailCollapsed={false}
|
|
88
|
+
contentClassName="h-dvh min-h-0 overflow-hidden" defaultRailCollapsed={false}
|
|
89
|
+
prewarm={{ url: '/api/chat/prewarm' }}>
|
|
89
90
|
{error && <div role="alert" className="p-4 text-sm text-destructive">{error} <button className="underline" onClick={() => location.reload()}>Reload</button></div>}
|
|
90
91
|
{showApiAccess ? <ApiAccess /> : showHistory ? loaded ? <HistoryPage threads={threads} /> : <p role="status" className="p-6">Loading History…</p>
|
|
91
92
|
: <AgentWorkspaceCompanion tabs={workspaceTools} persistenceKey={`workspace:${session.user.id}:tools`}><Conversation initialThreadId={initialThreadId} onThread={setActiveId} onChanged={refreshThreads} /></AgentWorkspaceCompanion>}
|