sang-ai-kit 0.2.0 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sang-ai-kit",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "repository": {
@@ -58,7 +58,7 @@ interface Props {
58
58
  onSelect: (id: string) => Promise<void>;
59
59
  onRename: (id: string, title: string) => Promise<void>;
60
60
  onDelete: (id: string) => Promise<void>;
61
- onLoadImage?: (sessionId: string, imageId: string) => Promise<Blob | null>;
61
+ onLoadImage?: (sessionId: string, imageId: string, type?: string) => Promise<Blob | null>;
62
62
  onDeleteImages?: (images: { sessionId: string; imageId: string }[]) => Promise<boolean>;
63
63
  onInsertImage?: (item: InsertGalleryImageInput) => Promise<unknown>;
64
64
  }
@@ -134,6 +134,7 @@ export default function SessionList({
134
134
  const [loadingImages, setLoadingImages] = useState<Record<string, boolean>>({});
135
135
  const cachedUrls = useRef<Record<string, string>>({});
136
136
  const attemptedImages = useRef(new Set<string>());
137
+ const activeLoads = useRef(0);
137
138
  const mounted = useRef(true);
138
139
 
139
140
  const label = (zh: string, en: string) => (english ? en : zh);
@@ -298,10 +299,11 @@ export default function SessionList({
298
299
  if (view !== 'gallery' || !onLoadImage) return;
299
300
 
300
301
  filteredGalleryItems.forEach(item => {
301
- if (!imageUrls[item.id] && !attemptedImages.current.has(item.id)) {
302
+ if (activeLoads.current < 4 && !imageUrls[item.id] && !attemptedImages.current.has(item.id)) {
302
303
  attemptedImages.current.add(item.id);
304
+ activeLoads.current++;
303
305
  setLoadingImages(prev => ({ ...prev, [item.id]: true }));
304
- onLoadImage(item.sessionId, item.imageId)
306
+ onLoadImage(item.sessionId, item.imageId, item.type)
305
307
  .then(blob => {
306
308
  if (blob && mounted.current && validImageIds.current.has(item.id)) {
307
309
  const url = URL.createObjectURL(blob);
@@ -311,6 +313,7 @@ export default function SessionList({
311
313
  })
312
314
  .catch(() => {})
313
315
  .finally(() => {
316
+ activeLoads.current--;
314
317
  if (mounted.current) setLoadingImages(prev => ({ ...prev, [item.id]: false }));
315
318
  });
316
319
  }
@@ -363,7 +366,7 @@ export default function SessionList({
363
366
  }
364
367
 
365
368
  if (onLoadImage) {
366
- onLoadImage(item.sessionId, item.imageId).then(blob => {
369
+ onLoadImage(item.sessionId, item.imageId, item.type).then(blob => {
367
370
  if (!blob) return;
368
371
  const tempUrl = URL.createObjectURL(blob);
369
372
  const a = document.createElement('a');
@@ -133,7 +133,7 @@ export interface AssistantViewProps {
133
133
  active: { endpoint?: unknown; model: { label?: string; supportsVision: boolean } };
134
134
  imageToolsEnabled?: boolean;
135
135
  toolLabels?: Record<string, [string, string]>;
136
- loadImage: (sessionId: string, imageId: string) => Promise<Blob | null>;
136
+ loadImage: (sessionId: string, imageId: string, type?: string) => Promise<Blob | null>;
137
137
  createImageUpload: (file: File, imageToolsEnabled?: boolean) => Promise<ImageUpload>;
138
138
  imageModelForTool: (tool: ToolEvent) => { modelId: string; label: string } | undefined;
139
139
  sumUsage: (requests: RequestUsage[]) => TokenUsage | undefined;
@@ -93,7 +93,25 @@ export function createBuiltInTools(options: BuiltInToolOptions): { tools: Respon
93
93
  prompt = args.prompt;
94
94
  } catch (error) { if (!signal.aborted) failures++; throw error; }
95
95
  if (prepared) {
96
- try { return await deliverImageResult(prepared, { images: results, model, prompt }, signal, options.saveImage, options.onImage); }
96
+ try {
97
+ const saved: SessionImage[] = [];
98
+ if (options.saveImage) {
99
+ try {
100
+ for (const blob of results) {
101
+ signal.throwIfAborted();
102
+ const image = await options.saveImage(blob, `generated-${Date.now()}-${saved.length + 1}.${blob.type === 'image/jpeg' ? 'jpg' : blob.type === 'image/webp' ? 'webp' : 'png'}`);
103
+ saved.push(image);
104
+ options.onImage?.(image);
105
+ }
106
+ signal.throwIfAborted();
107
+ } catch (error) {
108
+ if (signal.aborted) throw error;
109
+ throw new ImageApplicationError(`Image generation succeeded but saving failed: ${error instanceof Error ? error.message : String(error)}. Saved image IDs: ${saved.map(image => image.id).join(', ') || 'none'}. Do not automatically repeat image generation.`, error);
110
+ }
111
+ }
112
+ const applied = await deliverImageResult(prepared, { images: results, model, prompt }, signal, undefined, undefined, options.saveImage ? saved : undefined);
113
+ return saved.length && applied && typeof applied === 'object' && !Array.isArray(applied) ? { ...applied, images: saved } : applied;
114
+ }
97
115
  catch (error) {
98
116
  if (error instanceof ImageApplicationError) applicationFailures.add(definition.name);
99
117
  throw error;
@@ -59,16 +59,17 @@ export async function deliverImageResult(
59
59
  result: ImageToolResult,
60
60
  signal: AbortSignal,
61
61
  saveImage?: (blob: Blob, name: string) => Promise<SessionImage>,
62
- onImage?: (image: SessionImage) => void
62
+ onImage?: (image: SessionImage) => void,
63
+ archivedImages?: SessionImage[]
63
64
  ): Promise<unknown> {
64
65
  try {
65
66
  signal.throwIfAborted();
66
67
  return await prepared.complete(result, signal);
67
68
  } catch (error) {
68
69
  if (signal.aborted) throw error;
69
- const saved: SessionImage[] = [];
70
+ const saved: SessionImage[] = archivedImages ?? [];
70
71
  let recoveryError: unknown;
71
- if (saveImage) {
72
+ if (saveImage && !archivedImages) {
72
73
  try {
73
74
  for (const [index, blob] of result.images.entries()) {
74
75
  const extension = blob.type === 'image/jpeg' ? 'jpg' : blob.type === 'image/webp' ? 'webp' : 'png';