@tangle-network/create-agent-app 0.60.220 → 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
|
|
@@ -40,7 +41,6 @@ import {
|
|
|
40
41
|
type ChatStore,
|
|
41
42
|
} from '@tangle-network/agent-app/chat-store'
|
|
42
43
|
import { guardResolution } from '@tangle-network/agent-app/platform'
|
|
43
|
-
import { createIntelligenceTurnTraceSink } from '@tangle-network/agent-app/turn-trace'
|
|
44
44
|
import {
|
|
45
45
|
createD1TurnEventStore,
|
|
46
46
|
type TurnEventStore,
|
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
import { drizzle } from 'drizzle-orm/d1'
|
|
49
49
|
import { accounts, messages, sessions, threads, users, verifications } from './db/schema'
|
|
50
50
|
import type { AppEnv } from './env'
|
|
51
|
-
import { appSlug,
|
|
51
|
+
import { appSlug, createHybridLane, createPrewarmRoute, resolveSidecarConnection, resolveUploadSink } from './sandbox'
|
|
52
52
|
|
|
53
53
|
export interface ChatAppOverrides {
|
|
54
54
|
/** Test seam: an in-memory drizzle db (the e2e test runs the REAL
|
|
@@ -56,7 +56,7 @@ export interface ChatAppOverrides {
|
|
|
56
56
|
db?: ChatDatabase
|
|
57
57
|
/** Test seam: `createMemoryTurnEventStore()`. Default: D1-backed buffer. */
|
|
58
58
|
turnStore?: TurnEventStore
|
|
59
|
-
/** Test seam: a fake
|
|
59
|
+
/** Test seam: a fake producer. Default: the real hybrid lane. */
|
|
60
60
|
produce?: (args: ChatTurnProduceArgs<void>) => ChatTurnRouteProducer | Promise<ChatTurnRouteProducer>
|
|
61
61
|
/** Test seam: where large uploads land. Default: the workspace box's fs. */
|
|
62
62
|
uploadSink?: (scope: { workspaceId: string; userId: string }) => Promise<SandboxUploadSink | null>
|
|
@@ -80,6 +80,8 @@ export interface ChatApp {
|
|
|
80
80
|
threadMessages(request: Request, params: { threadId: string }): Promise<Response>
|
|
81
81
|
}
|
|
82
82
|
upload(request: Request): Promise<Response>
|
|
83
|
+
/** POST: warm the open workspace's box (`AgentWorkspaceLayout`'s `prewarm`). */
|
|
84
|
+
prewarm(request: Request): Promise<Response>
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
const notFound = () => Response.json({ error: 'Thread not found' }, { status: 404 })
|
|
@@ -143,7 +145,8 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
143
145
|
: null
|
|
144
146
|
interface Admission { handle: unknown; started: boolean; producer?: ChatTurnRouteProducer; userMessageId?: string }
|
|
145
147
|
const admissions = new Map<string, Admission>()
|
|
146
|
-
const
|
|
148
|
+
const lane = overrides.produce ? null : createHybridLane(env)
|
|
149
|
+
const produce = overrides.produce ?? lane!.produce
|
|
147
150
|
const routes = createChatTurnRoutes<void>({
|
|
148
151
|
turnLock: {
|
|
149
152
|
acquire: async args => {
|
|
@@ -163,13 +166,15 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
163
166
|
await durableLock?.release(admission.handle)
|
|
164
167
|
},
|
|
165
168
|
},
|
|
169
|
+
// `projectId` also names the Intelligence project every turn's trace goes to (first_text_ms included). The route
|
|
170
|
+
// reads the Sandbox product key and warns in the Worker log when an export fails; pass `turnTrace: false` only
|
|
171
|
+
// with an exemption in signoff.config.mjs.
|
|
166
172
|
projectId: appSlug,
|
|
167
|
-
// One trace per turn, sent to Tangle Intelligence with first_text_ms. The sink reads the Sandbox product key and
|
|
168
|
-
// warns in the Worker log when an export fails; pass `turnTrace: false` only with an exemption in signoff.config.mjs.
|
|
169
|
-
turnTrace: { sink: createIntelligenceTurnTraceSink({ project: appSlug }) },
|
|
170
173
|
authorize,
|
|
171
174
|
store,
|
|
172
175
|
turnStore,
|
|
176
|
+
// The box starts the moment the caller is admitted when the ask needs a computer.
|
|
177
|
+
...(lane ? { afterAuthorize: lane.afterAuthorize } : {}),
|
|
173
178
|
beforeTurn: async args => {
|
|
174
179
|
const admission = admissions.get(args.executionId)
|
|
175
180
|
if (args.userMessageId) {
|
|
@@ -255,11 +260,17 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
|
|
|
255
260
|
return Response.json({ thread, messages: await store.listMessages(params.threadId) })
|
|
256
261
|
}
|
|
257
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
|
+
|
|
258
268
|
return {
|
|
259
269
|
auth,
|
|
260
270
|
store,
|
|
261
271
|
turnStore,
|
|
262
272
|
routes: { ...routes, createThread, listThreads, threadMessages },
|
|
263
273
|
upload,
|
|
274
|
+
prewarm: request => prewarmRoute(request),
|
|
264
275
|
}
|
|
265
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>}
|