@ddwang/magnitude-core 0.3.1-ddwang.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.
Files changed (142) hide show
  1. package/dist/actions/desktopActions.d.ts +58 -0
  2. package/dist/actions/desktopActions.js +154 -0
  3. package/dist/actions/index.d.ts +26 -0
  4. package/dist/actions/index.js +11 -0
  5. package/dist/actions/taskActions.d.ts +8 -0
  6. package/dist/actions/taskActions.js +27 -0
  7. package/dist/actions/types.d.ts +31 -0
  8. package/dist/actions/types.js +5 -0
  9. package/dist/actions/util.d.ts +20 -0
  10. package/dist/actions/util.js +297 -0
  11. package/dist/actions/webActions.d.ts +79 -0
  12. package/dist/actions/webActions.js +190 -0
  13. package/dist/agent/browserAgent.d.ts +29 -0
  14. package/dist/agent/browserAgent.js +122 -0
  15. package/dist/agent/errors.d.ts +8 -0
  16. package/dist/agent/errors.js +68 -0
  17. package/dist/agent/index.d.ts +53 -0
  18. package/dist/agent/index.js +377 -0
  19. package/dist/agent/narrator.d.ts +4 -0
  20. package/dist/agent/narrator.js +63 -0
  21. package/dist/agent/pause.test.d.ts +1 -0
  22. package/dist/agent/pause.test.js +194 -0
  23. package/dist/ai/baml_client/async_client.d.ts +73 -0
  24. package/dist/ai/baml_client/async_client.js +152 -0
  25. package/dist/ai/baml_client/async_request.d.ts +35 -0
  26. package/dist/ai/baml_client/async_request.js +98 -0
  27. package/dist/ai/baml_client/config.d.ts +11 -0
  28. package/dist/ai/baml_client/config.js +20 -0
  29. package/dist/ai/baml_client/globals.d.ts +16 -0
  30. package/dist/ai/baml_client/globals.js +37 -0
  31. package/dist/ai/baml_client/index.d.ts +16 -0
  32. package/dist/ai/baml_client/index.js +41 -0
  33. package/dist/ai/baml_client/inlinedbaml.d.ts +19 -0
  34. package/dist/ai/baml_client/inlinedbaml.js +31 -0
  35. package/dist/ai/baml_client/parser.d.ts +53 -0
  36. package/dist/ai/baml_client/parser.js +86 -0
  37. package/dist/ai/baml_client/partial_types.d.ts +61 -0
  38. package/dist/ai/baml_client/partial_types.js +10 -0
  39. package/dist/ai/baml_client/sync_client.d.ts +50 -0
  40. package/dist/ai/baml_client/sync_client.js +100 -0
  41. package/dist/ai/baml_client/sync_request.d.ts +35 -0
  42. package/dist/ai/baml_client/sync_request.js +98 -0
  43. package/dist/ai/baml_client/tracing.d.ts +16 -0
  44. package/dist/ai/baml_client/tracing.js +18 -0
  45. package/dist/ai/baml_client/type_builder.d.ts +38 -0
  46. package/dist/ai/baml_client/type_builder.js +96 -0
  47. package/dist/ai/baml_client/types.d.ts +69 -0
  48. package/dist/ai/baml_client/types.js +15 -0
  49. package/dist/ai/claudeCode.d.ts +1 -0
  50. package/dist/ai/claudeCode.js +186 -0
  51. package/dist/ai/modelHarness.d.ts +39 -0
  52. package/dist/ai/modelHarness.js +189 -0
  53. package/dist/ai/multiModelHarness.d.ts +27 -0
  54. package/dist/ai/multiModelHarness.js +52 -0
  55. package/dist/ai/types.d.ts +90 -0
  56. package/dist/ai/types.js +5 -0
  57. package/dist/ai/util.d.ts +13 -0
  58. package/dist/ai/util.js +165 -0
  59. package/dist/cli.d.ts +0 -0
  60. package/dist/cli.js +3 -0
  61. package/dist/common/actions.d.ts +0 -0
  62. package/dist/common/actions.js +14 -0
  63. package/dist/common/events.d.ts +15 -0
  64. package/dist/common/events.js +3 -0
  65. package/dist/common/failure.d.ts +68 -0
  66. package/dist/common/failure.js +1 -0
  67. package/dist/common/index.d.ts +3 -0
  68. package/dist/common/index.js +3 -0
  69. package/dist/common/retry.d.ts +16 -0
  70. package/dist/common/retry.js +30 -0
  71. package/dist/common/util.d.ts +23 -0
  72. package/dist/common/util.js +132 -0
  73. package/dist/connectors/browserConnector.d.ts +40 -0
  74. package/dist/connectors/browserConnector.js +127 -0
  75. package/dist/connectors/desktopConnector.d.ts +58 -0
  76. package/dist/connectors/desktopConnector.js +68 -0
  77. package/dist/connectors/index.d.ts +10 -0
  78. package/dist/connectors/index.js +2 -0
  79. package/dist/index.cjs +6143 -0
  80. package/dist/index.d.cts +825 -0
  81. package/dist/index.d.ts +20 -0
  82. package/dist/index.js +26 -0
  83. package/dist/index.mjs +6097 -0
  84. package/dist/logger.d.ts +3 -0
  85. package/dist/logger.js +15 -0
  86. package/dist/memory/agentMemory.d.ts +37 -0
  87. package/dist/memory/agentMemory.js +121 -0
  88. package/dist/memory/image.d.ts +26 -0
  89. package/dist/memory/image.js +106 -0
  90. package/dist/memory/index.d.ts +3 -0
  91. package/dist/memory/index.js +3 -0
  92. package/dist/memory/masking.d.ts +7 -0
  93. package/dist/memory/masking.js +141 -0
  94. package/dist/memory/masking.test.d.ts +1 -0
  95. package/dist/memory/masking.test.js +117 -0
  96. package/dist/memory/observation.d.ts +38 -0
  97. package/dist/memory/observation.js +79 -0
  98. package/dist/memory/rendering/index.d.ts +10 -0
  99. package/dist/memory/rendering/index.js +13 -0
  100. package/dist/memory/rendering/renderJsonParts.d.ts +3 -0
  101. package/dist/memory/rendering/renderJsonParts.js +115 -0
  102. package/dist/memory/rendering/renderJsonParts.test.d.ts +1 -0
  103. package/dist/memory/rendering/renderJsonParts.test.js +217 -0
  104. package/dist/memory/rendering/renderXmlParts.d.ts +7 -0
  105. package/dist/memory/rendering/renderXmlParts.js +127 -0
  106. package/dist/memory/serde.d.ts +20 -0
  107. package/dist/memory/serde.js +81 -0
  108. package/dist/memory/util.d.ts +2 -0
  109. package/dist/memory/util.js +17 -0
  110. package/dist/telemetry/events.d.ts +9 -0
  111. package/dist/telemetry/events.js +80 -0
  112. package/dist/telemetry/index.d.ts +6 -0
  113. package/dist/telemetry/index.js +91 -0
  114. package/dist/types.d.ts +30 -0
  115. package/dist/types.js +1 -0
  116. package/dist/util.d.ts +1 -0
  117. package/dist/util.js +12 -0
  118. package/dist/version.d.ts +1 -0
  119. package/dist/version.js +3 -0
  120. package/dist/web/browserProvider.d.ts +23 -0
  121. package/dist/web/browserProvider.js +153 -0
  122. package/dist/web/harness.d.ts +102 -0
  123. package/dist/web/harness.js +340 -0
  124. package/dist/web/stability.d.ts +57 -0
  125. package/dist/web/stability.js +316 -0
  126. package/dist/web/tabs.d.ts +58 -0
  127. package/dist/web/tabs.js +293 -0
  128. package/dist/web/transformer.d.ts +7 -0
  129. package/dist/web/transformer.js +41 -0
  130. package/dist/web/types.d.ts +32 -0
  131. package/dist/web/types.js +2 -0
  132. package/dist/web/util.d.ts +3 -0
  133. package/dist/web/util.js +81 -0
  134. package/dist/web/visualizer/cursor.d.ts +16 -0
  135. package/dist/web/visualizer/cursor.js +152 -0
  136. package/dist/web/visualizer/index.d.ts +22 -0
  137. package/dist/web/visualizer/index.js +47 -0
  138. package/dist/web/visualizer/mouseEffects.d.ts +18 -0
  139. package/dist/web/visualizer/mouseEffects.js +287 -0
  140. package/dist/web/visualizer/typeEffects.d.ts +10 -0
  141. package/dist/web/visualizer/typeEffects.js +190 -0
  142. package/package.json +71 -0
@@ -0,0 +1,127 @@
1
+ //import { Observation, BamlRenderable } from "@/memory";
2
+ import { WebHarness } from "@/web/harness";
3
+ import { webActions } from '@/actions/webActions';
4
+ import { BrowserProvider } from "@/web/browserProvider";
5
+ import logger from "@/logger";
6
+ import { Observation } from "@/memory/observation";
7
+ // export type BrowserOptions = ({ instance: Browser } | { launchOptions?: LaunchOptions }) & {
8
+ // contextOptions?: BrowserContextOptions;
9
+ // };
10
+ // const foo: BrowserOptions = {
11
+ // launchOptions: {},
12
+ // instance: {},
13
+ // }
14
+ // Changed back to 3 - too many situations where the amnesia of having only 1 is very problematic and makes agent act stupidly
15
+ // With caching, using 3 is relatively ok tradeoff
16
+ // Maybe try 2 for now, or could do 3 when prompt caching available else 2
17
+ const DEFAULT_MIN_RETAINED_SCREENSHOTS = 2;
18
+ export class BrowserConnector {
19
+ id = "web";
20
+ harness;
21
+ options;
22
+ browser;
23
+ context;
24
+ logger;
25
+ constructor(options = {}) {
26
+ // console.log("options", options)
27
+ // console.log("options.screenshotMemoryLimit", options.screenshotMemoryLimit)
28
+ this.options = options;
29
+ this.logger = logger.child({
30
+ name: `connectors.${this.id}`
31
+ });
32
+ }
33
+ async onStart() {
34
+ this.logger.info("Starting...");
35
+ this.logger.info("Creating new browser context.");
36
+ this.context = await BrowserProvider.getInstance().newContext(this.options.browser);
37
+ //const contextOptions = this.options.browser && 'contextOptions' in this.options.browser ? this.options.browser.contextOptions : {};
38
+ this.harness = new WebHarness(this.context, {
39
+ //fallbackViewportDimensions: contextOptions?.viewport ?? { width: 1024, height: 768 },
40
+ virtualScreenDimensions: this.options.virtualScreenDimensions,
41
+ visuals: this.options.visuals
42
+ });
43
+ await this.harness.start();
44
+ this.logger.info("WebHarness started.");
45
+ if (this.options.url) {
46
+ this.logger.info(`Navigating to initial URL: ${this.options.url}`);
47
+ await this.harness.navigate(this.options.url);
48
+ //await this.harness.waitForStability();
49
+ }
50
+ this.logger.info("Started successfully.");
51
+ }
52
+ async onStop() {
53
+ this.logger.info("Stopping...");
54
+ if (this.harness) {
55
+ await this.harness.stop();
56
+ this.logger.info("WebHarness cleaned up.");
57
+ }
58
+ if (this.context) {
59
+ await this.context.close();
60
+ this.logger.info("Browser context closed.");
61
+ }
62
+ // Note: We don't close this.browser here if obtained from BrowserProvider,
63
+ // as BrowserProvider manages the singleton browser lifecycle.
64
+ // If this.options.browser was provided, its lifecycle is managed externally.
65
+ this.logger.info("Stopped successfully.");
66
+ }
67
+ getActionSpace() {
68
+ return [...webActions];
69
+ }
70
+ // public get page(): Page {
71
+ // if (!this.harness || !this.harness.page) {
72
+ // throw new Error("WebInteractionConnector: Harness or Page is not available. Ensure onStart has completed.");
73
+ // }
74
+ // return this.harness.page;
75
+ // }
76
+ getHarness() {
77
+ if (!this.harness) {
78
+ throw new Error("WebInteractionConnector: Harness is not available. Ensure onStart has completed.");
79
+ }
80
+ return this.harness;
81
+ }
82
+ async captureCurrentState() {
83
+ if (!this.harness || !this.harness.page) {
84
+ throw new Error("WebInteractionConnector: Harness or Page is not available for capturing state.");
85
+ }
86
+ const [screenshot, tabs] = await Promise.all([
87
+ this.harness.screenshot(),
88
+ this.harness.retrieveTabState()
89
+ ]);
90
+ //const resizedScreenshot = await screenshot.resize()
91
+ // if (this.options.autoResize) {
92
+ // return { screenshot: await screenshot.resize(this.options.autoResize.width, this.options.autoResize.height), tabs: tabs };
93
+ // }
94
+ return { screenshot: await this.transformScreenshot(screenshot), tabs: tabs };
95
+ }
96
+ async transformScreenshot(screenshot) {
97
+ if (this.options.virtualScreenDimensions) {
98
+ return await screenshot.resize(this.options.virtualScreenDimensions.width, this.options.virtualScreenDimensions.height);
99
+ }
100
+ else {
101
+ return screenshot;
102
+ }
103
+ }
104
+ async getLastScreenshot() {
105
+ //return { image: "", dimensions: { width: 0, height: 0 } };
106
+ // TODO: better to use last
107
+ return (await this.captureCurrentState()).screenshot;
108
+ }
109
+ async collectObservations() {
110
+ const currentState = await this.captureCurrentState();
111
+ const observations = [];
112
+ const currentTabs = currentState.tabs;
113
+ let tabInfo = "Open Tabs:\n";
114
+ currentTabs.tabs.forEach((tab, index) => {
115
+ tabInfo += `${index === currentTabs.activeTab ? '[ACTIVE] ' : ''}${tab.title} (${tab.url})`;
116
+ });
117
+ //console.log("this.options.screenshotMemoryLimit", this.options.screenshotMemoryLimit);
118
+ const screenshotLimit = this.options.minScreenshots ?? DEFAULT_MIN_RETAINED_SCREENSHOTS;
119
+ //console.log("screenshotLimit:", screenshotLimit);
120
+ observations.push(Observation.fromConnector(this.id, await this.transformScreenshot(currentState.screenshot), { type: 'screenshot', limit: screenshotLimit, dedupe: true }));
121
+ observations.push(Observation.fromConnector(this.id, tabInfo, { type: 'tabinfo', limit: 1 }));
122
+ return observations;
123
+ }
124
+ async getInstructions() {
125
+ return;
126
+ }
127
+ }
@@ -0,0 +1,58 @@
1
+ import { AgentConnector } from ".";
2
+ import { ActionDefinition } from '@/actions';
3
+ import { Observation } from "@/memory/observation";
4
+ /**
5
+ * Generic desktop automation interface.
6
+ * Implementations can use any desktop automation technology
7
+ * (Lume, PyAutoGUI, Windows UI Automation, etc.)
8
+ */
9
+ export interface DesktopInterface {
10
+ click(x: number, y: number): Promise<void>;
11
+ rightClick(x: number, y: number): Promise<void>;
12
+ doubleClick(x: number, y: number): Promise<void>;
13
+ moveCursor(x: number, y: number): Promise<void>;
14
+ drag(fromX: number, fromY: number, toX: number, toY: number): Promise<void>;
15
+ scroll(x: number, y: number, deltaX: number, deltaY: number): Promise<void>;
16
+ type(text: string): Promise<void>;
17
+ key(key: string): Promise<void>;
18
+ hotkey(keys: string[]): Promise<void>;
19
+ screenshot(): Promise<Buffer>;
20
+ getScreenSize(): Promise<{
21
+ width: number;
22
+ height: number;
23
+ }>;
24
+ navigate?(url: string): Promise<void>;
25
+ getActiveWindow?(): Promise<{
26
+ title: string;
27
+ app: string;
28
+ }>;
29
+ getOpenWindows?(): Promise<Array<{
30
+ title: string;
31
+ app: string;
32
+ isActive: boolean;
33
+ }>>;
34
+ focusWindow?(title: string): Promise<void>;
35
+ openApplication?(name: string): Promise<void>;
36
+ closeApplication?(name: string): Promise<void>;
37
+ }
38
+ export interface DesktopConnectorOptions {
39
+ desktopInterface: DesktopInterface;
40
+ virtualScreenDimensions?: {
41
+ width: number;
42
+ height: number;
43
+ };
44
+ minScreenshots?: number;
45
+ }
46
+ export declare class DesktopConnector implements AgentConnector {
47
+ readonly id: string;
48
+ private desktopInterface;
49
+ private options;
50
+ private logger;
51
+ constructor(options: DesktopConnectorOptions);
52
+ onStart(): Promise<void>;
53
+ onStop(): Promise<void>;
54
+ getActionSpace(): ActionDefinition<any>[];
55
+ collectObservations(): Promise<Observation[]>;
56
+ private formatWindowInfo;
57
+ getInterface(): DesktopInterface;
58
+ }
@@ -0,0 +1,68 @@
1
+ import { desktopActions } from '@/actions/desktopActions';
2
+ import { Observation } from "@/memory/observation";
3
+ import { Image } from "@/memory/image";
4
+ import logger from "@/logger";
5
+ import sharp from 'sharp';
6
+ export class DesktopConnector {
7
+ id = "desktop";
8
+ desktopInterface;
9
+ options;
10
+ logger;
11
+ constructor(options) {
12
+ this.options = options;
13
+ this.desktopInterface = options.desktopInterface;
14
+ this.logger = logger.child({
15
+ name: `connectors.${this.id}`
16
+ });
17
+ }
18
+ async onStart() {
19
+ // Desktop interface should already be initialized by the service
20
+ this.logger.info("Desktop connector started");
21
+ }
22
+ async onStop() {
23
+ // Cleanup handled by the interface provider
24
+ this.logger.info("Desktop connector stopped");
25
+ }
26
+ getActionSpace() {
27
+ return desktopActions;
28
+ }
29
+ async collectObservations() {
30
+ const observations = [];
31
+ // Always collect screenshot
32
+ const screenshot = await this.desktopInterface.screenshot();
33
+ const sharpImage = sharp(screenshot);
34
+ const image = new Image(sharpImage);
35
+ // Apply virtual screen dimensions if configured
36
+ const transformedImage = this.options.virtualScreenDimensions
37
+ ? await image.resize(this.options.virtualScreenDimensions.width, this.options.virtualScreenDimensions.height)
38
+ : image;
39
+ observations.push(Observation.fromConnector(this.id, transformedImage, {
40
+ type: 'screenshot',
41
+ limit: this.options.minScreenshots ?? 2,
42
+ dedupe: true
43
+ }));
44
+ // Optional: Window information
45
+ if (this.desktopInterface.getOpenWindows) {
46
+ try {
47
+ const windows = await this.desktopInterface.getOpenWindows();
48
+ const windowInfo = this.formatWindowInfo(windows);
49
+ observations.push(Observation.fromConnector(this.id, windowInfo, { type: 'window-info', limit: 1 }));
50
+ }
51
+ catch (error) {
52
+ this.logger.warn('Failed to get window information', error);
53
+ }
54
+ }
55
+ return observations;
56
+ }
57
+ formatWindowInfo(windows) {
58
+ let info = "Open Windows:\n";
59
+ windows.forEach(window => {
60
+ info += `${window.isActive ? '[ACTIVE] ' : ''}${window.title} (${window.app})\n`;
61
+ });
62
+ return info;
63
+ }
64
+ // Expose interface for actions to use
65
+ getInterface() {
66
+ return this.desktopInterface;
67
+ }
68
+ }
@@ -0,0 +1,10 @@
1
+ import { ActionDefinition } from '@/actions';
2
+ import { Observation } from '@/memory/observation';
3
+ export interface AgentConnector {
4
+ id: string;
5
+ onStart?(): Promise<void>;
6
+ onStop?(): Promise<void>;
7
+ getActionSpace?(): ActionDefinition<any>[];
8
+ collectObservations?(): Promise<Observation[]>;
9
+ getInstructions?(): Promise<void | string>;
10
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //export { BrowserConnector, BrowserConnectorOptions } from './browserConnector';