@aiscene/shared 8.0.3 → 8.0.5

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 (105) hide show
  1. package/dist/es/agent-tools/agent-behavior-init-args.mjs +44 -0
  2. package/dist/es/agent-tools/base-tools.mjs +163 -0
  3. package/dist/es/agent-tools/chrome-path.mjs +50 -0
  4. package/dist/es/agent-tools/cli-report-session.mjs +78 -0
  5. package/dist/es/agent-tools/error-formatter.mjs +106 -0
  6. package/dist/es/agent-tools/index.mjs +9 -0
  7. package/dist/es/agent-tools/init-arg-utils.mjs +38 -0
  8. package/dist/es/agent-tools/observation-artifact.mjs +5 -0
  9. package/dist/es/agent-tools/observation-record.mjs +248 -0
  10. package/dist/es/agent-tools/tool-defaults.mjs +54 -0
  11. package/dist/es/agent-tools/tool-generator.mjs +529 -0
  12. package/dist/es/agent-tools/types.mjs +3 -0
  13. package/dist/es/agent-tools/user-prompt.mjs +66 -0
  14. package/dist/es/cli/interrupt.mjs +116 -0
  15. package/dist/es/cli/record-command.mjs +130 -0
  16. package/dist/es/cli/screenshot-file.mjs +24 -0
  17. package/dist/es/cli/verbose-ai-act.mjs +230 -0
  18. package/dist/es/cli/verbose-screenshot.mjs +134 -0
  19. package/dist/es/cli/verbose.mjs +404 -0
  20. package/dist/es/env/parse-model-config.mjs +1 -1
  21. package/dist/es/env/types.mjs +18 -3
  22. package/dist/es/img/index.mjs +3 -3
  23. package/dist/es/img/info.mjs +45 -1
  24. package/dist/es/img/photon-loader.mjs +4 -0
  25. package/dist/es/img/transform.mjs +113 -3
  26. package/dist/es/recorder.mjs +249 -0
  27. package/dist/lib/agent-tools/agent-behavior-init-args.js +87 -0
  28. package/dist/lib/agent-tools/base-tools.js +197 -0
  29. package/dist/lib/agent-tools/chrome-path.js +87 -0
  30. package/dist/lib/agent-tools/cli-report-session.js +121 -0
  31. package/dist/lib/agent-tools/error-formatter.js +149 -0
  32. package/dist/lib/agent-tools/index.js +114 -0
  33. package/dist/lib/agent-tools/init-arg-utils.js +78 -0
  34. package/dist/lib/agent-tools/observation-artifact.js +42 -0
  35. package/dist/lib/agent-tools/observation-record.js +297 -0
  36. package/dist/lib/agent-tools/tool-defaults.js +97 -0
  37. package/dist/lib/agent-tools/tool-generator.js +569 -0
  38. package/dist/lib/agent-tools/types.js +40 -0
  39. package/dist/lib/agent-tools/user-prompt.js +103 -0
  40. package/dist/lib/cli/interrupt.js +156 -0
  41. package/dist/lib/cli/record-command.js +164 -0
  42. package/dist/lib/cli/screenshot-file.js +58 -0
  43. package/dist/lib/cli/verbose-ai-act.js +273 -0
  44. package/dist/lib/cli/verbose-screenshot.js +177 -0
  45. package/dist/lib/cli/verbose.js +465 -0
  46. package/dist/lib/env/parse-model-config.js +1 -1
  47. package/dist/lib/env/types.js +32 -2
  48. package/dist/lib/img/index.js +29 -5
  49. package/dist/lib/img/info.js +48 -1
  50. package/dist/lib/img/photon-loader.js +38 -0
  51. package/dist/lib/img/transform.js +135 -4
  52. package/dist/lib/recorder.js +307 -0
  53. package/dist/types/agent-tools/agent-behavior-init-args.d.ts +17 -0
  54. package/dist/types/agent-tools/base-tools.d.ts +158 -0
  55. package/dist/types/agent-tools/chrome-path.d.ts +2 -0
  56. package/dist/types/agent-tools/cli-report-session.d.ts +12 -0
  57. package/dist/types/agent-tools/error-formatter.d.ts +30 -0
  58. package/dist/types/agent-tools/index.d.ts +9 -0
  59. package/dist/types/agent-tools/init-arg-utils.d.ts +13 -0
  60. package/dist/types/agent-tools/observation-artifact.d.ts +10 -0
  61. package/dist/types/agent-tools/observation-record.d.ts +38 -0
  62. package/dist/types/agent-tools/tool-defaults.d.ts +63 -0
  63. package/dist/types/agent-tools/tool-generator.d.ts +13 -0
  64. package/dist/types/agent-tools/types.d.ts +213 -0
  65. package/dist/types/agent-tools/user-prompt.d.ts +13 -0
  66. package/dist/types/cli/interrupt.d.ts +49 -0
  67. package/dist/types/cli/record-command.d.ts +3 -0
  68. package/dist/types/cli/screenshot-file.d.ts +10 -0
  69. package/dist/types/cli/verbose-ai-act.d.ts +44 -0
  70. package/dist/types/cli/verbose-screenshot.d.ts +10 -0
  71. package/dist/types/cli/verbose.d.ts +40 -0
  72. package/dist/types/env/types.d.ts +16 -7
  73. package/dist/types/img/index.d.ts +2 -2
  74. package/dist/types/img/info.d.ts +2 -0
  75. package/dist/types/img/photon-loader.d.ts +2 -0
  76. package/dist/types/img/transform.d.ts +22 -2
  77. package/dist/types/mcp/types.d.ts +1 -0
  78. package/dist/types/recorder.d.ts +113 -0
  79. package/package.json +1 -1
  80. package/src/agent-tools/agent-behavior-init-args.ts +109 -0
  81. package/src/agent-tools/base-tools.ts +399 -0
  82. package/src/agent-tools/chrome-path.ts +74 -0
  83. package/src/agent-tools/cli-report-session.ts +130 -0
  84. package/src/agent-tools/error-formatter.ts +177 -0
  85. package/src/agent-tools/index.ts +9 -0
  86. package/src/agent-tools/init-arg-utils.ts +105 -0
  87. package/src/agent-tools/observation-artifact.ts +29 -0
  88. package/src/agent-tools/observation-record.ts +331 -0
  89. package/src/agent-tools/tool-defaults.ts +119 -0
  90. package/src/agent-tools/tool-generator.ts +866 -0
  91. package/src/agent-tools/types.ts +250 -0
  92. package/src/agent-tools/user-prompt.ts +102 -0
  93. package/src/cli/interrupt.ts +207 -0
  94. package/src/cli/record-command.ts +177 -0
  95. package/src/cli/screenshot-file.ts +61 -0
  96. package/src/cli/verbose-ai-act.ts +387 -0
  97. package/src/cli/verbose-screenshot.ts +269 -0
  98. package/src/cli/verbose.ts +753 -0
  99. package/src/env/types.ts +30 -2
  100. package/src/img/index.ts +12 -0
  101. package/src/img/info.ts +61 -0
  102. package/src/img/photon-loader.ts +5 -0
  103. package/src/img/transform.ts +262 -3
  104. package/src/mcp/types.ts +2 -0
  105. package/src/recorder.ts +625 -0
@@ -0,0 +1,269 @@
1
+ import { basename, dirname, isAbsolute, join, relative } from 'node:path';
2
+ import { writeCliScreenshotFile } from './screenshot-file';
3
+
4
+ export type CliVerboseScreenshotExportMode = 'none' | 'tmp' | 'report';
5
+
6
+ export interface CliVerboseScreenshotCollectOptions {
7
+ reportFile?: unknown;
8
+ exportMode?: CliVerboseScreenshotExportMode;
9
+ cache?: Map<string, string>;
10
+ }
11
+
12
+ interface CliVerboseScreenshotRefLike {
13
+ type: 'midscene_screenshot_ref';
14
+ id?: unknown;
15
+ mimeType?: unknown;
16
+ storage?: unknown;
17
+ path?: string;
18
+ }
19
+
20
+ function isRecord(value: unknown): value is Record<string, unknown> {
21
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
22
+ }
23
+
24
+ function isCliVerboseScreenshotRefLike(
25
+ value: unknown,
26
+ ): value is CliVerboseScreenshotRefLike {
27
+ return isRecord(value) && value.type === 'midscene_screenshot_ref';
28
+ }
29
+
30
+ function toSerializableScreenshot(
31
+ value: unknown,
32
+ ): CliVerboseScreenshotRefLike | null {
33
+ if (!value || typeof value !== 'object') {
34
+ return null;
35
+ }
36
+
37
+ const maybeSerializable = value as {
38
+ toSerializable?: () => unknown;
39
+ };
40
+ if (typeof maybeSerializable.toSerializable === 'function') {
41
+ try {
42
+ const serialized = maybeSerializable.toSerializable();
43
+ return isCliVerboseScreenshotRefLike(serialized) ? serialized : null;
44
+ } catch {
45
+ return null;
46
+ }
47
+ }
48
+
49
+ return isCliVerboseScreenshotRefLike(value) ? value : null;
50
+ }
51
+
52
+ function getStringProperty(value: unknown, key: string): string | undefined {
53
+ if (!isRecord(value)) {
54
+ return undefined;
55
+ }
56
+
57
+ try {
58
+ const property = value[key];
59
+ return typeof property === 'string' && property.length > 0
60
+ ? property
61
+ : undefined;
62
+ } catch {
63
+ return undefined;
64
+ }
65
+ }
66
+
67
+ function screenshotRawBase64(value: unknown): string | undefined {
68
+ const rawBase64 = getStringProperty(value, 'rawBase64');
69
+ if (rawBase64) {
70
+ return rawBase64;
71
+ }
72
+
73
+ const base64 = getStringProperty(value, 'base64');
74
+ const match = base64?.match(/^data:image\/(?:png|jpeg|jpg);base64,(.+)$/);
75
+ return match?.[1];
76
+ }
77
+
78
+ function inlineScreenshotCacheKey(
79
+ rawBase64: string,
80
+ serialized: CliVerboseScreenshotRefLike,
81
+ directoryPath: string | undefined,
82
+ directoryName: string | undefined,
83
+ extension: string | undefined,
84
+ ): string {
85
+ return JSON.stringify([
86
+ serialized.id,
87
+ serialized.mimeType,
88
+ directoryPath,
89
+ directoryName,
90
+ extension,
91
+ rawBase64,
92
+ ]);
93
+ }
94
+
95
+ function exportInlineScreenshotForVerbose(
96
+ value: unknown,
97
+ serialized: CliVerboseScreenshotRefLike,
98
+ options: CliVerboseScreenshotCollectOptions,
99
+ ): string | undefined {
100
+ if (typeof serialized.path === 'string') {
101
+ return serialized.path;
102
+ }
103
+
104
+ const exportMode = options.exportMode ?? 'tmp';
105
+ if (exportMode === 'none') {
106
+ return undefined;
107
+ }
108
+
109
+ const rawBase64 = screenshotRawBase64(value);
110
+ if (!rawBase64) {
111
+ return undefined;
112
+ }
113
+
114
+ const directoryPath =
115
+ exportMode === 'report' &&
116
+ typeof options.reportFile === 'string' &&
117
+ options.reportFile.length > 0
118
+ ? join(dirname(options.reportFile), 'screenshots')
119
+ : undefined;
120
+ const directoryName = directoryPath ? undefined : 'midscene-cli-screenshots';
121
+ const extension = getStringProperty(value, 'extension');
122
+ const cacheKey = inlineScreenshotCacheKey(
123
+ rawBase64,
124
+ serialized,
125
+ directoryPath,
126
+ directoryName,
127
+ extension,
128
+ );
129
+ const cachedPath = options.cache?.get(cacheKey);
130
+ if (cachedPath) {
131
+ return cachedPath;
132
+ }
133
+
134
+ try {
135
+ const path = writeCliScreenshotFile(rawBase64, {
136
+ id: serialized.id,
137
+ mimeType: serialized.mimeType,
138
+ extension,
139
+ ...(directoryPath ? { directoryPath } : { directoryName }),
140
+ overwrite: false,
141
+ });
142
+ options.cache?.set(cacheKey, path);
143
+ return path;
144
+ } catch {
145
+ return undefined;
146
+ }
147
+ }
148
+
149
+ export function collectScreenshotRefs(
150
+ value: unknown,
151
+ options: CliVerboseScreenshotCollectOptions = {},
152
+ ): Array<Record<string, unknown>> {
153
+ const screenshots: Array<Record<string, unknown>> = [];
154
+ const visit = (candidate: unknown, timing?: unknown): void => {
155
+ if (Array.isArray(candidate)) {
156
+ for (const item of candidate) {
157
+ visit(item, timing);
158
+ }
159
+ return;
160
+ }
161
+
162
+ const serialized = toSerializableScreenshot(candidate);
163
+ if (serialized?.type === 'midscene_screenshot_ref') {
164
+ const screenshot: Record<string, unknown> = {
165
+ id: serialized.id,
166
+ storage: serialized.storage,
167
+ };
168
+ const exportedPath = exportInlineScreenshotForVerbose(
169
+ candidate,
170
+ serialized,
171
+ options,
172
+ );
173
+ if (exportedPath) {
174
+ screenshot.path = exportedPath;
175
+ screenshot.file = basename(exportedPath);
176
+ }
177
+ if (typeof timing === 'string') {
178
+ screenshot.timing = timing;
179
+ }
180
+ screenshots.push(screenshot);
181
+ return;
182
+ }
183
+
184
+ if (!isRecord(candidate)) {
185
+ return;
186
+ }
187
+
188
+ const screenshotRecord = candidate.screenshot;
189
+ if (screenshotRecord) {
190
+ visit(screenshotRecord, candidate.timing);
191
+ }
192
+ if (candidate.recorder) {
193
+ visit(candidate.recorder);
194
+ }
195
+ if (isRecord(candidate.uiContext)) {
196
+ visit(candidate.uiContext.screenshot);
197
+ }
198
+ };
199
+
200
+ visit(value);
201
+ return screenshots;
202
+ }
203
+
204
+ export function pathForReportScreenshot(
205
+ path: string,
206
+ reportFile?: unknown,
207
+ ): string {
208
+ const resolvedPath =
209
+ typeof reportFile === 'string' &&
210
+ reportFile.length > 0 &&
211
+ (path.startsWith('./') || path.startsWith('../'))
212
+ ? join(dirname(reportFile), path)
213
+ : path;
214
+
215
+ if (!isAbsolute(resolvedPath)) {
216
+ return resolvedPath;
217
+ }
218
+
219
+ const relativePath = relative(process.cwd(), resolvedPath);
220
+ if (
221
+ relativePath &&
222
+ !relativePath.startsWith('..') &&
223
+ !isAbsolute(relativePath)
224
+ ) {
225
+ return relativePath;
226
+ }
227
+
228
+ return resolvedPath;
229
+ }
230
+
231
+ export function latestScreenshotPathForAiAct(
232
+ value: unknown,
233
+ options: CliVerboseScreenshotCollectOptions = {},
234
+ ): string {
235
+ const screenshot = collectScreenshotRefs(value, {
236
+ ...options,
237
+ exportMode: options.exportMode ?? 'report',
238
+ })
239
+ .slice()
240
+ .reverse()
241
+ .find((item) => typeof item.path === 'string' && item.path.length > 0);
242
+ const path = typeof screenshot?.path === 'string' ? screenshot.path : '';
243
+ return path ? pathForReportScreenshot(path, options.reportFile) : '';
244
+ }
245
+
246
+ export function renderScreenshotList(screenshots: unknown): string {
247
+ if (!Array.isArray(screenshots) || screenshots.length === 0) {
248
+ return '';
249
+ }
250
+
251
+ return screenshots
252
+ .map((item) => {
253
+ if (!isRecord(item)) {
254
+ return '';
255
+ }
256
+ const path =
257
+ typeof item.path === 'string'
258
+ ? item.path
259
+ : typeof item.file === 'string'
260
+ ? item.file
261
+ : typeof item.id === 'string'
262
+ ? item.id
263
+ : '';
264
+ const timing = typeof item.timing === 'string' ? item.timing : '';
265
+ return [timing, path].filter(Boolean).join(' ');
266
+ })
267
+ .filter(Boolean)
268
+ .join(', ');
269
+ }