@@ -8,19 +8,20 @@ import {
8
8
ActionResult ,
9
9
ActionResultSuccess ,
10
10
} from "@typeagent/agent-sdk" ;
11
- import { StopWatch } from "common-utils" ;
11
+ import { downloadImage , StopWatch } from "common-utils" ;
12
12
import {
13
13
createActionResult ,
14
14
createActionResultFromHtmlDisplayWithScript ,
15
15
} from "@typeagent/agent-sdk/helpers/action" ;
16
16
import { bing , GeneratedImage , openai } from "aiclient" ;
17
17
import { Image } from "../../../aiclient/dist/bing.js" ;
18
- import { randomBytes } from "crypto" ;
18
+ import { randomBytes , randomUUID } from "crypto" ;
19
19
import {
20
20
CreateImageAction ,
21
21
FindImageAction ,
22
22
ImageAction ,
23
23
} from "./imageActionSchema.js" ;
24
+ import path from "path" ;
24
25
25
26
export function instantiate ( ) : AppAgent {
26
27
return {
@@ -75,11 +76,6 @@ async function handlePhotoAction(
75
76
result = createActionResult (
76
77
`Unable to find any images for ${ findImageAction . parameters . searchTerm } ` ,
77
78
) ;
78
- // } else if (searchResults.length == 1) {
79
- // result = createActionResultFromHtmlDisplay(
80
- // `<img class="chat-input-image" src="${searchResults[0].contentUrl}" />`,
81
- // "Found 1 image.",
82
- // );
83
79
} else {
84
80
const urls : string [ ] = [ ] ;
85
81
const captions : string [ ] = [ ] ;
@@ -88,6 +84,15 @@ async function handlePhotoAction(
88
84
captions . push ( findImageAction . parameters . searchTerm ) ;
89
85
} ) ;
90
86
result = createCarouselForImages ( urls , captions ) ;
87
+
88
+ // add the found images to the entities
89
+ for ( let i = 0 ; i < searchResults . length ; i ++ ) {
90
+ result . entities . push ( {
91
+ name : path . basename ( searchResults [ i ] . contentUrl ) ,
92
+ type : [ "image" , "url" , "search" ] ,
93
+ additionalEntityText : searchResults [ i ] . contentUrl ,
94
+ } ) ;
95
+ }
91
96
}
92
97
break ;
93
98
}
@@ -141,6 +146,23 @@ async function handlePhotoAction(
141
146
captions . push ( i . revised_prompt ) ;
142
147
} ) ;
143
148
result = createCarouselForImages ( urls , captions ) ;
149
+
150
+ // save the generated image in the session store and add the image to the knoweledge store
151
+ const id = randomUUID ( ) ;
152
+ const fileName = `../generated_images/${ id . toString ( ) } .png` ;
153
+ if (
154
+ await downloadImage (
155
+ urls [ 0 ] ,
156
+ fileName ,
157
+ photoContext . sessionContext . sessionStorage ! ,
158
+ )
159
+ ) {
160
+ // add the generated image to the entities
161
+ result . entities . push ( {
162
+ name : fileName . substring ( 3 ) ,
163
+ type : [ "file" , "image" , "ai_generated" ] ,
164
+ } ) ;
165
+ }
144
166
}
145
167
break ;
146
168
default :
@@ -153,6 +175,7 @@ function createCarouselForImages(
153
175
images : string [ ] ,
154
176
captions : string [ ] ,
155
177
) : ActionResultSuccess {
178
+ let literal : string = `There are ${ images . length } shown. ` ;
156
179
const hash : string = randomBytes ( 4 ) . readUInt32LE ( 0 ) . toString ( ) ;
157
180
const jScript : string = `
158
181
<script>
@@ -216,6 +239,8 @@ function createCarouselForImages(
216
239
</div>` ;
217
240
218
241
carouselDots += `<span class="dot ${ hash } " onclick="slideShow_${ hash } .currentSlide(${ index + 1 } )"></span>` ;
242
+
243
+ literal += `Image ${ index + 1 } : ${ url } , Caption: ${ captions [ index ] } ` ;
219
244
} ) ;
220
245
221
246
const carousel_end : string = `
@@ -233,6 +258,6 @@ function createCarouselForImages(
233
258
234
259
return createActionResultFromHtmlDisplayWithScript (
235
260
carousel_start + carousel + carousel_end + jScript ,
236
- `There are ${ images . length } shown.` ,
261
+ literal ,
237
262
) ;
238
263
}
0 commit comments