@adminforth/agent 3.4.0 → 3.5.0
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/dist/custom/ComposerAttachments.vue +4 -4
- package/dist/custom/conversation_area/MessageAttachments.vue +1 -1
- package/dist/domain/imageAttachments.d.ts +4 -2
- package/dist/domain/imageAttachments.js +12 -4
- package/dist/llm/agentRuntime.d.ts +3 -0
- package/dist/llm/middleware/attachmentsMiddleware.d.ts +4 -3
- package/dist/llm/middleware/attachmentsMiddleware.js +46 -37
- package/dist/tools/viewImage.d.ts +13 -3
- package/dist/tools/viewImage.js +66 -15
- package/dist/types.d.ts +12 -3
- package/package.json +1 -1
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
:key="attachment.id"
|
|
6
6
|
class="af-agent-composer-attachment relative"
|
|
7
7
|
>
|
|
8
|
-
|
|
8
|
+
<!-- natural proportions: a cropped square would be zoomed cropped too -->
|
|
9
|
+
<ZoomableImage
|
|
9
10
|
:src="attachment.url"
|
|
10
|
-
|
|
11
|
-
class="w-14 h-14 object-cover rounded-default border border-lightInputBorder dark:border-darkInputBorder"
|
|
11
|
+
class="block max-h-14 max-w-28 rounded-default border border-lightInputBorder dark:border-darkInputBorder"
|
|
12
12
|
/>
|
|
13
13
|
<Button
|
|
14
14
|
variant="secondary"
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
<script setup lang="ts">
|
|
31
31
|
import { IconX } from '@iconify-prerendered/vue-lucide';
|
|
32
|
-
import { Button } from '@/afcl';
|
|
32
|
+
import { Button, ZoomableImage } from '@/afcl';
|
|
33
33
|
import { useAgentStore } from './composables/useAgentStore';
|
|
34
34
|
|
|
35
35
|
const agentStore = useAgentStore();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div class="af-agent-message-attachments self-end flex flex-wrap justify-end gap-2 mr-4 mb-1">
|
|
2
|
+
<div class="af-agent-message-attachments self-end flex flex-wrap items-end justify-end gap-2 mr-4 mb-1">
|
|
3
3
|
<ZoomableImage
|
|
4
4
|
v-for="attachment in attachments"
|
|
5
5
|
:key="attachment.url"
|
|
@@ -32,8 +32,8 @@ export declare function getAttachmentsFrontendConfig(limits: ImageAttachmentsLim
|
|
|
32
32
|
mimeTypes: string[];
|
|
33
33
|
};
|
|
34
34
|
export declare const MISSING_IMAGE_TEXT = "[An image of this message is no longer available]";
|
|
35
|
-
export declare const
|
|
36
|
-
export declare const
|
|
35
|
+
export declare const hiddenImagesText: (filePaths: string[]) => string;
|
|
36
|
+
export declare const viewedBelowImagesText: (filePaths: string[]) => string;
|
|
37
37
|
export declare const viewedImageLabel: (filePath: string) => string;
|
|
38
38
|
/**
|
|
39
39
|
* What view_image loaded, kept as the artifact of its tool result: the attachments middleware
|
|
@@ -41,6 +41,8 @@ export declare const viewedImageLabel: (filePath: string) => string;
|
|
|
41
41
|
*/
|
|
42
42
|
export type ViewImageArtifact = {
|
|
43
43
|
filePaths: string[];
|
|
44
|
+
keptFilePaths?: string[];
|
|
45
|
+
turnId?: string;
|
|
44
46
|
viewedAt: string;
|
|
45
47
|
};
|
|
46
48
|
/** The part of the UploadPlugin (@adminforth/upload) the agent relies on. */
|
|
@@ -33,20 +33,26 @@ export const DEFAULT_IMAGE_ATTACHMENTS_LIMITS = {
|
|
|
33
33
|
maxPerMessage: 5,
|
|
34
34
|
visibleMinutes: 30,
|
|
35
35
|
maxVisible: 5,
|
|
36
|
+
maxViewedPerTurn: 10,
|
|
36
37
|
};
|
|
37
38
|
/** The `imageAttachments` options with the defaults for the ones not set; each is a whole number from 1. */
|
|
38
39
|
export function resolveImageAttachmentsLimits(options = {}) {
|
|
39
|
-
var _a, _b, _c;
|
|
40
|
+
var _a, _b, _c, _d;
|
|
40
41
|
const limits = {
|
|
41
42
|
maxPerMessage: (_a = options.maxPerMessage) !== null && _a !== void 0 ? _a : DEFAULT_IMAGE_ATTACHMENTS_LIMITS.maxPerMessage,
|
|
42
43
|
visibleMinutes: (_b = options.visibleMinutes) !== null && _b !== void 0 ? _b : DEFAULT_IMAGE_ATTACHMENTS_LIMITS.visibleMinutes,
|
|
43
44
|
maxVisible: (_c = options.maxVisible) !== null && _c !== void 0 ? _c : DEFAULT_IMAGE_ATTACHMENTS_LIMITS.maxVisible,
|
|
45
|
+
maxViewedPerTurn: (_d = options.maxViewedPerTurn) !== null && _d !== void 0 ? _d : DEFAULT_IMAGE_ATTACHMENTS_LIMITS.maxViewedPerTurn,
|
|
44
46
|
};
|
|
45
47
|
for (const [name, value] of Object.entries(limits)) {
|
|
46
48
|
if (!Number.isInteger(value) || value < 1) {
|
|
47
49
|
throw new Error(`imageAttachments.${name} must be a whole number from 1, got ${value}`);
|
|
48
50
|
}
|
|
49
51
|
}
|
|
52
|
+
// view_image loads all images of a message, so a whole message has to fit into one answer
|
|
53
|
+
if (limits.maxPerMessage > limits.maxViewedPerTurn) {
|
|
54
|
+
throw new Error(`imageAttachments.maxPerMessage (${limits.maxPerMessage}) must not exceed maxViewedPerTurn (${limits.maxViewedPerTurn})`);
|
|
55
|
+
}
|
|
50
56
|
return limits;
|
|
51
57
|
}
|
|
52
58
|
/** Limits of the chat composer, passed to the frontend so both sides share them. */
|
|
@@ -58,6 +64,8 @@ export function getAttachmentsFrontendConfig(limits) {
|
|
|
58
64
|
}
|
|
59
65
|
// notes the model gets in place of images which are not shown
|
|
60
66
|
export const MISSING_IMAGE_TEXT = "[An image of this message is no longer available]";
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
export const
|
|
67
|
+
// one note for all such images of a message: the model takes them as one group
|
|
68
|
+
const imageIdsText = (filePaths) => filePaths.map((filePath) => `"${filePath}"`).join(", ");
|
|
69
|
+
export const hiddenImagesText = (filePaths) => `[Images of this message not shown now: ${imageIdsText(filePaths)}. To answer about what they show, call ${VIEW_IMAGE_TOOL_NAME} with these ids first.]`;
|
|
70
|
+
export const viewedBelowImagesText = (filePaths) => `[Images of this message shown further below: ${imageIdsText(filePaths)}.]`;
|
|
71
|
+
export const viewedImageLabel = (filePath) => `[Image "${filePath}"]`;
|
|
@@ -19,10 +19,13 @@ export declare class AgentRuntime {
|
|
|
19
19
|
constructor(options: AgentRuntimeOptions);
|
|
20
20
|
stream(input: AgentRuntimeRunInput): Promise<AsyncGenerator<["messages", [import("langchain").BaseMessage<import("@langchain/core/messages").MessageStructure<import("@langchain/core/messages").MessageToolSet>, import("@langchain/core/messages").MessageType>, Record<string, any>]] | ["updates", Record<string, Omit<import("langchain").BuiltInState<import("@langchain/core/messages").MessageStructure<import("langchain").ToolsToMessageToolSet<readonly (import("@langchain/core/tools").ClientTool | import("@langchain/core/tools").ServerTool)[] | readonly (import("@langchain/core/tools").ClientTool | import("@langchain/core/tools").ServerTool | import("langchain").DynamicStructuredTool<import("zod").ZodObject<{
|
|
21
21
|
imageIds: import("zod").ZodArray<import("zod").ZodString>;
|
|
22
|
+
keepImageIds: import("zod").ZodArray<import("zod").ZodString>;
|
|
22
23
|
}, import("zod/v4/core").$strip>, {
|
|
23
24
|
imageIds: string[];
|
|
25
|
+
keepImageIds: string[];
|
|
24
26
|
}, {
|
|
25
27
|
imageIds: string[];
|
|
28
|
+
keepImageIds: string[];
|
|
26
29
|
}, [string, import("../domain/imageAttachments.js").ViewImageArtifact], unknown, "view_image">)[]>>>, "jumpTo">>], void, any>>;
|
|
27
30
|
private getThreadState;
|
|
28
31
|
/**
|
|
@@ -5,9 +5,10 @@ import { type ImageAttachmentsLimits } from "../../domain/imageAttachments.js";
|
|
|
5
5
|
* not the debug trace or the summarization of other middleware. Created per turn, so an
|
|
6
6
|
* image is loaded from the storage once for all model calls of the turn.
|
|
7
7
|
*
|
|
8
|
-
* An image is shown for `visibleMinutes` after its message is sent
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* An image is shown for `visibleMinutes` after its message is sent or a view_image keeps it, at most
|
|
9
|
+
* `maxVisible` at once; the other images of a message become one note with their ids and are not
|
|
10
|
+
* read at all. Images the model loads again with view_image are all shown for the answer of that
|
|
11
|
+
* turn and follow the tool results of that step as one user message: a user message between tool results is refused by providers, and provider
|
|
11
12
|
* adapters may send only the messages after their previous response (OpenAI previous_response_id),
|
|
12
13
|
* so a change in an earlier message would not reach the model. An image which can not be read
|
|
13
14
|
* (deleted from the storage, the storage changed) becomes a note too, so one broken file does not
|
|
@@ -9,46 +9,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { logger } from "adminforth";
|
|
11
11
|
import { HumanMessage, ToolMessage, createMiddleware } from "langchain";
|
|
12
|
+
import { getViewedImages } from "../../tools/viewImage.js";
|
|
12
13
|
import { getErrorMessage } from "../../shared/errors.js";
|
|
13
|
-
import { ATTACHMENTS_SENT_AT_KWARG, MISSING_IMAGE_TEXT,
|
|
14
|
-
/** What a view_image result loaded; a failed call (bad arguments, tool off) has no artifact. */
|
|
15
|
-
function getViewedImages(message) {
|
|
16
|
-
var _a;
|
|
17
|
-
return ToolMessage.isInstance(message) && message.name === VIEW_IMAGE_TOOL_NAME
|
|
18
|
-
? (_a = message.artifact) !== null && _a !== void 0 ? _a : null
|
|
19
|
-
: null;
|
|
20
|
-
}
|
|
14
|
+
import { ATTACHMENTS_SENT_AT_KWARG, MISSING_IMAGE_TEXT, getAttachmentPaths, hiddenImagesText, viewedBelowImagesText, viewedImageLabel, } from "../../domain/imageAttachments.js";
|
|
21
15
|
/**
|
|
22
|
-
* The images to show now, by the message they are shown in
|
|
23
|
-
*
|
|
24
|
-
* latest `maxVisible` only
|
|
16
|
+
* The images to show now, by the message they are shown in. All the images view_image loaded in
|
|
17
|
+
* this turn, once each, after its latest result that has them. Besides them the kept ones: sent in
|
|
18
|
+
* a message or kept by an earlier view_image for `visibleMinutes`, the latest `maxVisible` only
|
|
19
|
+
* (more when the last images were a view_image which kept more); one loaded in this turn keeps its
|
|
20
|
+
* place among them but is shown after the view_image result.
|
|
25
21
|
*/
|
|
26
|
-
function getShownImages(messages, now, { visibleMinutes, maxVisible }) {
|
|
27
|
-
var _a;
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (now - at < visibleMs && at > ((_b = (_a = latestByFilePath.get(filePath)) === null || _a === void 0 ? void 0 : _a.at) !== null && _b !== void 0 ? _b : 0)) {
|
|
33
|
-
latestByFilePath.set(filePath, { message, at });
|
|
34
|
-
}
|
|
35
|
-
};
|
|
22
|
+
function getShownImages(messages, now, turnId, { visibleMinutes, maxVisible }) {
|
|
23
|
+
var _a, _b, _c, _d;
|
|
24
|
+
const viewedNow = new Map();
|
|
25
|
+
const kept = [];
|
|
26
|
+
// a view_image which kept more than maxVisible widens the buffer until the next images
|
|
27
|
+
let bufferSize = maxVisible;
|
|
36
28
|
for (const message of messages) {
|
|
37
29
|
const filePaths = getAttachmentPaths(message);
|
|
38
30
|
const sentAt = (_a = message.additional_kwargs) === null || _a === void 0 ? void 0 : _a[ATTACHMENTS_SENT_AT_KWARG];
|
|
39
31
|
if (filePaths && sentAt) {
|
|
40
|
-
filePaths.
|
|
32
|
+
kept.push(...filePaths.map((filePath) => ({ filePath, message, at: Date.parse(sentAt) })));
|
|
33
|
+
bufferSize = maxVisible;
|
|
41
34
|
}
|
|
42
35
|
const viewed = getViewedImages(message);
|
|
43
|
-
if (viewed) {
|
|
44
|
-
viewed.filePaths.forEach((filePath) =>
|
|
36
|
+
if ((viewed === null || viewed === void 0 ? void 0 : viewed.turnId) === turnId) {
|
|
37
|
+
viewed.filePaths.forEach((filePath) => viewedNow.set(filePath, message));
|
|
38
|
+
bufferSize = maxVisible;
|
|
39
|
+
}
|
|
40
|
+
else if (viewed) {
|
|
41
|
+
const keptFilePaths = (_b = viewed.keptFilePaths) !== null && _b !== void 0 ? _b : viewed.filePaths;
|
|
42
|
+
kept.push(...keptFilePaths.map((filePath) => ({ filePath, message, at: Date.parse(viewed.viewedAt) })));
|
|
43
|
+
bufferSize = Math.max(maxVisible, keptFilePaths.length);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const visibleMs = visibleMinutes * 60000;
|
|
47
|
+
const latestByFilePath = new Map();
|
|
48
|
+
for (const { filePath, message, at } of kept) {
|
|
49
|
+
if (now - at < visibleMs && at > ((_d = (_c = latestByFilePath.get(filePath)) === null || _c === void 0 ? void 0 : _c.at) !== null && _d !== void 0 ? _d : 0)) {
|
|
50
|
+
latestByFilePath.set(filePath, { message, at });
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
const shown = new Map();
|
|
54
|
+
const show = (filePath, message) => { var _a; return shown.set(message, [...((_a = shown.get(message)) !== null && _a !== void 0 ? _a : []), filePath]); };
|
|
48
55
|
[...latestByFilePath]
|
|
49
56
|
.sort(([, a], [, b]) => b.at - a.at)
|
|
50
|
-
.slice(0,
|
|
51
|
-
.
|
|
57
|
+
.slice(0, bufferSize)
|
|
58
|
+
.filter(([filePath]) => !viewedNow.has(filePath))
|
|
59
|
+
.forEach(([filePath, { message }]) => show(filePath, message));
|
|
60
|
+
viewedNow.forEach((message, filePath) => show(filePath, message));
|
|
52
61
|
return shown;
|
|
53
62
|
}
|
|
54
63
|
/**
|
|
@@ -57,9 +66,10 @@ function getShownImages(messages, now, { visibleMinutes, maxVisible }) {
|
|
|
57
66
|
* not the debug trace or the summarization of other middleware. Created per turn, so an
|
|
58
67
|
* image is loaded from the storage once for all model calls of the turn.
|
|
59
68
|
*
|
|
60
|
-
* An image is shown for `visibleMinutes` after its message is sent
|
|
61
|
-
*
|
|
62
|
-
*
|
|
69
|
+
* An image is shown for `visibleMinutes` after its message is sent or a view_image keeps it, at most
|
|
70
|
+
* `maxVisible` at once; the other images of a message become one note with their ids and are not
|
|
71
|
+
* read at all. Images the model loads again with view_image are all shown for the answer of that
|
|
72
|
+
* turn and follow the tool results of that step as one user message: a user message between tool results is refused by providers, and provider
|
|
63
73
|
* adapters may send only the messages after their previous response (OpenAI previous_response_id),
|
|
64
74
|
* so a change in an earlier message would not reach the model. An image which can not be read
|
|
65
75
|
* (deleted from the storage, the storage changed) becomes a note too, so one broken file does not
|
|
@@ -90,7 +100,7 @@ export function createAttachmentsMiddleware(loadAsDataUrl, limits) {
|
|
|
90
100
|
if (!request.messages.some((message) => getAttachmentPaths(message) || getViewedImages(message))) {
|
|
91
101
|
return handler(request);
|
|
92
102
|
}
|
|
93
|
-
const shownImages = getShownImages(request.messages, Date.now(), limits);
|
|
103
|
+
const shownImages = getShownImages(request.messages, Date.now(), request.runtime.context.turnId, limits);
|
|
94
104
|
// images loaded again are shown after the tool results, their own message only points there
|
|
95
105
|
const viewedImages = new Set([...shownImages]
|
|
96
106
|
.filter(([message]) => getViewedImages(message))
|
|
@@ -101,17 +111,16 @@ export function createAttachmentsMiddleware(loadAsDataUrl, limits) {
|
|
|
101
111
|
const shown = (_a = shownImages.get(message)) !== null && _a !== void 0 ? _a : [];
|
|
102
112
|
const filePaths = getAttachmentPaths(message);
|
|
103
113
|
if (filePaths) {
|
|
104
|
-
const images = yield Promise.all(filePaths.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const text = viewedImages.has(filePath) ? viewedBelowImageText(filePath) : hiddenImageText(filePath);
|
|
109
|
-
return { type: "text", text };
|
|
110
|
-
}));
|
|
114
|
+
const images = yield Promise.all(filePaths.filter((filePath) => shown.includes(filePath)).map(imageBlock));
|
|
115
|
+
const notShown = filePaths.filter((filePath) => !shown.includes(filePath));
|
|
116
|
+
const viewedBelow = notShown.filter((filePath) => viewedImages.has(filePath));
|
|
117
|
+
const hidden = notShown.filter((filePath) => !viewedImages.has(filePath));
|
|
111
118
|
messages.push(new HumanMessage({
|
|
112
119
|
content: [
|
|
113
120
|
...(message.content ? [{ type: "text", text: message.content }] : []),
|
|
114
121
|
...images,
|
|
122
|
+
...(viewedBelow.length ? [{ type: "text", text: viewedBelowImagesText(viewedBelow) }] : []),
|
|
123
|
+
...(hidden.length ? [{ type: "text", text: hiddenImagesText(hidden) }] : []),
|
|
115
124
|
],
|
|
116
125
|
}));
|
|
117
126
|
continue;
|
|
@@ -1,13 +1,23 @@
|
|
|
1
|
+
import { type BaseMessage } from "langchain";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
import { type ImageAttachmentsLimits, type ViewImageArtifact } from "../domain/imageAttachments.js";
|
|
4
|
+
/** What a view_image result loaded; a failed call (bad arguments, tool off) has no artifact. */
|
|
5
|
+
export declare function getViewedImages(message: BaseMessage): ViewImageArtifact | null;
|
|
3
6
|
/**
|
|
4
|
-
* Shows again images of earlier user messages
|
|
5
|
-
*
|
|
7
|
+
* Shows again images of earlier user messages, once per answer: the model has to pick all the
|
|
8
|
+
* likely messages at once and not search them one call after another. An id loads all images of
|
|
9
|
+
* its message, the model can not tell which image of a message holds what it needs. Messages go in
|
|
10
|
+
* the order of the ids, the most likely first, while they fit into `maxViewedPerTurn` images. The
|
|
11
|
+
* loaded images go to the artifact of the tool result: the attachments middleware shows all of
|
|
12
|
+
* them for the answer of this turn, and the kept ones for a while after it.
|
|
6
13
|
*/
|
|
7
|
-
export declare function createViewImageTool({ visibleMinutes, maxVisible }: ImageAttachmentsLimits): import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
14
|
+
export declare function createViewImageTool({ visibleMinutes, maxVisible, maxViewedPerTurn }: ImageAttachmentsLimits): import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
8
15
|
imageIds: z.ZodArray<z.ZodString>;
|
|
16
|
+
keepImageIds: z.ZodArray<z.ZodString>;
|
|
9
17
|
}, z.core.$strip>, {
|
|
10
18
|
imageIds: string[];
|
|
19
|
+
keepImageIds: string[];
|
|
11
20
|
}, {
|
|
12
21
|
imageIds: string[];
|
|
22
|
+
keepImageIds: string[];
|
|
13
23
|
}, [string, ViewImageArtifact], unknown, "view_image">;
|
package/dist/tools/viewImage.js
CHANGED
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import path from "path";
|
|
11
|
-
import { tool } from "langchain";
|
|
11
|
+
import { ToolMessage, tool } from "langchain";
|
|
12
12
|
import { z } from "zod";
|
|
13
13
|
import { VIEW_IMAGE_TOOL_NAME, getAttachmentPaths, } from "../domain/imageAttachments.js";
|
|
14
14
|
/**
|
|
@@ -25,32 +25,83 @@ function resolveImagePath(filePaths, imageId) {
|
|
|
25
25
|
});
|
|
26
26
|
return byFileName.length === 1 ? byFileName[0] : null;
|
|
27
27
|
}
|
|
28
|
+
/** What a view_image result loaded; a failed call (bad arguments, tool off) has no artifact. */
|
|
29
|
+
export function getViewedImages(message) {
|
|
30
|
+
var _a;
|
|
31
|
+
return ToolMessage.isInstance(message) && message.name === VIEW_IMAGE_TOOL_NAME
|
|
32
|
+
? (_a = message.artifact) !== null && _a !== void 0 ? _a : null
|
|
33
|
+
: null;
|
|
34
|
+
}
|
|
28
35
|
/**
|
|
29
|
-
* Shows again images of earlier user messages
|
|
30
|
-
*
|
|
36
|
+
* Shows again images of earlier user messages, once per answer: the model has to pick all the
|
|
37
|
+
* likely messages at once and not search them one call after another. An id loads all images of
|
|
38
|
+
* its message, the model can not tell which image of a message holds what it needs. Messages go in
|
|
39
|
+
* the order of the ids, the most likely first, while they fit into `maxViewedPerTurn` images. The
|
|
40
|
+
* loaded images go to the artifact of the tool result: the attachments middleware shows all of
|
|
41
|
+
* them for the answer of this turn, and the kept ones for a while after it.
|
|
31
42
|
*/
|
|
32
|
-
export function createViewImageTool({ visibleMinutes, maxVisible }) {
|
|
43
|
+
export function createViewImageTool({ visibleMinutes, maxVisible, maxViewedPerTurn }) {
|
|
33
44
|
const viewImageSchema = z.object({
|
|
34
45
|
imageIds: z
|
|
35
46
|
.array(z.string())
|
|
36
47
|
.min(1)
|
|
37
|
-
.max(
|
|
38
|
-
.describe("Ids
|
|
48
|
+
.max(maxViewedPerTurn)
|
|
49
|
+
.describe("Ids copied from the notes shown in place of the images, never made up; the most likely message first."),
|
|
50
|
+
keepImageIds: z
|
|
51
|
+
.array(z.string())
|
|
52
|
+
.max(maxViewedPerTurn)
|
|
53
|
+
.describe(`Ids out of imageIds to keep seeing in the next messages: the ones the conversation is about, usually up to ${maxVisible}. More are kept only until the next images. Empty when none is needed later.`),
|
|
39
54
|
});
|
|
40
|
-
return tool((_a, runtime_1) => __awaiter(this, [_a, runtime_1], void 0, function* ({ imageIds }, runtime) {
|
|
41
|
-
// the agent passes its state to every tool call next to the run config
|
|
42
|
-
const {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
return tool((_a, runtime_1) => __awaiter(this, [_a, runtime_1], void 0, function* ({ imageIds, keepImageIds }, runtime) {
|
|
56
|
+
// the agent passes its state and its context to every tool call next to the run config
|
|
57
|
+
const { state, context } = runtime;
|
|
58
|
+
const viewedAt = new Date().toISOString();
|
|
59
|
+
if (state.messages.some((message) => { var _a; return ((_a = getViewedImages(message)) === null || _a === void 0 ? void 0 : _a.turnId) === context.turnId; })) {
|
|
60
|
+
return [
|
|
61
|
+
JSON.stringify({
|
|
62
|
+
error: "ALREADY_VIEWED",
|
|
63
|
+
message: "This answer already loaded images. Answer from them, or ask the user which images matter or to send them again.",
|
|
64
|
+
}, null, 2),
|
|
65
|
+
{ filePaths: [], keptFilePaths: [], turnId: context.turnId, viewedAt },
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
const imagesByMessage = state.messages.flatMap((message) => {
|
|
69
|
+
const filePaths = getAttachmentPaths(message);
|
|
70
|
+
return filePaths ? [filePaths] : [];
|
|
71
|
+
});
|
|
72
|
+
const filePathsInHistory = [...new Set(imagesByMessage.flat())];
|
|
73
|
+
const resolve = (imageId) => resolveImagePath(filePathsInHistory, imageId);
|
|
74
|
+
const resolved = imageIds.map((imageId) => ({ imageId, filePath: resolve(imageId) }));
|
|
46
75
|
const notFound = resolved.filter(({ filePath }) => !filePath).map(({ imageId }) => imageId);
|
|
76
|
+
// the messages of the found images, in the order of the ids
|
|
77
|
+
const groups = [...new Set(resolved.flatMap(({ filePath }) => (filePath ? [imagesByMessage.find((filePaths) => filePaths.includes(filePath))] : [])))];
|
|
78
|
+
let filePaths = [];
|
|
79
|
+
const overLimit = [];
|
|
80
|
+
for (const group of groups) {
|
|
81
|
+
const withGroup = [...new Set([...filePaths, ...group])];
|
|
82
|
+
if (withGroup.length > maxViewedPerTurn) {
|
|
83
|
+
overLimit.push(...group.filter((filePath) => !filePaths.includes(filePath)));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
filePaths = withGroup;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const keptFilePaths = [...new Set(keepImageIds.map(resolve))].filter((filePath) => (filePath !== null && filePaths.includes(filePath)));
|
|
47
90
|
return [
|
|
48
|
-
JSON.stringify({ shown: filePaths, notFound }, null, 2),
|
|
49
|
-
{ filePaths,
|
|
91
|
+
JSON.stringify({ shown: filePaths, kept: keptFilePaths, notFound, overLimit }, null, 2),
|
|
92
|
+
{ filePaths, keptFilePaths, turnId: context.turnId, viewedAt },
|
|
50
93
|
];
|
|
51
94
|
}), {
|
|
52
95
|
name: VIEW_IMAGE_TOOL_NAME,
|
|
53
|
-
description:
|
|
96
|
+
description: [
|
|
97
|
+
"Shows again images of earlier messages which are replaced with notes, by the ids written in these notes.",
|
|
98
|
+
"Call it before answering anything that depends on what such an image shows: your earlier replies about it are a retelling, not a source, and miss details.",
|
|
99
|
+
"Don't call it for images you see in messages, they are already in full quality, and for a message with new images and no text, it is about the new images only.",
|
|
100
|
+
"Pick the messages by the conversation: your reply right after a user message is about the images of that message.",
|
|
101
|
+
"Call it once per answer, a second call is refused: pass one id of every message whose texts or replies may match the request, the most likely first; an id loads all images of its message.",
|
|
102
|
+
`It loads ${maxViewedPerTurn} images at most; messages over it are not loaded and are listed in overLimit, if the loaded ones are not enough ask the user which images matter or to send them again.`,
|
|
103
|
+
`You see them for this answer and keep seeing the ones in keepImageIds for ${visibleMinutes} minutes.`,
|
|
104
|
+
].join(" "),
|
|
54
105
|
schema: viewImageSchema,
|
|
55
106
|
responseFormat: "content_and_artifact",
|
|
56
107
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -31,7 +31,8 @@ interface ITurnResource {
|
|
|
31
31
|
}
|
|
32
32
|
export interface IImageAttachmentsOptions {
|
|
33
33
|
/**
|
|
34
|
-
* How many images one message can have
|
|
34
|
+
* How many images one message can have, at most `maxViewedPerTurn`: the `view_image` tool
|
|
35
|
+
* loads all images of a message at once.
|
|
35
36
|
* Default is 5
|
|
36
37
|
*/
|
|
37
38
|
maxPerMessage?: number;
|
|
@@ -42,11 +43,19 @@ export interface IImageAttachmentsOptions {
|
|
|
42
43
|
*/
|
|
43
44
|
visibleMinutes?: number;
|
|
44
45
|
/**
|
|
45
|
-
* How many images the model
|
|
46
|
-
* model can load again with the `view_image` tool.
|
|
46
|
+
* How many images the model keeps seeing, the latest ones; the others become notes the
|
|
47
|
+
* model can load again with the `view_image` tool. A `view_image` can keep more of the images
|
|
48
|
+
* it loaded, up to `maxViewedPerTurn`, until the next images are sent or loaded.
|
|
47
49
|
* Default is 5
|
|
48
50
|
*/
|
|
49
51
|
maxVisible?: number;
|
|
52
|
+
/**
|
|
53
|
+
* How many images the model can load again with the `view_image` tool during one answer: one
|
|
54
|
+
* call, whole messages, the most likely first. It sees all of them for that answer and keeps the
|
|
55
|
+
* ones it picks for the next messages; past the limit it asks the user which images matter.
|
|
56
|
+
* Default is 10
|
|
57
|
+
*/
|
|
58
|
+
maxViewedPerTurn?: number;
|
|
50
59
|
}
|
|
51
60
|
interface ICheckpointResource {
|
|
52
61
|
resourceId: string;
|