@midscene/core 1.12.3 → 1.12.4-beta-20260902093735.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.
Files changed (61) hide show
  1. package/dist/es/agent/agent.mjs +88 -25
  2. package/dist/es/agent/agent.mjs.map +1 -1
  3. package/dist/es/agent/index.mjs +2 -1
  4. package/dist/es/agent/insight.mjs +40 -10
  5. package/dist/es/agent/insight.mjs.map +1 -1
  6. package/dist/es/agent/prompt-context.mjs +42 -6
  7. package/dist/es/agent/prompt-context.mjs.map +1 -1
  8. package/dist/es/agent/run-gherkin-scenario.mjs.map +1 -1
  9. package/dist/es/agent/utils.mjs +1 -1
  10. package/dist/es/ai-model/prompt/insight/user-prompt.mjs +1 -1
  11. package/dist/es/ai-model/prompt/insight/user-prompt.mjs.map +1 -1
  12. package/dist/es/ai-model/workflows/insight/extraction.mjs +4 -1
  13. package/dist/es/ai-model/workflows/insight/extraction.mjs.map +1 -1
  14. package/dist/es/ai-model/workflows/planning/custom-planning.mjs +3 -3
  15. package/dist/es/ai-model/workflows/planning/custom-planning.mjs.map +1 -1
  16. package/dist/es/ai-model/workflows/planning/standard-planning.mjs +1 -1
  17. package/dist/es/ai-model/workflows/planning/standard-planning.mjs.map +1 -1
  18. package/dist/es/index.mjs +2 -2
  19. package/dist/es/index.mjs.map +1 -1
  20. package/dist/es/report-html-template.mjs +1 -1
  21. package/dist/es/types.mjs.map +1 -1
  22. package/dist/es/utils.mjs +1 -1
  23. package/dist/es/yaml/player.mjs +0 -5
  24. package/dist/es/yaml/player.mjs.map +1 -1
  25. package/dist/es/yaml/utils.mjs +4 -3
  26. package/dist/es/yaml/utils.mjs.map +1 -1
  27. package/dist/lib/agent/agent.js +87 -24
  28. package/dist/lib/agent/agent.js.map +1 -1
  29. package/dist/lib/agent/index.js +5 -1
  30. package/dist/lib/agent/insight.js +40 -10
  31. package/dist/lib/agent/insight.js.map +1 -1
  32. package/dist/lib/agent/prompt-context.js +55 -7
  33. package/dist/lib/agent/prompt-context.js.map +1 -1
  34. package/dist/lib/agent/run-gherkin-scenario.js.map +1 -1
  35. package/dist/lib/agent/utils.js +1 -1
  36. package/dist/lib/ai-model/prompt/insight/user-prompt.js +1 -1
  37. package/dist/lib/ai-model/prompt/insight/user-prompt.js.map +1 -1
  38. package/dist/lib/ai-model/workflows/insight/extraction.js +4 -1
  39. package/dist/lib/ai-model/workflows/insight/extraction.js.map +1 -1
  40. package/dist/lib/ai-model/workflows/planning/custom-planning.js +3 -3
  41. package/dist/lib/ai-model/workflows/planning/custom-planning.js.map +1 -1
  42. package/dist/lib/ai-model/workflows/planning/standard-planning.js +1 -1
  43. package/dist/lib/ai-model/workflows/planning/standard-planning.js.map +1 -1
  44. package/dist/lib/index.js +4 -1
  45. package/dist/lib/index.js.map +1 -1
  46. package/dist/lib/report-html-template.js +1 -1
  47. package/dist/lib/types.js.map +1 -1
  48. package/dist/lib/utils.js +1 -1
  49. package/dist/lib/yaml/player.js +0 -5
  50. package/dist/lib/yaml/player.js.map +1 -1
  51. package/dist/lib/yaml/utils.js +3 -2
  52. package/dist/lib/yaml/utils.js.map +1 -1
  53. package/dist/types/agent/agent.d.ts +22 -3
  54. package/dist/types/agent/index.d.ts +2 -1
  55. package/dist/types/agent/insight.d.ts +6 -1
  56. package/dist/types/agent/prompt-context.d.ts +27 -1
  57. package/dist/types/agent/run-gherkin-scenario.d.ts +1 -0
  58. package/dist/types/index.d.ts +1 -1
  59. package/dist/types/types.d.ts +26 -0
  60. package/dist/types/yaml.d.ts +12 -4
  61. package/package.json +2 -2
@@ -1,6 +1,6 @@
1
1
  import type { TUserPrompt } from '../ai-model/index';
2
2
  import Service from '../service/index';
3
- import { type ActionParam, type ActionReturn, type AgentAssertResult, type AgentOpt, type AgentProgressListener, type AgentWaitForOpt, type AiActEffort, type AssertOptions, type DeepThinkOption, type DeviceAction, ExecutionDump, type InsightAPI, type LocateOption, type LocateResultElement, type OnTaskStartTip, type QueryOptions, type RecordToReportOptions, ReportActionDump, type ScrollParam, type ServiceAction, type ServiceExtractParam, type UIContext } from '../types';
3
+ import { type ActionParam, type ActionReturn, type AgentAssertResult, type AgentContextKey, type AgentOpt, type AgentProgressListener, type AgentWaitForOpt, type AiActEffort, type AssertOptions, type DeepThinkOption, type DeviceAction, ExecutionDump, type InsightAPI, type LocateOption, type LocateResultElement, type OnTaskStartTip, type QueryOptions, type RecordToReportOptions, ReportActionDump, type ScrollParam, type ServiceAction, type ServiceExtractParam, type UIContext } from '../types';
4
4
  import type { AbstractInterface, InputStrategy } from '../device';
5
5
  import type { TaskRunner } from '../task-runner';
6
6
  import { ModelConfigManager } from '@midscene/shared/env';
@@ -17,6 +17,11 @@ export type AiActOptions = {
17
17
  deepThink?: DeepThinkOption;
18
18
  deepLocate?: boolean;
19
19
  abortSignal?: AbortSignal;
20
+ /**
21
+ * Additional facts, rules, constraints, or output requirements for this AI
22
+ * call. It overrides `contexts.aiAct` and `contexts.default`; `''` disables
23
+ * inherited user context for this call.
24
+ */
20
25
  context?: string;
21
26
  };
22
27
  /**
@@ -63,7 +68,10 @@ export declare class Agent<InterfaceType extends AbstractInterface = AbstractInt
63
68
  private activeObserver;
64
69
  /** Observers own temporary frame files until their observation is disposed. */
65
70
  private ownedObservers;
66
- private get aiActContext();
71
+ private get contexts();
72
+ private resolveUserContext;
73
+ private resolveContext;
74
+ private withContext;
67
75
  private executionDumpIndexByRunner;
68
76
  private fullActionSpace;
69
77
  private activeFileChooserAccepter?;
@@ -123,10 +131,21 @@ export declare class Agent<InterfaceType extends AbstractInterface = AbstractInt
123
131
  */
124
132
  startObserving(opt?: UIObserverOption): Promise<UIObserver>;
125
133
  /**
126
- * @deprecated Use {@link setAIActContext} instead.
134
+ * @deprecated Use `setContext('aiAct', context)` instead.
127
135
  */
128
136
  setAIActionContext(prompt: string): Promise<void>;
137
+ /**
138
+ * @deprecated Use `setContext('aiAct', context)` instead.
139
+ */
129
140
  setAIActContext(prompt: string): Promise<void>;
141
+ /**
142
+ * Set Agent-level AI guidance. Use `default` as the shared fallback for all
143
+ * AI-powered APIs, or an API name to override that fallback for the API.
144
+ * API-specific values are not implicitly concatenated with `default`.
145
+ * Passing `undefined` removes an API override; passing `''` keeps an explicit
146
+ * empty value and therefore prevents that API from using `default`.
147
+ */
148
+ setContext(target: AgentContextKey, context: string | undefined): void;
130
149
  resetDump(): ReportActionDump;
131
150
  appendExecutionDump(execution: ExecutionDump, runner?: TaskRunner): void;
132
151
  /**
@@ -8,6 +8,7 @@ export { cacheFileExt } from './task-cache';
8
8
  export { TaskExecutor } from './tasks';
9
9
  export type { MidsceneUsageMetrics, UsageBucket } from './metrics';
10
10
  export type { GherkinStepKeyword, RunGherkinScenarioOptions, } from './run-gherkin-scenario';
11
- export type { AgentOpt } from '../types';
11
+ export type { AgentContextKey, AgentContexts, AgentOpt, AiApiName, } from '../types';
12
12
  export type { RecordToReportOptions, RecordToReportScreenshot } from '../types';
13
13
  export type { AiActOptions } from './agent';
14
+ export { mergeAIContexts } from './prompt-context';
@@ -1,6 +1,8 @@
1
1
  import type { TUserPrompt } from '../ai-model/index';
2
2
  import type { ModelRuntime } from '../ai-model/models';
3
3
  import type { AgentAssertResult, AssertOptions, InsightAPI, QueryOptions, ServiceExtractParam, UIContext } from '../types';
4
+ import type { AiApiName } from '@midscene/shared/agent-tools/agent-context';
5
+ import { type ResolvedAIContext } from './prompt-context';
4
6
  import { type TaskExecutor } from './tasks';
5
7
  type InsightTaskExecutor = Pick<TaskExecutor, 'createTypeQueryExecution'>;
6
8
  /** Execute read-only AI operations against either live or fixed UI context. */
@@ -8,7 +10,10 @@ export declare class Insight implements InsightAPI {
8
10
  private readonly taskExecutor;
9
11
  private readonly resolveModelRuntime;
10
12
  private readonly getUIContext?;
11
- constructor(taskExecutor: InsightTaskExecutor, resolveModelRuntime: () => ModelRuntime, getUIContext?: (() => UIContext) | undefined);
13
+ private readonly resolveAgentContext?;
14
+ constructor(taskExecutor: InsightTaskExecutor, resolveModelRuntime: () => ModelRuntime, getUIContext?: (() => UIContext) | undefined, resolveAgentContext?: ((apiName: AiApiName, callContext?: string) => ResolvedAIContext | undefined) | undefined);
15
+ private resolveContext;
16
+ private withContext;
12
17
  private executionOptions;
13
18
  aiQuery<ReturnType = any>(demand: ServiceExtractParam, options?: QueryOptions): Promise<ReturnType>;
14
19
  aiBoolean(prompt: TUserPrompt, options?: QueryOptions): Promise<boolean>;
@@ -1,3 +1,29 @@
1
1
  import type { TUserPrompt } from '../ai-model';
2
+ import type { AiApiName } from '@midscene/shared/agent-tools/agent-context';
3
+ export type AIContextMetadata = {
4
+ source: 'default';
5
+ } | {
6
+ source: 'api';
7
+ apiName: AiApiName;
8
+ } | {
9
+ source: 'call';
10
+ };
11
+ export interface ResolvedAIContext {
12
+ value: string;
13
+ metadata: AIContextMetadata;
14
+ }
15
+ export declare const INTERNAL_AI_CONTEXT_METADATA_KEY: "_internalAIContextMetadata";
16
+ export interface InternalAIContextOptions {
17
+ [INTERNAL_AI_CONTEXT_METADATA_KEY]?: AIContextMetadata;
18
+ /** Framework-owned workflow history appended after the selected context. */
19
+ _internalAdditionalContext?: string;
20
+ }
21
+ export declare const resolvedAIContextFromOptions: (options: ({
22
+ context?: string;
23
+ } & Partial<InternalAIContextOptions>) | undefined) => ResolvedAIContext | undefined;
24
+ /** Render the selected user context and framework history as separate blocks. */
25
+ export declare const renderAIContext: (resolvedContext: ResolvedAIContext | undefined, workflowHistory?: string) => string | undefined;
26
+ /** Explicitly concatenate context fragments in the order provided. */
27
+ export declare const mergeAIContexts: (...contexts: Array<string | undefined>) => string | undefined;
2
28
  export declare const buildPromptWithContext: (prompt: TUserPrompt, context: string | undefined) => TUserPrompt;
3
- export declare const buildLocatePromptWithContext: (prompt: TUserPrompt, context: string | undefined) => TUserPrompt;
29
+ export declare const buildLocatePromptWithContext: (prompt: TUserPrompt, context: string | undefined, metadata?: AIContextMetadata) => TUserPrompt;
@@ -1,6 +1,7 @@
1
1
  import type { AiActOptions } from './agent';
2
2
  export type GherkinStepKeyword = 'Given' | 'When' | 'Then' | 'And' | 'But';
3
3
  export type RunGherkinScenarioOptions = AiActOptions & {
4
+ /** Additional AI guidance applied to every step in this scenario run. */
4
5
  context?: string;
5
6
  };
6
7
  type GherkinPrimaryKeyword = 'Given' | 'When' | 'Then';
@@ -10,7 +10,7 @@ export { z };
10
10
  export default Service;
11
11
  export { TaskRunner, Service, getVersion };
12
12
  export type { MidsceneYamlScript, MidsceneYamlTask, MidsceneYamlFlowItem, MidsceneYamlConfigResult, MidsceneYamlConfig, MidsceneYamlScriptWebEnv, MidsceneYamlScriptAndroidEnv, MidsceneYamlScriptIOSEnv, MidsceneYamlScriptEnv, LocateOption, DetailedLocateParam, } from './yaml';
13
- export { Agent, type AgentOpt, type AiActOptions, type GherkinStepKeyword, type MidsceneUsageMetrics, type RunGherkinScenarioOptions, type UsageBucket, createAgent, type UIObservation, type UIObserver, type UIObserverOption, } from './agent';
13
+ export { Agent, mergeAIContexts, type AgentOpt, type AiActOptions, type GherkinStepKeyword, type MidsceneUsageMetrics, type RunGherkinScenarioOptions, type UsageBucket, createAgent, type UIObservation, type UIObserver, type UIObserverOption, } from './agent';
14
14
  export { describeElementAtPoint, verifyElementDescriptionAtPoint, verifyLocator, type DescribeElementAtPointOptions, type DescribeElementCoordinateSpace, type ElementDescriberRuntime, type VerifyElementDescriptionAtPointOptions, } from './element-describer';
15
15
  export { createInlineImageResolver, restoreImageReferences, restoreReportImageReferences, escapeContent, unescapeContent, parseImageScripts, parseDumpScript, parseDumpScriptAttributes, generateImageScriptTag, generateDumpScriptTag, deriveTaskStatus, deriveCaseStatus, } from './dump';
16
16
  export type { TaskStatusFields, DerivedTaskStatus, RestoredScreenshotReference, StoredImageReferenceResolver, } from './dump';
@@ -1,3 +1,4 @@
1
+ import type { AgentContexts } from '@midscene/shared/agent-tools/agent-context';
1
2
  import type { SerializedError } from '@midscene/shared/agent-tools/error-formatter';
2
3
  import type { NodeType } from '@midscene/shared/constants';
3
4
  import type { CreateOpenAIClientFn, TModelConfig } from '@midscene/shared/env';
@@ -8,6 +9,7 @@ import type { ScreenshotItem } from './screenshot-item';
8
9
  import type { DetailedLocateParam, MidsceneYamlFlowItem, ServiceExtractOption } from './yaml';
9
10
  export type { ElementTreeNode, BaseElement, Rect, Size, Point, } from '@midscene/shared/types';
10
11
  export type { SerializedError } from '@midscene/shared/agent-tools/error-formatter';
12
+ export type { AgentContextKey, AgentContexts, AiApiName, } from '@midscene/shared/agent-tools/agent-context';
11
13
  export * from './yaml';
12
14
  export { ServiceError } from './errors';
13
15
  export { ExecutionDump, ReportActionDump, GroupedActionDump, } from './dump/report-action-dump';
@@ -228,6 +230,11 @@ export interface AgentWaitForOpt extends ServiceExtractOption {
228
230
  }
229
231
  export interface AgentAssertOpt {
230
232
  keepRawResponse?: boolean;
233
+ /**
234
+ * Additional facts, decision rules, constraints, or output requirements for
235
+ * this assertion. It overrides `contexts.aiAssert` and `contexts.default`;
236
+ * `''` disables inherited user context for this call.
237
+ */
231
238
  context?: string;
232
239
  abortSignal?: AbortSignal;
233
240
  }
@@ -678,7 +685,26 @@ export interface AgentOpt {
678
685
  */
679
686
  outputFormat?: 'single-html' | 'html-and-external-assets';
680
687
  onTaskStartTip?: OnTaskStartTip;
688
+ /**
689
+ * Agent-level AI guidance containing business facts, rules, constraints, or
690
+ * output requirements. `contexts.default` is a shared fallback used only
691
+ * when neither the call nor the matching API key provides a context.
692
+ *
693
+ * A per-call `options.context` overrides the matching API context, which
694
+ * overrides `contexts.default`. An empty string explicitly clears inherited
695
+ * user context for that scope. These layers are not implicitly concatenated.
696
+ */
697
+ contexts?: AgentContexts;
698
+ /**
699
+ * Compatibility alias for `contexts.aiAct`. `contexts.aiAct` takes
700
+ * precedence when both are provided.
701
+ * @deprecated Use `contexts.aiAct` instead.
702
+ */
681
703
  aiActContext?: string;
704
+ /**
705
+ * Older compatibility alias for `contexts.aiAct`.
706
+ * @deprecated Use `contexts.aiAct` instead.
707
+ */
682
708
  aiActionContext?: string;
683
709
  reportFileName?: string;
684
710
  reportAttributes?: ReportAttributes;
@@ -4,7 +4,11 @@ import type { AgentOpt, LocateResultElement } from './types';
4
4
  import type { UIContext } from './types';
5
5
  export interface LocateOption extends Partial<TMultimodalPrompt> {
6
6
  prompt?: TUserPrompt;
7
- /** Additional context for this AI request. */
7
+ /**
8
+ * Additional facts, rules, or constraints for this AI call. It overrides
9
+ * the matching API context and `contexts.default`; `''` disables inherited
10
+ * user context for this call.
11
+ */
8
12
  context?: string;
9
13
  deepLocate?: boolean;
10
14
  /** @deprecated Use `deepLocate` instead. Kept for backward compatibility. */
@@ -15,7 +19,11 @@ export interface LocateOption extends Partial<TMultimodalPrompt> {
15
19
  fileChooserAccept?: string | string[];
16
20
  }
17
21
  export interface ServiceExtractOption {
18
- /** Additional context for this AI request. */
22
+ /**
23
+ * Additional facts, decision rules, constraints, or output requirements for
24
+ * this AI call. It overrides the matching API context and
25
+ * `contexts.default`; `''` disables inherited user context for this call.
26
+ */
19
27
  context?: string;
20
28
  domIncluded?: boolean | 'visible-only';
21
29
  screenshotIncluded?: boolean;
@@ -25,7 +33,7 @@ export interface DetailedLocateParam extends Omit<LocateOption, 'context' | 'dee
25
33
  prompt: TUserPrompt;
26
34
  /** Original prompt text used for user-facing reports. */
27
35
  promptDisplay?: string;
28
- /** Per-call business context used for user-facing reports. */
36
+ /** Per-call AI guidance recorded in user-facing reports. */
29
37
  context?: string;
30
38
  }
31
39
  export type ScrollType = 'singleAction' | 'scrollToBottom' | 'scrollToTop' | 'scrollToRight' | 'scrollToLeft' | 'once' | 'untilBottom' | 'untilTop' | 'untilRight' | 'untilLeft';
@@ -78,7 +86,7 @@ export interface MidsceneYamlTask {
78
86
  * strategy: "read-write"
79
87
  * ```
80
88
  */
81
- export type MidsceneYamlScriptAgentOpt = Pick<AgentOpt, 'testId' | 'groupName' | 'groupDescription' | 'generateReport' | 'persistExecutionDump' | 'autoPrintReportMsg' | 'reportFileName' | 'replanningCycleLimit' | 'aiActContext' | 'aiActionContext' | 'cache' | 'screenshotShrinkFactor'>;
89
+ export type MidsceneYamlScriptAgentOpt = Pick<AgentOpt, 'testId' | 'groupName' | 'groupDescription' | 'generateReport' | 'persistExecutionDump' | 'autoPrintReportMsg' | 'reportFileName' | 'replanningCycleLimit' | 'contexts' | 'aiActContext' | 'aiActionContext' | 'cache' | 'screenshotShrinkFactor'>;
82
90
  export interface MidsceneYamlScriptConfig {
83
91
  output?: string;
84
92
  unstableLogContent?: boolean | string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midscene/core",
3
3
  "description": "Automate browser actions, extract data, and perform assertions using AI. It offers JavaScript SDK, Chrome extension, and support for scripting in YAML. See https://midscenejs.com/ for details.",
4
- "version": "1.12.3",
4
+ "version": "1.12.4-beta-20260902093735.0",
5
5
  "repository": "https://github.com/web-infra-dev/midscene",
6
6
  "homepage": "https://midscenejs.com/",
7
7
  "main": "./dist/lib/index.js",
@@ -153,7 +153,7 @@
153
153
  "semver": "7.5.2",
154
154
  "undici": "^6.0.0",
155
155
  "zod": "^3.25.1",
156
- "@midscene/shared": "1.12.3"
156
+ "@midscene/shared": "1.12.4-beta-20260902093735.0"
157
157
  },
158
158
  "devDependencies": {
159
159
  "@rslib/core": "^0.18.3",