mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-30 06:17:06 +00:00
* feat(hub,shared): scratchlist v2.2 hub attachment storage foundation (#921) Hub stores scratchlist attachment bytes on filesystem; SQLite holds AttachmentMetadata[] JSON via session_scratchlist.attachments (v11→v12). Upstream ladder: v10→v11 text-only scratchlist table (#896), v11→v12 attachments column. Configurable limits via HAPI_SCRATCHLIST_* env vars. Upload, serve, and limits REST routes; delete entry cleans hub files. Web promote/rehydrate still TODO. Soup renumber branch follows. Co-authored-by: Cursor <cursoragent@cursor.com> * feat(web): scratchlist v2.2 attachment UX (#921) Route scratchlist-mode composer submits with attachments to hub storage, show image thumbnails in the drawer, and rehydrate attachments on promote to composer or queue (hub fetch → CLI upload for send). Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): scratchlist attach submit, float thumbs, copy tooltip (#921) Hub upload adapter now sets path on ready attachments so the composer send button unlocks in scratchlist mode; routing label matches attachments too. Entry thumbnails float left with text wrap; copy tooltip clarifies text-only. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): adapt scratchlist update tests to patch API (#921) update() now takes { text?, attachments? }; v12 CRUD tests still passed a string. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): harden scratchlist attachment ownership and orphan cleanup Resolve claimed hub paths against the current session before persist, count on-disk session bytes for upload caps, delete blobs dropped on entry update, and DELETE pending uploads when composer remove runs. Co-authored-by: Cursor <cursoragent@cursor.com> * chore: drop accidental .cursor files from attachment PR Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): exit scratchlist mode before rehydrate; delete raced uploads Promote-to-composer flushes mode exit so attachments use the chat adapter. Cancel-during-upload deletes the hub blob once upload returns. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): exact UUID delete match; stage hub paths on chat send Reject partial attachment ids on disk delete, and restage scratchlist hub attachments through uploadFile when sending after leaving scratchlist mode. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): skip text-only PUT resolve; cleanup session attachment dirs Text-only edits keep existing attachment metadata after session-id transfer. Require full UUID on resolve. Delete scratchlist attachment files when a session is deleted. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): scratchlist attach route, PUT bytes, orphan deletes Park only hub-resident attachments; subtract removed blobs from the PUT session cap; delete attachment files only when no other entry still references them. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): canonicalize scratchlist attachment filenames Resolve stores the on-disk sanitized name (not claimed.filename) and hardens Content-Disposition against CR/LF/quote injection. Co-authored-by: Cursor <cursoragent@cursor.com> * test(hub): cover toxic filename canonicalize on resolve Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): serialize scratchlist uploads; drop hub blobs after chat stage Per-session upload lock keeps disk byte caps honest under concurrency. After a successful toggle-off chat send, delete the staged hub copies so they no longer count against the session attachment budget. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(shared,web): allow clearing scratchlist attachments; cleanup staged uploads PUT may send attachments:[] without a text change. Staging to chat rolls back partial normal-upload copies on failure. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): re-key scratchlist attachment files on session merge Move hub blobs when scratchlist rows transfer between session ids so quota and path ownership stay correct. Reject PUT that would leave an empty textless entry after clearing attachments. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): reuse restored scratchlist hub attachments without re-upload Composer draft remount was re-uploading blobs that already had a hapi-hub:scratchlist path, orphaning the originals against session quota. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
447 lines
16 KiB
TypeScript
447 lines
16 KiB
TypeScript
import { z } from 'zod'
|
||
import { CODEX_COLLABORATION_MODES, PERMISSION_MODES } from './modes'
|
||
|
||
export const PermissionModeSchema = z.enum(PERMISSION_MODES)
|
||
export const CodexCollaborationModeSchema = z.enum(CODEX_COLLABORATION_MODES)
|
||
export const SessionEndReasonSchema = z.enum(['completed', 'terminated', 'error', 'handoff'])
|
||
export type SessionEndReason = z.infer<typeof SessionEndReasonSchema>
|
||
|
||
const MetadataSummarySchema = z.object({
|
||
text: z.string(),
|
||
updatedAt: z.number()
|
||
})
|
||
|
||
const SessionCapabilitiesSchema = z.object({
|
||
terminal: z.boolean().optional()
|
||
})
|
||
|
||
export const WorktreeMetadataSchema = z.object({
|
||
basePath: z.string(),
|
||
branch: z.string(),
|
||
name: z.string(),
|
||
worktreePath: z.string().optional(),
|
||
createdAt: z.number().optional()
|
||
})
|
||
|
||
export type WorktreeMetadata = z.infer<typeof WorktreeMetadataSchema>
|
||
|
||
export const MetadataSchema = z.object({
|
||
path: z.string(),
|
||
host: z.string(),
|
||
version: z.string().optional(),
|
||
name: z.string().optional(),
|
||
os: z.string().optional(),
|
||
summary: MetadataSummarySchema.optional(),
|
||
machineId: z.string().optional(),
|
||
claudeSessionId: z.string().optional(),
|
||
codexSessionId: z.string().optional(),
|
||
// 原始 Codex thread id。导入 Codex 历史后,HAPI 会 fork 出自己的续写 thread;
|
||
// codexSessionId 保存 fork 后的 thread,codexSourceSessionId 保留来源 thread 便于同步/展示。
|
||
codexSourceSessionId: z.string().optional(),
|
||
geminiSessionId: z.string().optional(),
|
||
opencodeSessionId: z.string().optional(),
|
||
grokSessionId: z.string().optional(),
|
||
cursorSessionId: z.string().optional(),
|
||
cursorSessionProtocol: z.enum(['acp', 'stream-json']).optional(),
|
||
// Drives the web `CursorMigrationBanner`:
|
||
// 'in_progress' = legacy-to-ACP transplant currently running; banner shows spinner + "Upgrading..."
|
||
// 'ambiguous' = migrator refused to transplant (ambiguous source drawer OR size mismatch);
|
||
// banner switches to "Manual review needed" until the operator resolves on disk.
|
||
// undefined = no migration in flight; banner hidden.
|
||
// tiann/hapi#873.
|
||
cursorMigrationState: z.enum(['in_progress', 'ambiguous']).optional(),
|
||
kimiSessionId: z.string().optional(),
|
||
piSessionId: z.string().optional(),
|
||
tools: z.array(z.string()).optional(),
|
||
slashCommands: z.array(z.string()).optional(),
|
||
homeDir: z.string().optional(),
|
||
happyHomeDir: z.string().optional(),
|
||
happyLibDir: z.string().optional(),
|
||
happyToolsDir: z.string().optional(),
|
||
startedFromRunner: z.boolean().optional(),
|
||
hostPid: z.number().optional(),
|
||
hapiMcpUrl: z.string().url().optional(),
|
||
startedBy: z.enum(['runner', 'terminal']).optional(),
|
||
lifecycleState: z.string().optional(),
|
||
lifecycleStateSince: z.number().optional(),
|
||
archivedBy: z.string().optional(),
|
||
archiveReason: z.string().optional(),
|
||
preferredPermissionMode: PermissionModeSchema.optional(),
|
||
flavor: z.string().nullish(),
|
||
capabilities: SessionCapabilitiesSchema.optional(),
|
||
worktree: WorktreeMetadataSchema.optional(),
|
||
// Cached Pi model list — written by CLI, read by web (inactive session fallback).
|
||
// Minimal shape: each entry must have modelId; other fields (provider, name, etc.) pass through.
|
||
piAvailableModels: z.array(z.object({ modelId: z.string() }).passthrough()).optional(),
|
||
// Pi-selected model with provider identity. The legacy `session.model`
|
||
// field stores only modelId (shared across all flavors); this preserves
|
||
// the provider so web can resolve the exact model when two providers
|
||
// share a modelId.
|
||
piSelectedModel: z.object({ provider: z.string(), modelId: z.string() }).nullable().optional()
|
||
})
|
||
|
||
export type Metadata = z.infer<typeof MetadataSchema>
|
||
|
||
export const AgentStateRequestSchema = z.object({
|
||
tool: z.string(),
|
||
arguments: z.unknown(),
|
||
createdAt: z.number().nullish()
|
||
})
|
||
|
||
export type AgentStateRequest = z.infer<typeof AgentStateRequestSchema>
|
||
|
||
export const AgentStateCompletedRequestSchema = z.object({
|
||
tool: z.string(),
|
||
arguments: z.unknown(),
|
||
createdAt: z.number().nullish(),
|
||
completedAt: z.number().nullish(),
|
||
status: z.enum(['canceled', 'denied', 'approved']),
|
||
reason: z.string().optional(),
|
||
mode: z.string().optional(),
|
||
decision: z.enum(['approved', 'approved_for_session', 'denied', 'abort']).optional(),
|
||
allowTools: z.array(z.string()).optional(),
|
||
// Flat format: Record<string, string[]> (AskUserQuestion)
|
||
// Nested format: Record<string, { answers: string[] }> (request_user_input)
|
||
answers: z.union([
|
||
z.record(z.string(), z.array(z.string())),
|
||
z.record(z.string(), z.object({ answers: z.array(z.string()) }))
|
||
]).optional()
|
||
})
|
||
|
||
export type AgentStateCompletedRequest = z.infer<typeof AgentStateCompletedRequestSchema>
|
||
|
||
export const AgentStateSchema = z.object({
|
||
controlledByUser: z.boolean().nullish(),
|
||
requests: z.record(z.string(), AgentStateRequestSchema).nullish(),
|
||
completedRequests: z.record(z.string(), AgentStateCompletedRequestSchema).nullish()
|
||
})
|
||
|
||
export type AgentState = z.infer<typeof AgentStateSchema>
|
||
|
||
export const TodoItemSchema = z.object({
|
||
content: z.string(),
|
||
status: z.enum(['pending', 'in_progress', 'completed']),
|
||
priority: z.enum(['high', 'medium', 'low']).optional().default('medium'),
|
||
id: z.string().optional().default(''),
|
||
activeForm: z.string().optional()
|
||
})
|
||
|
||
export type TodoItem = z.infer<typeof TodoItemSchema>
|
||
|
||
export const TodosSchema = z.array(TodoItemSchema)
|
||
|
||
export const TeamMemberSchema = z.object({
|
||
name: z.string(),
|
||
agentType: z.string().optional(),
|
||
status: z.enum(['active', 'idle', 'shutdown']).optional()
|
||
})
|
||
|
||
export type TeamMember = z.infer<typeof TeamMemberSchema>
|
||
|
||
export const TeamTaskSchema = z.object({
|
||
id: z.string(),
|
||
title: z.string(),
|
||
description: z.string().optional(),
|
||
status: z.enum(['pending', 'in_progress', 'completed', 'blocked']).optional(),
|
||
owner: z.string().optional()
|
||
})
|
||
|
||
export type TeamTask = z.infer<typeof TeamTaskSchema>
|
||
|
||
export const TeamMessageSchema = z.object({
|
||
from: z.string(),
|
||
to: z.string(),
|
||
summary: z.string(),
|
||
type: z.enum(['message', 'broadcast', 'shutdown_request', 'shutdown_response']),
|
||
timestamp: z.number()
|
||
})
|
||
|
||
export type TeamMessage = z.infer<typeof TeamMessageSchema>
|
||
|
||
export const TeamStateSchema = z.object({
|
||
teamName: z.string(),
|
||
description: z.string().optional(),
|
||
members: z.array(TeamMemberSchema).optional(),
|
||
tasks: z.array(TeamTaskSchema).optional(),
|
||
messages: z.array(TeamMessageSchema).optional(),
|
||
updatedAt: z.number().optional()
|
||
})
|
||
|
||
export type TeamState = z.infer<typeof TeamStateSchema>
|
||
|
||
export const ThreadGoalStatusSchema = z.enum(['active', 'paused', 'budgetLimited', 'complete'])
|
||
export type ThreadGoalStatus = z.infer<typeof ThreadGoalStatusSchema>
|
||
|
||
export const ThreadGoalSchema = z.object({
|
||
threadId: z.string(),
|
||
objective: z.string(),
|
||
status: ThreadGoalStatusSchema,
|
||
tokenBudget: z.number().nullable().optional(),
|
||
tokensUsed: z.number().optional().default(0),
|
||
timeUsedSeconds: z.number().optional().default(0),
|
||
createdAt: z.number().optional().default(0),
|
||
updatedAt: z.number().optional().default(0)
|
||
})
|
||
|
||
export type ThreadGoal = z.infer<typeof ThreadGoalSchema>
|
||
|
||
export const AttachmentMetadataSchema = z.object({
|
||
id: z.string(),
|
||
filename: z.string(),
|
||
mimeType: z.string(),
|
||
size: z.number(),
|
||
path: z.string(),
|
||
previewUrl: z.string().optional()
|
||
})
|
||
|
||
export type AttachmentMetadata = z.infer<typeof AttachmentMetadataSchema>
|
||
|
||
export const DecryptedMessageSchema = z.object({
|
||
id: z.string(),
|
||
seq: z.number().nullable(),
|
||
localId: z.string().nullable(),
|
||
content: z.unknown(),
|
||
createdAt: z.number(),
|
||
invokedAt: z.number().nullable().optional(),
|
||
scheduledAt: z.number().nullable().optional()
|
||
})
|
||
|
||
export type DecryptedMessage = z.infer<typeof DecryptedMessageSchema>
|
||
|
||
export const SessionSchema = z.object({
|
||
id: z.string(),
|
||
namespace: z.string(),
|
||
seq: z.number(),
|
||
createdAt: z.number(),
|
||
updatedAt: z.number(),
|
||
active: z.boolean(),
|
||
// Hub may still emit null for legacy SQLite rows; keep output type number.
|
||
activeAt: z.number().nullish().transform((value) => value ?? 0),
|
||
metadata: MetadataSchema.nullable(),
|
||
metadataVersion: z.number(),
|
||
agentState: AgentStateSchema.nullable(),
|
||
agentStateVersion: z.number(),
|
||
thinking: z.boolean(),
|
||
thinkingAt: z.number(),
|
||
backgroundTaskCount: z.number().optional(),
|
||
todos: TodosSchema.optional(),
|
||
teamState: TeamStateSchema.optional(),
|
||
model: z.string().nullable().optional().default(null),
|
||
modelReasoningEffort: z.string().nullable().optional().default(null),
|
||
effort: z.string().nullable().optional().default(null),
|
||
serviceTier: z.string().nullable().optional().default(null),
|
||
permissionMode: PermissionModeSchema.optional(),
|
||
collaborationMode: CodexCollaborationModeSchema.optional()
|
||
})
|
||
|
||
export type Session = z.infer<typeof SessionSchema>
|
||
|
||
export const SessionPatchSchema = z.object({
|
||
active: z.boolean().optional(),
|
||
thinking: z.boolean().optional(),
|
||
activeAt: z.number().optional(),
|
||
updatedAt: z.number().optional(),
|
||
model: z.string().nullable().optional(),
|
||
modelReasoningEffort: z.string().nullable().optional(),
|
||
effort: z.string().nullable().optional(),
|
||
serviceTier: z.string().nullable().optional(),
|
||
permissionMode: PermissionModeSchema.optional(),
|
||
collaborationMode: CodexCollaborationModeSchema.optional(),
|
||
backgroundTaskCount: z.number().optional(),
|
||
// tiann/hapi#893 (scratchlist v2). Bumped whenever any entry on the
|
||
// session_scratchlist table mutates. Web client uses the change as a
|
||
// trigger to refetch the entries query - the timestamp itself is the
|
||
// signal, not the payload. Keep this minimal: per the operator's 80/20
|
||
// ruling, scratchlist mutations are rare relative to keep-alive
|
||
// patches, so a fresh event type would be overkill.
|
||
scratchlistUpdatedAt: z.number().optional()
|
||
}).strict()
|
||
|
||
export type SessionPatch = z.infer<typeof SessionPatchSchema>
|
||
|
||
// tiann/hapi#893: per-session scratchlist entries (operator notes /
|
||
// drafts / parking-lot ideas). Hub-side typed-table source of truth;
|
||
// web treats localStorage as offline cache only. Single-user notes -
|
||
// no collaborative edit semantics (no version field, no conflict
|
||
// resolution beyond last-write-wins).
|
||
export const ScratchlistEntrySchema = z.object({
|
||
entryId: z.string().min(1),
|
||
text: z.string(),
|
||
createdAt: z.number(),
|
||
updatedAt: z.number(),
|
||
attachments: z.array(z.object({
|
||
id: z.string(),
|
||
filename: z.string(),
|
||
mimeType: z.string(),
|
||
size: z.number(),
|
||
path: z.string(),
|
||
})).optional().default([])
|
||
})
|
||
|
||
export type ScratchlistEntry = z.infer<typeof ScratchlistEntrySchema>
|
||
|
||
export const ScratchlistEntriesResponseSchema = z.object({
|
||
entries: z.array(ScratchlistEntrySchema)
|
||
})
|
||
|
||
export type ScratchlistEntriesResponse = z.infer<typeof ScratchlistEntriesResponseSchema>
|
||
|
||
export const MachineMetadataSchema = z.object({
|
||
host: z.string(),
|
||
platform: z.string(),
|
||
happyCliVersion: z.string(),
|
||
displayName: z.string().optional(),
|
||
homeDir: z.string().optional(),
|
||
happyHomeDir: z.string().optional(),
|
||
happyLibDir: z.string().optional(),
|
||
workspaceRoots: z.array(z.string()).optional()
|
||
})
|
||
|
||
export type MachineMetadata = z.infer<typeof MachineMetadataSchema>
|
||
|
||
export const RunnerStateSchema = z.object({
|
||
status: z.union([z.enum(['running', 'shutting-down']), z.string()]),
|
||
pid: z.number().optional(),
|
||
httpPort: z.number().optional(),
|
||
startedAt: z.number().optional(),
|
||
shutdownRequestedAt: z.number().optional(),
|
||
shutdownSource: z.union([z.enum(['mobile-app', 'cli', 'os-signal', 'unknown']), z.string()]).optional(),
|
||
lastSpawnError: z.object({
|
||
message: z.string(),
|
||
pid: z.number().optional(),
|
||
exitCode: z.number().nullable().optional(),
|
||
signal: z.string().nullable().optional(),
|
||
at: z.number()
|
||
}).nullable().optional()
|
||
})
|
||
|
||
export type RunnerState = z.infer<typeof RunnerStateSchema>
|
||
|
||
export const MachineHealthSchema = z.object({
|
||
collectedAt: z.number(),
|
||
cpuCount: z.number().int().positive().optional(),
|
||
load1m: z.number().nonnegative().optional(),
|
||
cpuPercent: z.number().min(0).max(100).optional(),
|
||
memoryPercent: z.number().min(0).max(100).optional(),
|
||
uptimeSeconds: z.number().nonnegative().optional()
|
||
}).strict()
|
||
|
||
export type MachineHealth = z.infer<typeof MachineHealthSchema>
|
||
|
||
export const MachineSchema = z.object({
|
||
id: z.string(),
|
||
namespace: z.string(),
|
||
seq: z.number(),
|
||
createdAt: z.number(),
|
||
updatedAt: z.number(),
|
||
active: z.boolean(),
|
||
activeAt: z.number(),
|
||
metadata: MachineMetadataSchema.nullable(),
|
||
metadataVersion: z.number(),
|
||
runnerState: RunnerStateSchema.nullable(),
|
||
runnerStateVersion: z.number(),
|
||
health: MachineHealthSchema.nullable().optional()
|
||
})
|
||
|
||
export type Machine = z.infer<typeof MachineSchema>
|
||
|
||
export const MachinePatchSchema = z.object({
|
||
active: z.boolean().optional(),
|
||
activeAt: z.number().optional(),
|
||
updatedAt: z.number().optional()
|
||
}).strict()
|
||
|
||
export type MachinePatch = z.infer<typeof MachinePatchSchema>
|
||
|
||
export const SessionUpdatedDataSchema = z.union([SessionSchema, SessionPatchSchema])
|
||
export type SessionUpdatedData = z.infer<typeof SessionUpdatedDataSchema>
|
||
|
||
export const MachineUpdatedDataSchema = z.union([MachineSchema, MachinePatchSchema, z.null()])
|
||
export type MachineUpdatedData = z.infer<typeof MachineUpdatedDataSchema>
|
||
|
||
const SessionEventBaseSchema = z.object({
|
||
namespace: z.string().optional()
|
||
})
|
||
|
||
const SessionChangedSchema = SessionEventBaseSchema.extend({
|
||
sessionId: z.string()
|
||
})
|
||
|
||
const MachineChangedSchema = SessionEventBaseSchema.extend({
|
||
machineId: z.string()
|
||
})
|
||
|
||
export const SyncEventSchema = z.discriminatedUnion('type', [
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('session-added'),
|
||
data: z.unknown().optional()
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('session-updated'),
|
||
data: SessionUpdatedDataSchema.optional()
|
||
}),
|
||
SessionEventBaseSchema.extend({
|
||
type: z.literal('session-removed'),
|
||
sessionId: z.string()
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('message-received'),
|
||
message: DecryptedMessageSchema
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('messages-invalidated')
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('scheduled-matured')
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('session-ended'),
|
||
reason: SessionEndReasonSchema.optional()
|
||
}),
|
||
MachineChangedSchema.extend({
|
||
type: z.literal('machine-updated'),
|
||
data: MachineUpdatedDataSchema.optional()
|
||
}),
|
||
SessionEventBaseSchema.extend({
|
||
type: z.literal('toast'),
|
||
data: z.object({
|
||
title: z.string(),
|
||
body: z.string(),
|
||
sessionId: z.string(),
|
||
url: z.string()
|
||
})
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('messages-consumed'),
|
||
localIds: z.array(z.string()),
|
||
invokedAt: z.number()
|
||
}),
|
||
SessionChangedSchema.extend({
|
||
type: z.literal('message-cancelled'),
|
||
messageId: z.string(),
|
||
localId: z.string().optional()
|
||
}),
|
||
SessionEventBaseSchema.extend({
|
||
type: z.literal('heartbeat'),
|
||
data: z.object({
|
||
timestamp: z.number()
|
||
}).optional()
|
||
}),
|
||
SessionEventBaseSchema.extend({
|
||
type: z.literal('connection-changed'),
|
||
data: z.object({
|
||
status: z.string(),
|
||
subscriptionId: z.string().optional()
|
||
}).optional()
|
||
})
|
||
])
|
||
|
||
export type SyncEvent = z.infer<typeof SyncEventSchema>
|
||
|
||
export const CancelMessageResponseSchema = z.discriminatedUnion('status', [
|
||
z.object({ status: z.literal('cancelled'), localId: z.string().nullable() }),
|
||
z.object({ status: z.literal('invoked'), message: DecryptedMessageSchema }),
|
||
])
|
||
|
||
export type CancelMessageResponse = z.infer<typeof CancelMessageResponseSchema>
|