mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
refactor: extract session summary utilities to shared module
This commit is contained in:
@@ -1,69 +1,11 @@
|
||||
import { getPermissionModesForFlavor, isModelModeAllowedForFlavor, isPermissionModeAllowedForFlavor } from '@hapi/protocol'
|
||||
import { getPermissionModesForFlavor, isModelModeAllowedForFlavor, isPermissionModeAllowedForFlavor, toSessionSummary } from '@hapi/protocol'
|
||||
import { ModelModeSchema, PermissionModeSchema } from '@hapi/protocol/schemas'
|
||||
import { Hono } from 'hono'
|
||||
import { z } from 'zod'
|
||||
import type { ModelMode } from '@hapi/protocol/types'
|
||||
import type { SyncEngine, Session } from '../../sync/syncEngine'
|
||||
import type { WebAppEnv } from '../middleware/auth'
|
||||
import { requireSessionFromParam, requireSyncEngine } from './guards'
|
||||
|
||||
type SessionSummaryMetadata = {
|
||||
name?: string
|
||||
path: string
|
||||
machineId?: string
|
||||
summary?: { text: string }
|
||||
flavor?: string | null
|
||||
worktree?: {
|
||||
basePath: string
|
||||
branch: string
|
||||
name: string
|
||||
worktreePath?: string
|
||||
createdAt?: number
|
||||
}
|
||||
}
|
||||
|
||||
type SessionSummary = {
|
||||
id: string
|
||||
active: boolean
|
||||
thinking: boolean
|
||||
activeAt: number
|
||||
updatedAt: number
|
||||
metadata: SessionSummaryMetadata | null
|
||||
todoProgress: { completed: number; total: number } | null
|
||||
pendingRequestsCount: number
|
||||
modelMode?: ModelMode
|
||||
}
|
||||
|
||||
function toSessionSummary(session: Session): SessionSummary {
|
||||
const pendingRequestsCount = session.agentState?.requests ? Object.keys(session.agentState.requests).length : 0
|
||||
|
||||
const metadata: SessionSummaryMetadata | null = session.metadata ? {
|
||||
name: session.metadata.name,
|
||||
path: session.metadata.path,
|
||||
machineId: session.metadata.machineId ?? undefined,
|
||||
summary: session.metadata.summary ? { text: session.metadata.summary.text } : undefined,
|
||||
flavor: session.metadata.flavor ?? null,
|
||||
worktree: session.metadata.worktree
|
||||
} : null
|
||||
|
||||
const todoProgress = session.todos?.length ? {
|
||||
completed: session.todos.filter(t => t.status === 'completed').length,
|
||||
total: session.todos.length
|
||||
} : null
|
||||
|
||||
return {
|
||||
id: session.id,
|
||||
active: session.active,
|
||||
thinking: session.thinking,
|
||||
activeAt: session.activeAt,
|
||||
updatedAt: session.updatedAt,
|
||||
metadata,
|
||||
todoProgress,
|
||||
pendingRequestsCount,
|
||||
modelMode: session.modelMode
|
||||
}
|
||||
}
|
||||
|
||||
const permissionModeSchema = z.object({
|
||||
mode: PermissionModeSchema
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './messages'
|
||||
export * from './modes'
|
||||
export * from './sessionSummary'
|
||||
export type * from './types'
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { ModelMode } from './modes'
|
||||
import type { Session, WorktreeMetadata } from './schemas'
|
||||
|
||||
export type SessionSummaryMetadata = {
|
||||
name?: string
|
||||
path: string
|
||||
machineId?: string
|
||||
summary?: { text: string }
|
||||
flavor?: string | null
|
||||
worktree?: WorktreeMetadata
|
||||
}
|
||||
|
||||
export type SessionSummary = {
|
||||
id: string
|
||||
active: boolean
|
||||
thinking: boolean
|
||||
activeAt: number
|
||||
updatedAt: number
|
||||
metadata: SessionSummaryMetadata | null
|
||||
todoProgress: { completed: number; total: number } | null
|
||||
pendingRequestsCount: number
|
||||
modelMode?: ModelMode
|
||||
}
|
||||
|
||||
export function toSessionSummary(session: Session): SessionSummary {
|
||||
const pendingRequestsCount = session.agentState?.requests ? Object.keys(session.agentState.requests).length : 0
|
||||
|
||||
const metadata: SessionSummaryMetadata | null = session.metadata ? {
|
||||
name: session.metadata.name,
|
||||
path: session.metadata.path,
|
||||
machineId: session.metadata.machineId ?? undefined,
|
||||
summary: session.metadata.summary ? { text: session.metadata.summary.text } : undefined,
|
||||
flavor: session.metadata.flavor ?? null,
|
||||
worktree: session.metadata.worktree
|
||||
} : null
|
||||
|
||||
const todoProgress = session.todos?.length ? {
|
||||
completed: session.todos.filter(t => t.status === 'completed').length,
|
||||
total: session.todos.length
|
||||
} : null
|
||||
|
||||
return {
|
||||
id: session.id,
|
||||
active: session.active,
|
||||
thinking: session.thinking,
|
||||
activeAt: session.activeAt,
|
||||
updatedAt: session.updatedAt,
|
||||
metadata,
|
||||
todoProgress,
|
||||
pendingRequestsCount,
|
||||
modelMode: session.modelMode
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ export type {
|
||||
WorktreeMetadata
|
||||
} from './schemas'
|
||||
|
||||
export type { SessionSummary, SessionSummaryMetadata } from './sessionSummary'
|
||||
|
||||
export type {
|
||||
AgentFlavor,
|
||||
ClaudePermissionMode,
|
||||
|
||||
+11
-23
@@ -1,12 +1,21 @@
|
||||
import type {
|
||||
DecryptedMessage as ProtocolDecryptedMessage,
|
||||
ModelMode,
|
||||
Session,
|
||||
SessionSummary,
|
||||
SyncEvent as ProtocolSyncEvent,
|
||||
WorktreeMetadata
|
||||
} from '@hapi/protocol/types'
|
||||
|
||||
export type { AgentState, ModelMode, PermissionMode, Session, TodoItem, WorktreeMetadata } from '@hapi/protocol/types'
|
||||
export type {
|
||||
AgentState,
|
||||
ModelMode,
|
||||
PermissionMode,
|
||||
Session,
|
||||
SessionSummary,
|
||||
SessionSummaryMetadata,
|
||||
TodoItem,
|
||||
WorktreeMetadata
|
||||
} from '@hapi/protocol/types'
|
||||
|
||||
export type SessionMetadataSummary = {
|
||||
path: string
|
||||
@@ -21,27 +30,6 @@ export type SessionMetadataSummary = {
|
||||
worktree?: WorktreeMetadata
|
||||
}
|
||||
|
||||
export type SessionSummaryMetadata = {
|
||||
name?: string
|
||||
path: string
|
||||
machineId?: string
|
||||
summary?: { text: string }
|
||||
flavor?: string | null
|
||||
worktree?: WorktreeMetadata
|
||||
}
|
||||
|
||||
export type SessionSummary = {
|
||||
id: string
|
||||
active: boolean
|
||||
thinking: boolean
|
||||
activeAt: number
|
||||
updatedAt: number
|
||||
metadata: SessionSummaryMetadata | null
|
||||
todoProgress: { completed: number; total: number } | null
|
||||
pendingRequestsCount: number
|
||||
modelMode?: ModelMode
|
||||
}
|
||||
|
||||
export type MessageStatus = 'sending' | 'sent' | 'failed'
|
||||
|
||||
export type DecryptedMessage = ProtocolDecryptedMessage & {
|
||||
|
||||
Reference in New Issue
Block a user