mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 21:39:08 +00:00
* refactor(agent)!: migrate sessions to Pi Durable Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server. BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs. * fix(agent): constrain session log dialog scrolling Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow. * fix(agent): hide entry IDs from session log card headers Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them. * test(e2e): handle browser wheel limits in session log checks Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions. * fix(agent): select model providers without inheriting unrelated auth * fix(webui): prevent resize feedback when toggling agent panel * test(agent): reproduce concurrent comment session races * fix(agent): serialize comment sessions and deduplicate history * docs(changelog): consolidate Pi Durable changes into one entry
183 lines
6.6 KiB
TypeScript
183 lines
6.6 KiB
TypeScript
import { type ToolRegistration as AgentTool } from '@earendil-works/pi-durable'
|
|
import { mediaGenerationService } from '@shumai/core/src/media-generation/media-generation'
|
|
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
|
import { EnabledMediaModel } from '@shumai/dtos'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { Type, type Static, type TSchema } from 'typebox'
|
|
import { ulid } from 'ulid'
|
|
import { resolveS3MediaBuffer } from './download-asset'
|
|
|
|
export function createGenerateImageTool(
|
|
enabledModels: EnabledMediaModel[],
|
|
providerKeys: Record<string, { apiKey?: string }>,
|
|
userId?: string,
|
|
): AgentTool {
|
|
const modelIdentifiers = enabledModels.map((m) => `${m.provider}:${m.modelId}`)
|
|
const modelSchemas =
|
|
modelIdentifiers.length === 1
|
|
? Type.Literal(modelIdentifiers[0])
|
|
: Type.Union(
|
|
modelIdentifiers.map((id) => Type.Literal(id)) as unknown as [
|
|
TSchema,
|
|
TSchema,
|
|
...TSchema[],
|
|
],
|
|
)
|
|
|
|
const generateImageSchema = Type.Object(
|
|
{
|
|
prompt: Type.String({
|
|
description: 'The text prompt describing the image to generate in detail.',
|
|
}),
|
|
model: Type.Union([modelSchemas, Type.Null()], {
|
|
description: `The image model to use, in "provider:modelId" format. Available enabled models: ${modelIdentifiers.join(', ')}. Pass null to use the first enabled model (${modelIdentifiers[0]}).`,
|
|
}),
|
|
outputFileName: Type.Union([
|
|
Type.String({
|
|
description:
|
|
'Optional desired filename for the generated image (e.g. "cover.png"). Set to null for auto-generated name.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
images: Type.Union([
|
|
Type.Array(Type.String(), {
|
|
description:
|
|
'Optional list of storage S3 keys of images to use as image-to-image or style reference. Call read_asset with s3KeyOnly: true to obtain the S3 key of an asset or video frame without loading its content into context. Set to null if not using.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
mask: Type.Union([
|
|
Type.String({
|
|
description:
|
|
'Optional storage S3 key of an inpainting mask image (white=inpaint, black=keep). Call read_asset with s3KeyOnly: true to obtain the S3 key. Set to null if not inpainting.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
aspectRatio: Type.Union([
|
|
Type.String({
|
|
description:
|
|
'Optional aspect ratio, e.g. "1:1", "16:9", "9:16", "4:3", "3:4", "2:3", "3:2". Set to null for default.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
size: Type.Union([
|
|
Type.String({
|
|
description:
|
|
'Optional target image dimensions, e.g. "1024x1024", "1536x1024". Set to null for default.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
seed: Type.Union([
|
|
Type.Integer({
|
|
description:
|
|
'Optional random seed for reproducible generations. Set to null for random generation.',
|
|
}),
|
|
Type.Null(),
|
|
]),
|
|
},
|
|
{ additionalProperties: false },
|
|
)
|
|
|
|
type GenerateImageParams = Static<typeof generateImageSchema>
|
|
|
|
return {
|
|
name: 'generate_image',
|
|
|
|
description:
|
|
'Generate an image using a configured image model. Supports text-to-image, reference images (image-to-image using S3 keys from read_asset), and inpainting (using a mask image S3 key from read_asset). The generated file is saved in the local .pi/ directory.',
|
|
parameters: generateImageSchema,
|
|
execute: async (params) => {
|
|
const p = params as GenerateImageParams
|
|
|
|
const defaultModel = `${enabledModels[0].provider}:${enabledModels[0].modelId}`
|
|
const selected = p.model || defaultModel
|
|
const modelConfig =
|
|
enabledModels.find((m) => `${m.provider}:${m.modelId}` === selected) ||
|
|
enabledModels.find((m) => m.modelId === selected)
|
|
|
|
if (!modelConfig) {
|
|
throw new Error(
|
|
`Image model "${selected}" is not enabled. Available enabled models: ${modelIdentifiers.join(', ')}`,
|
|
)
|
|
}
|
|
|
|
const customKey = providerKeys[modelConfig.provider]?.apiKey
|
|
const apiKey = mediaGenerationService.resolveApiKey(modelConfig.provider, customKey)
|
|
if (!apiKey) {
|
|
throw new Error(
|
|
`API key for provider "${modelConfig.provider}" is not configured. Please configure it in settings.`,
|
|
)
|
|
}
|
|
|
|
let resolvedImages: Buffer[] | undefined
|
|
if (p.images && Array.isArray(p.images) && p.images.length > 0) {
|
|
resolvedImages = await Promise.all(
|
|
p.images.map(async (key: string) => {
|
|
const { buffer } = await resolveS3MediaBuffer(key, userId || '')
|
|
return buffer
|
|
}),
|
|
)
|
|
}
|
|
|
|
let resolvedMask: Buffer | undefined
|
|
if (p.mask) {
|
|
const { buffer } = await resolveS3MediaBuffer(p.mask, userId || '')
|
|
resolvedMask = buffer
|
|
}
|
|
|
|
const { buffer, mimeType } = await mediaGenerationService.generateImage({
|
|
provider: modelConfig.provider,
|
|
modelId: modelConfig.modelId,
|
|
apiKey,
|
|
prompt: p.prompt,
|
|
images: resolvedImages,
|
|
mask: resolvedMask,
|
|
aspectRatio: (p.aspectRatio as `${number}:${number}`) || undefined,
|
|
size: (p.size as `${number}x${number}`) || undefined,
|
|
seed: p.seed !== null && p.seed !== undefined ? p.seed : undefined,
|
|
})
|
|
|
|
const piDir = path.join(process.cwd(), '.pi')
|
|
if (!fs.existsSync(piDir)) {
|
|
fs.mkdirSync(piDir, { recursive: true })
|
|
}
|
|
|
|
const safeMime = mimeType || 'image/png'
|
|
let ext = '.png'
|
|
if (safeMime.includes('jpeg') || safeMime.includes('jpg')) ext = '.jpg'
|
|
else if (safeMime.includes('webp')) ext = '.webp'
|
|
|
|
let filename: string
|
|
if (p.outputFileName) {
|
|
const safe = sanitizeFilename(p.outputFileName)
|
|
filename = safe.includes('.') ? safe : `${safe}${ext}`
|
|
} else {
|
|
filename = `generated_image_${ulid()}${ext}`
|
|
}
|
|
|
|
const targetPath = path.join(piDir, filename)
|
|
const relativePath = path.join('.pi', filename)
|
|
fs.writeFileSync(targetPath, buffer)
|
|
|
|
return {
|
|
output: [
|
|
{
|
|
type: 'text',
|
|
text: `Successfully generated image using model "${modelConfig.modelId}" (${modelConfig.provider}). Saved to "${relativePath}". Size: ${(buffer.length / 1024).toFixed(1)} KB.`,
|
|
},
|
|
],
|
|
details: {
|
|
fileName: filename,
|
|
filePath: relativePath,
|
|
absolutePath: targetPath,
|
|
size: buffer.length,
|
|
mimeType,
|
|
provider: modelConfig.provider,
|
|
model: modelConfig.modelId,
|
|
},
|
|
}
|
|
},
|
|
}
|
|
}
|