@dickpy/dsh-imagegen 1.4.0 → 1.5.1

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.
Files changed (54) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +270 -124
  3. package/cordis.patch.yml +8 -8
  4. package/docs/images/ecommerce-mode.png +0 -0
  5. package/docs/images/image-generation-studio-three-column.png +0 -0
  6. package/docs/images/imagegen-overview.png +0 -0
  7. package/docs/videos/agent-chat-edit.gif +0 -0
  8. package/docs/videos/agent-chat-edit.mp4 +0 -0
  9. package/lib/client.js +1873 -431
  10. package/lib/client.js.map +1 -1
  11. package/lib/index.js +355 -116
  12. package/package.json +77 -70
  13. package/src/agent-image-tools.ts +447 -418
  14. package/src/client/ImageGenPanel.tsx +1243 -348
  15. package/src/client/SettingsCard.tsx +936 -936
  16. package/src/client/TemplateLibrary.tsx +336 -336
  17. package/src/client/api.ts +203 -193
  18. package/src/client/channels-form.ts +263 -263
  19. package/src/client/controller.ts +46 -46
  20. package/src/client/conversation-sync.ts +14 -14
  21. package/src/client/css-modules.d.ts +5 -5
  22. package/src/client/helpers.ts +33 -33
  23. package/src/client/image-toolview.module.css +73 -73
  24. package/src/client/image-toolview.tsx +34 -28
  25. package/src/client/index.ts +25 -24
  26. package/src/client/locales.ts +156 -28
  27. package/src/client/mount.tsx +117 -117
  28. package/src/client/panel.module.css +1243 -455
  29. package/src/client/settings-card.module.css +1023 -1023
  30. package/src/client/settings-form.ts +337 -336
  31. package/src/client/settings-scope.ts +302 -298
  32. package/src/client/sidebar-entry.ts +190 -190
  33. package/src/client/templates.module.css +453 -453
  34. package/src/edit-image-command.ts +110 -0
  35. package/src/engine.ts +520 -520
  36. package/src/gallery-store.ts +306 -286
  37. package/src/generation-runtime.ts +84 -79
  38. package/src/history-store.ts +270 -250
  39. package/src/image-format.ts +11 -11
  40. package/src/image-models.ts +19 -19
  41. package/src/index.ts +337 -318
  42. package/src/model-catalog.ts +115 -115
  43. package/src/presets.ts +71 -71
  44. package/src/prompt-enhancer.ts +137 -137
  45. package/src/protocol.ts +380 -338
  46. package/src/routes.ts +966 -916
  47. package/src/settings-compat.ts +60 -0
  48. package/src/task-queue.ts +113 -113
  49. package/src/templates/cases.json +10196 -10196
  50. package/src/templates-store.ts +278 -278
  51. package/src/updater.ts +117 -117
  52. package/docs/images/agent-chat-edit.png +0 -0
  53. package/docs/images/agent-chat-generate.png +0 -0
  54. package/docs/images/agent-chat-poster-workflow.png +0 -0
package/src/index.ts CHANGED
@@ -1,318 +1,337 @@
1
- /**
2
- * dsh-imagegen — host half. Mounts the plugin's settings section (channels
3
- * with per-channel model catalogs on the host settings seam), the
4
- * /api/dsh-imagegen route family (loopback-only settings bridge + presets /
5
- * usage / image-generation proxy that keeps every API key host-side), and a
6
- * system-prompt announcement. The browser half (./client) renders the sidebar
7
- * entry and the split-pane generation studio.
8
- */
9
-
10
- import type { Context } from '@deepseek-ai/cordis'
11
- import { installSettingsSection, settingsNamespace } from '@deepseek-ai/dsh-settings'
12
- import z from 'schemastery'
13
- // Type-only: pulls the webServer Context merge (route registration).
14
- import type {} from '@deepseek-ai/dsh-host-webserver'
15
- // Type-only: pulls the systemPrompt Context merge (announcement section).
16
- import type {} from '@deepseek-ai/dsh-system-prompt'
17
- import type {} from '@deepseek-ai/dsh-tools'
18
- import type {} from '@deepseek-ai/dsh-attachment'
19
- import { IMAGEGEN_SETTINGS_NAMESPACE, type ChannelConfig, type ModelMapping } from './protocol.ts'
20
- import { makeRoutes, type SettingsSeam } from './routes.ts'
21
- import { ImageGenerationRuntime, type ChannelsView, type RuntimeChannel } from './generation-runtime.ts'
22
- import { registerAgentImageTools } from './agent-image-tools.ts'
23
- import { presetById } from './presets.ts'
24
-
25
- /** Stable cordis plugin name. */
26
- export const name = 'imagegen'
27
-
28
- /** Services required before the surfaces can mount. */
29
- export const inject = ['webServer', 'systemPrompt']
30
-
31
- // Internals re-exported for smoke tests and host-side debugging; the plugin
32
- // contract only requires name / inject / Config / apply.
33
- export { makeRoutes } from './routes.ts'
34
- export { generateImage, ImageGenError } from './engine.ts'
35
- export { ImageGenerationRuntime } from './generation-runtime.ts'
36
- export { registerAgentImageTools } from './agent-image-tools.ts'
37
- export { appendGallery, clearGallery, listGallery, readGalleryImage, removeGallery, updateGalleryTags } from './gallery-store.ts'
38
- export { listTemplates, readTemplateImage, refreshTemplates, clearTemplateMemo } from './templates-store.ts'
39
- export { checkForUpdate, clearUpdateCache, compareVersions, CURRENT_VERSION, installUpdate, profileFromProcess } from './updater.ts'
40
-
41
- /** The branded settings namespace of this plugin (the card edits it). */
42
- export const ImageGenSettingsNamespace = settingsNamespace(IMAGEGEN_SETTINGS_NAMESPACE)
43
-
44
- /**
45
- * Plugin config, validated by the same-named schemastery schema.
46
- *
47
- * Channels own the endpoint + model catalog. The API key of each channel lives
48
- * in `channelSecrets` (a secret dict keyed by channel id) instead of inside the
49
- * channel objects dsh-settings redaction supports dict/array containers, but
50
- * path ops cannot reach inside arrays, so a whole-array write must never carry
51
- * secrets it would clobber.
52
- */
53
- export interface Config {
54
- /** Master switch for the plugin (routes, prompt section). */
55
- enabled?: boolean
56
- /** Announce the plugin in every agent's system prompt. */
57
- announceToAgent?: boolean
58
- /** Allow Agents to submit and retrieve image-generation tasks. */
59
- allowAgentImageGeneration?: boolean
60
- /** Configured channels (each: name, endpoint, model catalog). */
61
- channels?: ChannelConfig[]
62
- /** Per-channel API keys, keyed by channel id. */
63
- channelSecrets?: Record<string, string>
64
- /** Channel used when a request does not name one. */
65
- defaultChannelId?: string
66
- /** Optional OpenAI-compatible chat endpoint for prompt enhancement. */
67
- promptApiUrl?: string
68
- /** Optional secret for the prompt enhancement endpoint. */
69
- promptApiKey?: string
70
- /** Chat model used to expand short image prompts. */
71
- promptModel?: string
72
- /* ----- deprecated legacy single-endpoint fields (migrated to channels) ----- */
73
- /** Legacy base URL; synthesized into the default channel on upgrade. */
74
- apiUrl?: string
75
- /** Legacy secret; migrated into channelSecrets on upgrade. */
76
- apiKey?: string
77
- /** Legacy allow-list; migrated into the default channel's catalog. */
78
- imageModels?: string[]
79
- }
80
-
81
- export const Config: z<Config> = z.object({
82
- enabled: z.boolean().default(true),
83
- announceToAgent: z.boolean().default(true),
84
- allowAgentImageGeneration: z.boolean().default(true),
85
- channels: z.array(z.object({
86
- id: z.string(),
87
- preset: z.string().default(''),
88
- name: z.string().default(''),
89
- apiUrl: z.string().default(''),
90
- models: z.array(z.object({
91
- alias: z.string(),
92
- id: z.string(),
93
- })).default([]),
94
- })).default([]),
95
- channelSecrets: z.dict(z.string().role('secret')).default({}),
96
- defaultChannelId: z.string().default(''),
97
- promptApiUrl: z.string().default(''),
98
- promptApiKey: z.string().role('secret').default(''),
99
- promptModel: z.string().default(''),
100
- apiUrl: z.string().default(''),
101
- apiKey: z.string().role('secret').default(''),
102
- imageModels: z.array(z.string()).default([]),
103
- })
104
-
105
- /** Schema defaults, re-read for hand-built contexts (the loader applies them normally). */
106
- const DEFAULT_ENABLED = true
107
- const DEFAULT_ANNOUNCE = true
108
- const DEFAULT_ALLOW_AGENT_IMAGE_GENERATION = true
109
-
110
- /** Order of the announcement section within the tool-guidance band. */
111
- const SECTION_ORDER = 150
112
-
113
- /** Model-facing announcement: plugin presence, capabilities, and limits. */
114
- export const IMAGEGEN_GUIDANCE = '本机已安装 dsh-imagegen 插件(DSH AI 生图):侧边栏「AI 生图」入口。能力:通过「渠道」对接 OpenAI 兼容图像生成 API(每个渠道 = 一个 API 端点 + 各自的模型目录),支持文生图(/images/generations)与图生图(/images/edits,上传参考图,grok-imagine 模型按官方 JSON image_url 协议发送,nanobanana 系列按 aspect_ratio / image_size 参数协议发送;seedream 系列统一走 /images/generations,参考图以 JSON image 数组发送;智谱 `glm-image` 使用官方 `/api/paas/v4/images/generations`,当前仅支持文生图)。API 地址与密钥在 GUI 设置中按渠道配置,密钥仅存于本机设置文档;生成请求由本地宿主代理转发,结果以 base64 返回面板,可预览与下载。模型只能使用用户在各渠道配置目录中的模型;检测模型时会过滤聊天、Embedding 等非图片模型,但模型出现在 /models 中仍不等于其网关原生支持生图协议,遇到 Qwen、Gemini 等非 OpenAI 生图协议时应如实说明上游兼容性。可一键把满意的图片加入「画廊」。内置「提示词模板库」(面板提示词框左下角「模板库」按钮):打包 awesome-gpt-image-2 的数百条提示词案例,可搜索、筛选与复用。Agent 可直接调用 `generate_image` 提交文生图,也可用 `edit_image` 图生图;默认保持工具调用等待直到任务完成,完成图片显示在工具调用对应的左侧结果区域,模型收到状态和附件引用,不会额外伪造用户消息。若明确需要后台执行,可传 `wait_for_completion: false`,之后再用 `get_image_generation_task` 查询;不要反复轮询。限制:生成消耗上游 API 额度;图片内容由上游模型生成,可能不符合预期或包含不适宜内容;api_key 以明文存储在设置文档中;参考图会发送至所配置的 API 服务;模板库在线刷新与参考图首次加载需要访问 vibeui.top。用户提到「生图 / 绘画 / 生成图片 / 文生图 / 图生图 / 画廊 / 提示词模板」时即指本插件,请据此协作。'
115
-
116
- /** Append the live channel × model table so an Agent can honor user choices. */
117
- function guidanceFor(channels: RuntimeChannel[], defaultChannelId: string): string {
118
- if (channels.length === 0) {
119
- return `${IMAGEGEN_GUIDANCE} 尚未配置任何渠道:请先在「设置 → 插件 → AI 生图」添加渠道并填写 API 地址与密钥。`
120
- }
121
- const table = channels.map(channel => {
122
- const aliases = channel.models.map(model => model.alias).join('、')
123
- const mark = channel.id === defaultChannelId ? '(默认渠道)' : ''
124
- const key = channel.apiKey === '' ? '(未填密钥)' : ''
125
- const models = channel.models.length === 0 ? '未配置模型' : `可用模型:${aliases}`
126
- return `渠道「${channel.name}」${mark}[${channel.apiUrl}] ${models}${key}`
127
- }).join('')
128
- return `${IMAGEGEN_GUIDANCE} 当前渠道与模型:${table}。用户指定模型名时取该模型所属渠道(多渠道同名用默认渠道);未指定模型时若仅一个可用模型可直接生成,若有多个应先询问用户选择「渠道 + 模型」。`
129
- }
130
-
131
- /** Normalize raw channel entries into the wire shape (schema-adjacent guard). */
132
- function normalizeChannels(value: unknown): ChannelConfig[] {
133
- if (!Array.isArray(value)) return []
134
- const out: ChannelConfig[] = []
135
- for (const item of value) {
136
- if (item === null || typeof item !== 'object') continue
137
- const raw = item as Record<string, unknown>
138
- const id = typeof raw.id === 'string' ? raw.id.trim() : ''
139
- if (id === '') continue
140
- const models: ModelMapping[] = []
141
- if (Array.isArray(raw.models)) {
142
- for (const entry of raw.models) {
143
- if (entry === null || typeof entry !== 'object') continue
144
- const record = entry as Record<string, unknown>
145
- const alias = typeof record.alias === 'string' ? record.alias.trim() : ''
146
- const upstream = typeof record.id === 'string' ? record.id.trim() : ''
147
- if (alias === '') continue
148
- models.push({ alias, id: upstream === '' ? alias : upstream })
149
- }
150
- }
151
- out.push({
152
- id,
153
- preset: typeof raw.preset === 'string' ? raw.preset : '',
154
- name: typeof raw.name === 'string' ? raw.name.trim() : '',
155
- apiUrl: typeof raw.apiUrl === 'string' ? raw.apiUrl.trim() : '',
156
- models,
157
- })
158
- }
159
- return out
160
- }
161
-
162
- /** Effective config (schema defaults applied + legacy migration). */
163
- export interface EffectiveConfig {
164
- enabled: boolean
165
- announceToAgent: boolean
166
- allowAgentImageGeneration: boolean
167
- channels: RuntimeChannel[]
168
- defaultChannelId: string
169
- promptApiUrl: string
170
- promptApiKey: string
171
- promptModel: string
172
- }
173
-
174
- /**
175
- * Mount the settings section, routes, and announcement.
176
- * @param ctx - host plugin context carrying webServer/systemPrompt.
177
- * @param config - resolved plugin config (schema defaults applied by the loader).
178
- */
179
- export function apply(ctx: Context, config?: Config): void {
180
- // The live source the surfaces read: the settings section once the settings
181
- // service is attached, the composition entry otherwise.
182
- let current: () => Config = () => config ?? {}
183
- const resolve = (): EffectiveConfig => {
184
- const value = current() ?? {}
185
- let channels = normalizeChannels(value.channels)
186
- // Settings scopes are deep-frozen by the host. Legacy migration adds the
187
- // synthesized default-channel secret, so always work on a detached copy.
188
- const secrets: Record<string, string> = { ...(value.channelSecrets ?? {}) }
189
- // Legacy single-endpoint migration: no channels yet → synthesize the
190
- // default channel from the old flat fields so upgrades never break.
191
- if (channels.length === 0) {
192
- const legacyUrl = typeof value.apiUrl === 'string' ? value.apiUrl.trim() : ''
193
- const legacyModels: ModelMapping[] = Array.isArray(value.imageModels)
194
- ? value.imageModels
195
- .filter((model): model is string => typeof model === 'string' && model.trim() !== '')
196
- .map(model => ({ alias: model.trim(), id: model.trim() }))
197
- : []
198
- if (legacyUrl !== '' || legacyModels.length > 0) {
199
- channels = [{ id: 'default', preset: '', name: '默认渠道', apiUrl: legacyUrl, models: legacyModels }]
200
- const legacyKey = typeof value.apiKey === 'string' ? value.apiKey.trim() : ''
201
- if (legacyKey !== '') secrets['default'] = legacyKey
202
- }
203
- }
204
- const named = channels.map(channel => ({
205
- ...channel,
206
- name: channel.name === '' ? (presetById(channel.preset)?.name ?? '未命名渠道') : channel.name,
207
- }))
208
- const defaultChannelId = typeof value.defaultChannelId === 'string' && named.some(channel => channel.id === value.defaultChannelId)
209
- ? value.defaultChannelId
210
- : named[0]?.id ?? ''
211
- return {
212
- enabled: value.enabled ?? DEFAULT_ENABLED,
213
- announceToAgent: value.announceToAgent ?? DEFAULT_ANNOUNCE,
214
- allowAgentImageGeneration: value.allowAgentImageGeneration ?? DEFAULT_ALLOW_AGENT_IMAGE_GENERATION,
215
- channels: named.map(channel => ({
216
- ...channel,
217
- apiKey: typeof secrets[channel.id] === 'string' ? secrets[channel.id] : '',
218
- })),
219
- defaultChannelId,
220
- promptApiUrl: typeof value.promptApiUrl === 'string' ? value.promptApiUrl.trim() : '',
221
- promptApiKey: typeof value.promptApiKey === 'string' ? value.promptApiKey.trim() : '',
222
- promptModel: typeof value.promptModel === 'string' ? value.promptModel.trim() : '',
223
- }
224
- }
225
-
226
- // Transient helper used by several mount points below: resolve the shared
227
- // channel view once per access; the runtime then picks per-request creds.
228
- const channelsView = (): ChannelsView => {
229
- const value = resolve()
230
- return { channels: value.channels, defaultChannelId: value.defaultChannelId }
231
- }
232
-
233
- // Browser endpoints and Agent tools share the exact same serial queue. This
234
- // keeps image persistence, cancellation, and retries coherent across both
235
- // entry points; Agent tools wait for their task result by default and render
236
- // images in the tool result instead of injecting a synthetic user message.
237
- const runtime = new ImageGenerationRuntime(channelsView)
238
-
239
- // The route family mounts once, gated on the settings seam (the bridge
240
- // serves it; without the seam there is nothing to expose). Route handlers
241
- // read resolve() per request, so config edits apply live. The settings
242
- // bridge deliberately keeps serving while the plugin is disabled — it is
243
- // how the user re-enables the plugin from the settings card.
244
- ctx.inject(['settings', 'attachments'], (sctx) => {
245
- const seam = sctx.get('settings') as unknown as SettingsSeam
246
- sctx.effect(
247
- () => {
248
- const routes = makeRoutes({
249
- settings: seam,
250
- resolve: () => {
251
- const value = resolve()
252
- const channel = value.channels.find(candidate => candidate.id === value.defaultChannelId) ?? value.channels[0]
253
- return { apiUrl: channel?.apiUrl ?? '', apiKey: channel?.apiKey ?? '' }
254
- },
255
- resolveChannels: channelsView,
256
- resolvePrompt: () => {
257
- const value = resolve()
258
- const channel = value.channels.find(candidate => candidate.id === value.defaultChannelId) ?? value.channels[0]
259
- return {
260
- apiUrl: value.promptApiUrl !== '' ? value.promptApiUrl : (channel?.apiUrl ?? ''),
261
- apiKey: value.promptApiKey !== '' ? value.promptApiKey : (channel?.apiKey ?? ''),
262
- model: value.promptModel,
263
- }
264
- },
265
- resolveImageModels: () => {
266
- const value = resolve()
267
- return [...new Set(value.channels.flatMap(channel => channel.models.map(model => model.alias)))]
268
- },
269
- attachments: sctx.attachments,
270
- runtime,
271
- })
272
- const disposers = routes.map(route => ctx.webServer.register(route))
273
- return () => { for (const dispose of disposers) dispose() }
274
- },
275
- 'dsh-imagegen: routes',
276
- )
277
- })
278
-
279
- ctx.inject(['tools', 'attachments'], (tctx) => {
280
- tctx.effect(() => registerAgentImageTools(tctx, runtime, () => {
281
- const value = resolve()
282
- return {
283
- enabled: value.enabled,
284
- allowAgentImageGeneration: value.allowAgentImageGeneration,
285
- channels: value.channels,
286
- defaultChannelId: value.defaultChannelId,
287
- }
288
- }), 'dsh-imagegen: agent image tools')
289
- })
290
-
291
- // System-prompt announcement (toggled by settings changes).
292
- let disposeSection: (() => void) | undefined
293
- const sync = (): void => {
294
- if (disposeSection !== undefined) {
295
- disposeSection()
296
- disposeSection = undefined
297
- }
298
- const value = resolve()
299
- if (!value.enabled || !value.announceToAgent) return
300
- disposeSection = ctx.systemPrompt.section({
301
- name: 'plugin:dsh-imagegen',
302
- order: SECTION_ORDER,
303
- text: guidanceFor(value.channels, value.defaultChannelId),
304
- })
305
- }
306
-
307
- installSettingsSection(ctx, ImageGenSettingsNamespace, Config, config ?? {}, {
308
- setSource: (source) => {
309
- current = source
310
- sync()
311
- },
312
- onChange: sync,
313
- })
314
-
315
- // Initial registration from the composition entry (covers deployments with
316
- // no settings service, whose installSettingsSection never fires its hooks).
317
- sync()
318
- }
1
+ /**
2
+ * dsh-imagegen — host half. Mounts the plugin's settings section (channels
3
+ * with per-channel model catalogs on the host settings seam), the
4
+ * /api/dsh-imagegen route family (loopback-only settings bridge + presets /
5
+ * usage / image-generation proxy that keeps every API key host-side), and a
6
+ * system-prompt announcement. The browser half (./client) renders the sidebar
7
+ * entry and the split-pane generation studio.
8
+ */
9
+
10
+ import type { Context } from '@deepseek-ai/cordis'
11
+ import { installSettingsSectionCompat, settingsNamespaceCompat } from './settings-compat.ts'
12
+ import z from 'schemastery'
13
+ // Type-only: pulls the webServer Context merge (route registration).
14
+ import type {} from '@deepseek-ai/dsh-host-webserver'
15
+ // Type-only: pulls the systemPrompt Context merge (announcement section).
16
+ import type {} from '@deepseek-ai/dsh-system-prompt'
17
+ // Type-only: pulls the human slash-command registry Context merge.
18
+ import type {} from '@deepseek-ai/dsh-commands'
19
+ import type {} from '@deepseek-ai/dsh-tools'
20
+ import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
21
+ import { IMAGEGEN_SETTINGS_NAMESPACE, type ChannelConfig, type ModelMapping } from './protocol.ts'
22
+ import { makeRoutes, type SettingsSeam } from './routes.ts'
23
+ import { ImageGenerationRuntime, type ChannelsView, type RuntimeChannel } from './generation-runtime.ts'
24
+ import { registerAgentImageTools } from './agent-image-tools.ts'
25
+ import { registerEditImageCommand } from './edit-image-command.ts'
26
+ import { presetById } from './presets.ts'
27
+
28
+ /** Stable cordis plugin name. */
29
+ export const name = 'imagegen'
30
+
31
+ /** Services required before the surfaces can mount. */
32
+ export const inject = ['webServer', 'systemPrompt', 'commands']
33
+
34
+ // Internals re-exported for smoke tests and host-side debugging; the plugin
35
+ // contract only requires name / inject / Config / apply.
36
+ export { makeRoutes } from './routes.ts'
37
+ export { generateImage, ImageGenError } from './engine.ts'
38
+ export { ImageGenerationRuntime } from './generation-runtime.ts'
39
+ export { registerAgentImageTools } from './agent-image-tools.ts'
40
+ export { latestSessionImage, registerEditImageCommand } from './edit-image-command.ts'
41
+ export { appendGallery, clearGallery, listGallery, readGalleryImage, removeGallery, updateGalleryTags } from './gallery-store.ts'
42
+ export { listTemplates, readTemplateImage, refreshTemplates, clearTemplateMemo } from './templates-store.ts'
43
+ export { checkForUpdate, clearUpdateCache, compareVersions, CURRENT_VERSION, installUpdate, profileFromProcess } from './updater.ts'
44
+
45
+ /** The branded settings namespace of this plugin (the card edits it). */
46
+ export const ImageGenSettingsNamespace = settingsNamespaceCompat(IMAGEGEN_SETTINGS_NAMESPACE)
47
+
48
+ /**
49
+ * Plugin config, validated by the same-named schemastery schema.
50
+ *
51
+ * Channels own the endpoint + model catalog. The API key of each channel lives
52
+ * in `channelSecrets` (a secret dict keyed by channel id) instead of inside the
53
+ * channel objects — dsh-settings redaction supports dict/array containers, but
54
+ * path ops cannot reach inside arrays, so a whole-array write must never carry
55
+ * secrets it would clobber.
56
+ */
57
+ export interface Config {
58
+ /** Master switch for the plugin (routes, prompt section). */
59
+ enabled?: boolean
60
+ /** Announce the plugin in every agent's system prompt. */
61
+ announceToAgent?: boolean
62
+ /** Allow Agents to submit and retrieve image-generation tasks. */
63
+ allowAgentImageGeneration?: boolean
64
+ /** Configured channels (each: name, endpoint, model catalog). */
65
+ channels?: ChannelConfig[]
66
+ /** Per-channel API keys, keyed by channel id. */
67
+ channelSecrets?: Record<string, string>
68
+ /** Channel used when a request does not name one. */
69
+ defaultChannelId?: string
70
+ /** Optional OpenAI-compatible chat endpoint for prompt enhancement. */
71
+ promptApiUrl?: string
72
+ /** Optional secret for the prompt enhancement endpoint. */
73
+ promptApiKey?: string
74
+ /** Chat model used to expand short image prompts. */
75
+ promptModel?: string
76
+ /* ----- deprecated legacy single-endpoint fields (migrated to channels) ----- */
77
+ /** Legacy base URL; synthesized into the default channel on upgrade. */
78
+ apiUrl?: string
79
+ /** Legacy secret; migrated into channelSecrets on upgrade. */
80
+ apiKey?: string
81
+ /** Legacy allow-list; migrated into the default channel's catalog. */
82
+ imageModels?: string[]
83
+ }
84
+
85
+ export const Config: z<Config> = z.object({
86
+ enabled: z.boolean().default(true),
87
+ announceToAgent: z.boolean().default(true),
88
+ allowAgentImageGeneration: z.boolean().default(true),
89
+ channels: z.array(z.object({
90
+ id: z.string(),
91
+ preset: z.string().default(''),
92
+ name: z.string().default(''),
93
+ apiUrl: z.string().default(''),
94
+ models: z.array(z.object({
95
+ alias: z.string(),
96
+ id: z.string(),
97
+ })).default([]),
98
+ })).default([]),
99
+ channelSecrets: z.dict(z.string().role('secret')).default({}),
100
+ defaultChannelId: z.string().default(''),
101
+ promptApiUrl: z.string().default(''),
102
+ promptApiKey: z.string().role('secret').default(''),
103
+ promptModel: z.string().default(''),
104
+ apiUrl: z.string().default(''),
105
+ apiKey: z.string().role('secret').default(''),
106
+ imageModels: z.array(z.string()).default([]),
107
+ })
108
+
109
+ /** Schema defaults, re-read for hand-built contexts (the loader applies them normally). */
110
+ const DEFAULT_ENABLED = true
111
+ const DEFAULT_ANNOUNCE = true
112
+ const DEFAULT_ALLOW_AGENT_IMAGE_GENERATION = true
113
+
114
+ /** Order of the announcement section within the tool-guidance band. */
115
+ const SECTION_ORDER = 150
116
+
117
+ /** Model-facing announcement: plugin presence, capabilities, and limits. */
118
+ export const IMAGEGEN_GUIDANCE = '本机已安装 dsh-imagegen 插件(DSH AI 生图):侧边栏「AI 生图」入口。能力:通过「渠道」对接 OpenAI 兼容图像生成 API(每个渠道 = 一个 API 端点 + 各自的模型目录),支持文生图(/images/generations)与图生图(/images/edits,上传参考图,grok-imagine 模型按官方 JSON image_url 协议发送,nanobanana 系列按 aspect_ratio / image_size 参数协议发送;seedream 系列统一走 /images/generations,参考图以 JSON image 数组发送;智谱 `glm-image` 使用官方 `/api/paas/v4/images/generations`,当前仅支持文生图)。API 地址与密钥在 GUI 设置中按渠道配置,密钥仅存于本机设置文档;生成请求由本地宿主代理转发,结果以 base64 返回面板,可预览与下载。模型只能使用用户在各渠道配置目录中的模型;检测模型时会过滤聊天、Embedding 等非图片模型,但模型出现在 /models 中仍不等于其网关原生支持生图协议,遇到 Qwen、Gemini 等非 OpenAI 生图协议时应如实说明上游兼容性。可一键把满意的图片加入「画廊」。内置「提示词模板库」(面板提示词框左下角「模板库」按钮):打包 awesome-gpt-image-2 的数百条提示词案例,可搜索、筛选与复用。Agent 可直接调用 `generate_image` 提交文生图,也可用 `edit_image` 图生图;默认保持工具调用等待直到任务完成,完成图片显示在工具调用对应的左侧结果区域,模型收到状态和附件引用,不会额外伪造用户消息。用户也可以使用 `/edit_image <修改描述>`,命令会直接读取当前对话最近图片并调用插件图片模型,不经过对话模型的图片能力检查。若明确需要后台执行,可传 `wait_for_completion: false`,之后再用 `get_image_generation_task` 查询;不要反复轮询。限制:生成消耗上游 API 额度;图片内容由上游模型生成,可能不符合预期或包含不适宜内容;api_key 以明文存储在设置文档中;参考图会发送至所配置的 API 服务;模板库在线刷新与参考图首次加载需要访问 vibeui.top。用户提到「生图 / 绘画 / 生成图片 / 文生图 / 图生图 / 画廊 / 提示词模板」时即指本插件,请据此协作。'
119
+
120
+ /** Append the live channel × model table so an Agent can honor user choices. */
121
+ function guidanceFor(channels: RuntimeChannel[], defaultChannelId: string): string {
122
+ if (channels.length === 0) {
123
+ return `${IMAGEGEN_GUIDANCE} 尚未配置任何渠道:请先在「设置 插件 AI 生图」添加渠道并填写 API 地址与密钥。`
124
+ }
125
+ const table = channels.map(channel => {
126
+ const aliases = channel.models.map(model => model.alias).join('、')
127
+ const mark = channel.id === defaultChannelId ? '(默认渠道)' : ''
128
+ const key = channel.apiKey === '' ? '(未填密钥)' : ''
129
+ const models = channel.models.length === 0 ? '未配置模型' : `可用模型:${aliases}`
130
+ return `渠道「${channel.name}」${mark}[${channel.apiUrl}] ${models}${key}`
131
+ }).join(';')
132
+ return `${IMAGEGEN_GUIDANCE} 当前渠道与模型:${table}。用户指定模型名时取该模型所属渠道(多渠道同名用默认渠道);未指定模型时若仅一个可用模型可直接生成,若有多个应先询问用户选择「渠道 + 模型」。`
133
+ }
134
+
135
+ /** Normalize raw channel entries into the wire shape (schema-adjacent guard). */
136
+ function normalizeChannels(value: unknown): ChannelConfig[] {
137
+ if (!Array.isArray(value)) return []
138
+ const out: ChannelConfig[] = []
139
+ for (const item of value) {
140
+ if (item === null || typeof item !== 'object') continue
141
+ const raw = item as Record<string, unknown>
142
+ const id = typeof raw.id === 'string' ? raw.id.trim() : ''
143
+ if (id === '') continue
144
+ const models: ModelMapping[] = []
145
+ if (Array.isArray(raw.models)) {
146
+ for (const entry of raw.models) {
147
+ if (entry === null || typeof entry !== 'object') continue
148
+ const record = entry as Record<string, unknown>
149
+ const alias = typeof record.alias === 'string' ? record.alias.trim() : ''
150
+ const upstream = typeof record.id === 'string' ? record.id.trim() : ''
151
+ if (alias === '') continue
152
+ models.push({ alias, id: upstream === '' ? alias : upstream })
153
+ }
154
+ }
155
+ out.push({
156
+ id,
157
+ preset: typeof raw.preset === 'string' ? raw.preset : '',
158
+ name: typeof raw.name === 'string' ? raw.name.trim() : '',
159
+ apiUrl: typeof raw.apiUrl === 'string' ? raw.apiUrl.trim() : '',
160
+ models,
161
+ })
162
+ }
163
+ return out
164
+ }
165
+
166
+ /** Effective config (schema defaults applied + legacy migration). */
167
+ export interface EffectiveConfig {
168
+ enabled: boolean
169
+ announceToAgent: boolean
170
+ allowAgentImageGeneration: boolean
171
+ channels: RuntimeChannel[]
172
+ defaultChannelId: string
173
+ promptApiUrl: string
174
+ promptApiKey: string
175
+ promptModel: string
176
+ }
177
+
178
+ /**
179
+ * Mount the settings section, routes, and announcement.
180
+ * @param ctx - host plugin context carrying webServer/systemPrompt.
181
+ * @param config - resolved plugin config (schema defaults applied by the loader).
182
+ */
183
+ export function apply(ctx: Context, config?: Config): void {
184
+ // The live source the surfaces read: the settings section once the settings
185
+ // service is attached, the composition entry otherwise.
186
+ let current: () => Config = () => config ?? {}
187
+ const resolve = (): EffectiveConfig => {
188
+ const value = current() ?? {}
189
+ let channels = normalizeChannels(value.channels)
190
+ // Settings scopes are deep-frozen by the host. Legacy migration adds the
191
+ // synthesized default-channel secret, so always work on a detached copy.
192
+ const secrets: Record<string, string> = { ...(value.channelSecrets ?? {}) }
193
+ // Legacy single-endpoint migration: no channels yet → synthesize the
194
+ // default channel from the old flat fields so upgrades never break.
195
+ if (channels.length === 0) {
196
+ const legacyUrl = typeof value.apiUrl === 'string' ? value.apiUrl.trim() : ''
197
+ const legacyModels: ModelMapping[] = Array.isArray(value.imageModels)
198
+ ? value.imageModels
199
+ .filter((model): model is string => typeof model === 'string' && model.trim() !== '')
200
+ .map(model => ({ alias: model.trim(), id: model.trim() }))
201
+ : []
202
+ if (legacyUrl !== '' || legacyModels.length > 0) {
203
+ channels = [{ id: 'default', preset: '', name: '默认渠道', apiUrl: legacyUrl, models: legacyModels }]
204
+ const legacyKey = typeof value.apiKey === 'string' ? value.apiKey.trim() : ''
205
+ if (legacyKey !== '') secrets['default'] = legacyKey
206
+ }
207
+ }
208
+ const named = channels.map(channel => ({
209
+ ...channel,
210
+ name: channel.name === '' ? (presetById(channel.preset)?.name ?? '未命名渠道') : channel.name,
211
+ }))
212
+ const defaultChannelId = typeof value.defaultChannelId === 'string' && named.some(channel => channel.id === value.defaultChannelId)
213
+ ? value.defaultChannelId
214
+ : named[0]?.id ?? ''
215
+ return {
216
+ enabled: value.enabled ?? DEFAULT_ENABLED,
217
+ announceToAgent: value.announceToAgent ?? DEFAULT_ANNOUNCE,
218
+ allowAgentImageGeneration: value.allowAgentImageGeneration ?? DEFAULT_ALLOW_AGENT_IMAGE_GENERATION,
219
+ channels: named.map(channel => ({
220
+ ...channel,
221
+ apiKey: typeof secrets[channel.id] === 'string' ? secrets[channel.id] : '',
222
+ })),
223
+ defaultChannelId,
224
+ promptApiUrl: typeof value.promptApiUrl === 'string' ? value.promptApiUrl.trim() : '',
225
+ promptApiKey: typeof value.promptApiKey === 'string' ? value.promptApiKey.trim() : '',
226
+ promptModel: typeof value.promptModel === 'string' ? value.promptModel.trim() : '',
227
+ }
228
+ }
229
+
230
+ // Transient helper used by several mount points below: resolve the shared
231
+ // channel view once per access; the runtime then picks per-request creds.
232
+ const channelsView = (): ChannelsView => {
233
+ const value = resolve()
234
+ return { channels: value.channels, defaultChannelId: value.defaultChannelId }
235
+ }
236
+
237
+ // Browser endpoints and Agent tools share the exact same serial queue. This
238
+ // keeps image persistence, cancellation, and retries coherent across both
239
+ // entry points; Agent tools wait for their task result by default and render
240
+ // images in the tool result instead of injecting a synthetic user message.
241
+ const runtime = new ImageGenerationRuntime(channelsView)
242
+ const pendingConversationImages = new Map<string, ImageAttachmentRef>()
243
+
244
+ // The route family mounts once, gated on the settings seam (the bridge
245
+ // serves it; without the seam there is nothing to expose). Route handlers
246
+ // read resolve() per request, so config edits apply live. The settings
247
+ // bridge deliberately keeps serving while the plugin is disabled — it is
248
+ // how the user re-enables the plugin from the settings card.
249
+ ctx.inject(['settings', 'attachments'], (sctx) => {
250
+ const seam = sctx.get('settings') as unknown as SettingsSeam
251
+ sctx.effect(
252
+ () => {
253
+ const routes = makeRoutes({
254
+ settings: seam,
255
+ resolve: () => {
256
+ const value = resolve()
257
+ const channel = value.channels.find(candidate => candidate.id === value.defaultChannelId) ?? value.channels[0]
258
+ return { apiUrl: channel?.apiUrl ?? '', apiKey: channel?.apiKey ?? '' }
259
+ },
260
+ resolveChannels: channelsView,
261
+ resolvePrompt: () => {
262
+ const value = resolve()
263
+ const channel = value.channels.find(candidate => candidate.id === value.defaultChannelId) ?? value.channels[0]
264
+ return {
265
+ apiUrl: value.promptApiUrl !== '' ? value.promptApiUrl : (channel?.apiUrl ?? ''),
266
+ apiKey: value.promptApiKey !== '' ? value.promptApiKey : (channel?.apiKey ?? ''),
267
+ model: value.promptModel,
268
+ }
269
+ },
270
+ resolveImageModels: () => {
271
+ const value = resolve()
272
+ return [...new Set(value.channels.flatMap(channel => channel.models.map(model => model.alias)))]
273
+ },
274
+ attachments: sctx.attachments,
275
+ pendingConversationImages,
276
+ runtime,
277
+ })
278
+ const disposers = routes.map(route => ctx.webServer.register(route))
279
+ return () => { for (const dispose of disposers) dispose() }
280
+ },
281
+ 'dsh-imagegen: routes',
282
+ )
283
+ })
284
+
285
+ ctx.inject(['tools', 'attachments', 'commands'], (tctx) => {
286
+ tctx.effect(() => {
287
+ const resolveAgentConfig = () => {
288
+ const value = resolve()
289
+ return {
290
+ enabled: value.enabled,
291
+ allowAgentImageGeneration: value.allowAgentImageGeneration,
292
+ channels: value.channels,
293
+ defaultChannelId: value.defaultChannelId,
294
+ }
295
+ }
296
+ const disposeTools = registerAgentImageTools(tctx, runtime, resolveAgentConfig)
297
+ const disposeCommand = registerEditImageCommand(tctx, runtime, resolveAgentConfig, {
298
+ get: sessionId => pendingConversationImages.get(sessionId),
299
+ consume: (sessionId, ref) => {
300
+ if (pendingConversationImages.get(sessionId)?.attachmentId === ref.attachmentId) pendingConversationImages.delete(sessionId)
301
+ },
302
+ })
303
+ return () => {
304
+ disposeCommand()
305
+ disposeTools()
306
+ }
307
+ }, 'dsh-imagegen: agent image tools and commands')
308
+ })
309
+
310
+ // System-prompt announcement (toggled by settings changes).
311
+ let disposeSection: (() => void) | undefined
312
+ const sync = (): void => {
313
+ if (disposeSection !== undefined) {
314
+ disposeSection()
315
+ disposeSection = undefined
316
+ }
317
+ const value = resolve()
318
+ if (!value.enabled || !value.announceToAgent) return
319
+ disposeSection = ctx.systemPrompt.section({
320
+ name: 'plugin:dsh-imagegen',
321
+ order: SECTION_ORDER,
322
+ text: guidanceFor(value.channels, value.defaultChannelId),
323
+ })
324
+ }
325
+
326
+ installSettingsSectionCompat(ctx, ImageGenSettingsNamespace, Config, config ?? {}, {
327
+ setSource: (source) => {
328
+ current = source
329
+ sync()
330
+ },
331
+ onChange: sync,
332
+ })
333
+
334
+ // Initial registration from the composition entry (covers deployments with
335
+ // no settings service, whose installSettingsSection never fires its hooks).
336
+ sync()
337
+ }