@mindstudio-ai/agent 0.1.171 → 0.1.173
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/CHANGELOG.md +24 -0
- package/dist/cli.js +575 -12
- package/dist/index.d.ts +406 -10
- package/dist/index.js +569 -5
- package/dist/index.js.map +1 -1
- package/dist/postinstall.js +575 -12
- package/llms.txt +28 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4195,6 +4195,7 @@ function createVoice(call) {
|
|
|
4195
4195
|
async call(params) {
|
|
4196
4196
|
return await call("call", {
|
|
4197
4197
|
to: params.to,
|
|
4198
|
+
...params.from !== void 0 ? { from: params.from } : {},
|
|
4198
4199
|
assumeIdentity: params.assumeIdentity === true
|
|
4199
4200
|
});
|
|
4200
4201
|
}
|
|
@@ -4321,8 +4322,520 @@ function createEvents(call) {
|
|
|
4321
4322
|
};
|
|
4322
4323
|
}
|
|
4323
4324
|
|
|
4324
|
-
// src/
|
|
4325
|
+
// src/browser/index.ts
|
|
4325
4326
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
4327
|
+
var RETRYABLE_STATUSES = /* @__PURE__ */ new Set([502, 503, 504]);
|
|
4328
|
+
var RETRY_BACKOFFS_MS = [1e3, 2e3, 4e3, 8e3, 15e3, 15e3, 15e3];
|
|
4329
|
+
var MAX_TOOL_STEPS = 20;
|
|
4330
|
+
var describeScreenPrompt = (question) => [
|
|
4331
|
+
"This is a screenshot of a web page, taken by an agent working through the site.",
|
|
4332
|
+
question ? `First answer this question about it: ${question}
|
|
4333
|
+
|
|
4334
|
+
Then describe the page briefly: its layout, its content, and what state it is in.` : "Describe what a person looking at it would see, top to bottom: the layout, the content, what state the page is in (a form partly filled, an error, a dialog open), and anything that looks wrong.",
|
|
4335
|
+
"Be concise and concrete, and quote visible text exactly where it matters. Answer in plain Markdown with no preamble."
|
|
4336
|
+
].join("\n\n");
|
|
4337
|
+
var toWireTarget = (target) => typeof target === "string" ? { ref: target } : target;
|
|
4338
|
+
var BrowserSession = class {
|
|
4339
|
+
/** @internal Use `browser.open()`. */
|
|
4340
|
+
constructor(info, call, steps) {
|
|
4341
|
+
this.call = call;
|
|
4342
|
+
this.steps = steps;
|
|
4343
|
+
this.id = info.id;
|
|
4344
|
+
this.viewport = info.viewport;
|
|
4345
|
+
this.expiresAt = info.expiresAt;
|
|
4346
|
+
}
|
|
4347
|
+
call;
|
|
4348
|
+
steps;
|
|
4349
|
+
/** Pass to `browser.session(id)` from a later call to pick this session up again. */
|
|
4350
|
+
id;
|
|
4351
|
+
viewport;
|
|
4352
|
+
/** When the session closes, however busy it is. Milliseconds since epoch. */
|
|
4353
|
+
expiresAt;
|
|
4354
|
+
/** Every action so far, chained: see `run`. */
|
|
4355
|
+
queue = Promise.resolve();
|
|
4356
|
+
/**
|
|
4357
|
+
* One action at a time, in the order they were called. A session has one
|
|
4358
|
+
* page, so actions started together (a `Promise.all`, or a task agent's
|
|
4359
|
+
* tools from one turn, which run concurrently) would otherwise reach it in
|
|
4360
|
+
* whatever order their requests landed: a click on Submit before the field
|
|
4361
|
+
* it submits was typed.
|
|
4362
|
+
*/
|
|
4363
|
+
run(action) {
|
|
4364
|
+
const result = this.queue.then(() => this.send(action));
|
|
4365
|
+
this.queue = result.catch(() => {
|
|
4366
|
+
});
|
|
4367
|
+
return result;
|
|
4368
|
+
}
|
|
4369
|
+
/**
|
|
4370
|
+
* One action's request, asked again under the same id when the answer
|
|
4371
|
+
* didn't get back (a dropped connection, an api pod rolling). The platform
|
|
4372
|
+
* runs an id once, so asking again waits on the action already under way
|
|
4373
|
+
* rather than clicking twice.
|
|
4374
|
+
*/
|
|
4375
|
+
async send(action) {
|
|
4376
|
+
const commandId = randomUUID2();
|
|
4377
|
+
for (let attempt = 0; ; attempt++) {
|
|
4378
|
+
try {
|
|
4379
|
+
const { value } = await this.call("run", {
|
|
4380
|
+
sessionId: this.id,
|
|
4381
|
+
action,
|
|
4382
|
+
commandId
|
|
4383
|
+
});
|
|
4384
|
+
return value;
|
|
4385
|
+
} catch (err) {
|
|
4386
|
+
const cut = !(err instanceof MindStudioError) || RETRYABLE_STATUSES.has(err.status) && err.code !== "browser_unavailable";
|
|
4387
|
+
if (!cut || attempt >= RETRY_BACKOFFS_MS.length) {
|
|
4388
|
+
throw err;
|
|
4389
|
+
}
|
|
4390
|
+
await new Promise((r) => setTimeout(r, RETRY_BACKOFFS_MS[attempt]));
|
|
4391
|
+
}
|
|
4392
|
+
}
|
|
4393
|
+
}
|
|
4394
|
+
/** Open a URL and wait for it to load. */
|
|
4395
|
+
goto(url) {
|
|
4396
|
+
return this.run({ op: "goto", url });
|
|
4397
|
+
}
|
|
4398
|
+
/** The page as it is now. */
|
|
4399
|
+
snapshot() {
|
|
4400
|
+
return this.run({ op: "snapshot" });
|
|
4401
|
+
}
|
|
4402
|
+
/** Click an element (tap, on a mobile viewport), and wait for what it started to settle. */
|
|
4403
|
+
click(target) {
|
|
4404
|
+
return this.run({ op: "click", target: toWireTarget(target) });
|
|
4405
|
+
}
|
|
4406
|
+
/**
|
|
4407
|
+
* Click into a field and type. `clear` replaces what's there; `submit`
|
|
4408
|
+
* presses Enter after.
|
|
4409
|
+
*/
|
|
4410
|
+
type(target, text, options = {}) {
|
|
4411
|
+
return this.run({
|
|
4412
|
+
op: "type",
|
|
4413
|
+
target: toWireTarget(target),
|
|
4414
|
+
text,
|
|
4415
|
+
...options
|
|
4416
|
+
});
|
|
4417
|
+
}
|
|
4418
|
+
/** Choose an option of a `<select>`, by its text, value or label. */
|
|
4419
|
+
select(target, option) {
|
|
4420
|
+
return this.run({ op: "select", target: toWireTarget(target), option });
|
|
4421
|
+
}
|
|
4422
|
+
/**
|
|
4423
|
+
* Press a key on whatever has focus: `Enter`, `Tab`, `Escape`, `Backspace`,
|
|
4424
|
+
* `Delete`, the arrows, `PageUp`, `PageDown`, `Home`, `End`, `' '`, or any
|
|
4425
|
+
* single character.
|
|
4426
|
+
*/
|
|
4427
|
+
press(key) {
|
|
4428
|
+
return this.run({ op: "press", key });
|
|
4429
|
+
}
|
|
4430
|
+
/** Scroll an element into view, or the page by `by` pixels (most of a screen, by default). */
|
|
4431
|
+
scroll(options = {}) {
|
|
4432
|
+
return this.run({
|
|
4433
|
+
op: "scroll",
|
|
4434
|
+
...options.to !== void 0 ? { target: toWireTarget(options.to) } : {},
|
|
4435
|
+
...options.by !== void 0 ? { by: options.by } : {}
|
|
4436
|
+
});
|
|
4437
|
+
}
|
|
4438
|
+
/** Wait for text to appear on the page. Throws once `timeoutSeconds` (15 by default) pass without it. */
|
|
4439
|
+
waitFor(text, options = {}) {
|
|
4440
|
+
return this.run({
|
|
4441
|
+
op: "wait",
|
|
4442
|
+
text,
|
|
4443
|
+
...options.timeoutSeconds !== void 0 ? { timeoutMs: options.timeoutSeconds * 1e3 } : {}
|
|
4444
|
+
});
|
|
4445
|
+
}
|
|
4446
|
+
async screenshot(options = {}) {
|
|
4447
|
+
const { store, key, fullPage, cursor, boxes } = options;
|
|
4448
|
+
const asked = {
|
|
4449
|
+
op: "screenshot",
|
|
4450
|
+
fullPage: !!fullPage,
|
|
4451
|
+
cursor: !!cursor,
|
|
4452
|
+
...boxes?.length ? { boxes: boxes.map(toWireTarget) } : {}
|
|
4453
|
+
};
|
|
4454
|
+
const measured = (shot2) => ({
|
|
4455
|
+
width: shot2.width,
|
|
4456
|
+
height: shot2.height,
|
|
4457
|
+
deviceScaleFactor: shot2.deviceScaleFactor,
|
|
4458
|
+
...shot2.boxes ? { boxes: shot2.boxes } : {}
|
|
4459
|
+
});
|
|
4460
|
+
if (store) {
|
|
4461
|
+
const token = await store.createUploadToken({
|
|
4462
|
+
...key ? { key } : { filename: "screenshot.png" },
|
|
4463
|
+
contentType: "image/png"
|
|
4464
|
+
});
|
|
4465
|
+
const shot2 = await this.run({
|
|
4466
|
+
...asked,
|
|
4467
|
+
upload: { url: token.upload.url, fields: token.upload.fields }
|
|
4468
|
+
});
|
|
4469
|
+
return { ...await store.head(token.key), ...measured(shot2) };
|
|
4470
|
+
}
|
|
4471
|
+
const shot = await this.run(asked);
|
|
4472
|
+
return {
|
|
4473
|
+
data: Buffer.from(shot.data ?? "", "base64"),
|
|
4474
|
+
contentType: "image/png",
|
|
4475
|
+
...measured(shot)
|
|
4476
|
+
};
|
|
4477
|
+
}
|
|
4478
|
+
/**
|
|
4479
|
+
* Run a function in the page and get back what it returns (as JSON). The
|
|
4480
|
+
* function is sent as source, so it can't use variables from your code:
|
|
4481
|
+
* pass what it needs as `arg`.
|
|
4482
|
+
*
|
|
4483
|
+
* ```ts
|
|
4484
|
+
* const prices = await session.evaluate(
|
|
4485
|
+
* (selector) => [...document.querySelectorAll(selector)].map((el) => el.textContent),
|
|
4486
|
+
* '.price',
|
|
4487
|
+
* );
|
|
4488
|
+
* ```
|
|
4489
|
+
*/
|
|
4490
|
+
evaluate(fn, arg) {
|
|
4491
|
+
return this.run({
|
|
4492
|
+
op: "evaluate",
|
|
4493
|
+
expression: `(${fn.toString()})(${arg === void 0 ? "" : JSON.stringify(arg)})`
|
|
4494
|
+
});
|
|
4495
|
+
}
|
|
4496
|
+
/**
|
|
4497
|
+
* A tool for `mindstudio.runTask()` that drives this session, so a model can
|
|
4498
|
+
* browse. It takes a list of steps (open a URL, read the page, click, type,
|
|
4499
|
+
* choose an option, press a key, scroll, wait for text), runs them in order,
|
|
4500
|
+
* stops at the first that fails, and answers with each step's outcome and
|
|
4501
|
+
* the page after the last one. A model plans "fill the form and submit it"
|
|
4502
|
+
* as one call rather than one turn a field. Only the latest page stays in
|
|
4503
|
+
* the task's transcript (`supersedes`), so a long walk doesn't fill it.
|
|
4504
|
+
*
|
|
4505
|
+
* With `screenshots`, a second tool, `browser_screenshot`, lets the model
|
|
4506
|
+
* see the page: each shot goes into that store and the model reads a
|
|
4507
|
+
* description of it. Those answers all stay in the transcript, since each
|
|
4508
|
+
* names a kept file.
|
|
4509
|
+
*/
|
|
4510
|
+
tools(options = {}) {
|
|
4511
|
+
const secrets = options.secrets ?? {};
|
|
4512
|
+
const secretNames = Object.keys(secrets);
|
|
4513
|
+
const screenshots = options.screenshots;
|
|
4514
|
+
const tools = [
|
|
4515
|
+
{
|
|
4516
|
+
name: "browser",
|
|
4517
|
+
description: `Drive the browser. Run one or more steps in order: snapshot (read the page), navigate (open a URL), click, type, select (a dropdown option), press (a key), scroll, and wait (for text to appear). Start with a snapshot to see the page: everything you can act on has a ref ("e12"), good until the next page. Name an element by its ref, or by text, role, label or a CSS selector. Steps stop at the first that fails, and the answer says why. The answer has each step's outcome and the page after the last step: its URL, title and outline. Earlier pages leave the conversation when a newer one arrives, so plan from the latest. Batch the steps you can predict, such as filling a form and submitting it, into one call.` + (secretNames.length > 0 ? ` To type a secret you never see, give type a secret instead of text: ${secretNames.join(", ")}.` : "") + (screenshots ? " To see how the page looks, which the outline can only partly say, use browser_screenshot." : ""),
|
|
4518
|
+
inputSchema: {
|
|
4519
|
+
type: "object",
|
|
4520
|
+
properties: {
|
|
4521
|
+
steps: {
|
|
4522
|
+
type: "array",
|
|
4523
|
+
description: `One to ${MAX_TOOL_STEPS} steps, run in order.`,
|
|
4524
|
+
items: {
|
|
4525
|
+
type: "object",
|
|
4526
|
+
properties: {
|
|
4527
|
+
command: {
|
|
4528
|
+
type: "string",
|
|
4529
|
+
enum: [
|
|
4530
|
+
"snapshot",
|
|
4531
|
+
"navigate",
|
|
4532
|
+
"click",
|
|
4533
|
+
"type",
|
|
4534
|
+
"select",
|
|
4535
|
+
"press",
|
|
4536
|
+
"scroll",
|
|
4537
|
+
"wait"
|
|
4538
|
+
]
|
|
4539
|
+
},
|
|
4540
|
+
ref: {
|
|
4541
|
+
type: "string",
|
|
4542
|
+
description: "An element, by its ref from the latest page (most reliable)."
|
|
4543
|
+
},
|
|
4544
|
+
text: {
|
|
4545
|
+
type: "string",
|
|
4546
|
+
description: "For click and wait: the element's name or visible text, or the text to wait for. For type: the text to type."
|
|
4547
|
+
},
|
|
4548
|
+
role: {
|
|
4549
|
+
type: "string",
|
|
4550
|
+
description: "With text, only an element of this role (button, link, textbox\u2026)."
|
|
4551
|
+
},
|
|
4552
|
+
label: {
|
|
4553
|
+
type: "string",
|
|
4554
|
+
description: "A form field, by its label."
|
|
4555
|
+
},
|
|
4556
|
+
selector: {
|
|
4557
|
+
type: "string",
|
|
4558
|
+
description: "A CSS selector, the last resort."
|
|
4559
|
+
},
|
|
4560
|
+
url: {
|
|
4561
|
+
type: "string",
|
|
4562
|
+
description: "For navigate: an http or https URL."
|
|
4563
|
+
},
|
|
4564
|
+
...secretNames.length > 0 ? {
|
|
4565
|
+
secret: {
|
|
4566
|
+
type: "string",
|
|
4567
|
+
enum: secretNames,
|
|
4568
|
+
description: "For type: a secret to type instead of text."
|
|
4569
|
+
}
|
|
4570
|
+
} : {},
|
|
4571
|
+
clear: {
|
|
4572
|
+
type: "boolean",
|
|
4573
|
+
description: "For type: replace what the field holds."
|
|
4574
|
+
},
|
|
4575
|
+
submit: {
|
|
4576
|
+
type: "boolean",
|
|
4577
|
+
description: "For type: press Enter after."
|
|
4578
|
+
},
|
|
4579
|
+
option: {
|
|
4580
|
+
type: "string",
|
|
4581
|
+
description: "For select: the option's text or value."
|
|
4582
|
+
},
|
|
4583
|
+
key: {
|
|
4584
|
+
type: "string",
|
|
4585
|
+
description: "For press: Enter, Tab, Escape, Backspace, Delete, an arrow, PageUp, PageDown, Home, End, or one character."
|
|
4586
|
+
},
|
|
4587
|
+
by: {
|
|
4588
|
+
type: "number",
|
|
4589
|
+
description: "For scroll: pixels down (negative for up). With an element instead, scrolls it into view; with neither, most of a screen."
|
|
4590
|
+
},
|
|
4591
|
+
timeoutSeconds: {
|
|
4592
|
+
type: "number",
|
|
4593
|
+
description: "For wait: how long to wait (15 by default)."
|
|
4594
|
+
}
|
|
4595
|
+
},
|
|
4596
|
+
required: ["command"]
|
|
4597
|
+
}
|
|
4598
|
+
}
|
|
4599
|
+
},
|
|
4600
|
+
required: ["steps"]
|
|
4601
|
+
},
|
|
4602
|
+
supersedes: "browser-page",
|
|
4603
|
+
execute: async (input) => this.runSteps(input.steps, secrets)
|
|
4604
|
+
}
|
|
4605
|
+
];
|
|
4606
|
+
if (screenshots) {
|
|
4607
|
+
tools.push({
|
|
4608
|
+
name: "browser_screenshot",
|
|
4609
|
+
description: "See the page as a person would. Takes a screenshot of the browser, keeps it, and answers with a description of what it shows, and the answer to your question if you ask one. Use it when the page's outline can't tell you what you need: how the page looks, a chart or an image, whether something is visibly wrong. The answer's key names the kept screenshot.",
|
|
4610
|
+
inputSchema: {
|
|
4611
|
+
type: "object",
|
|
4612
|
+
properties: {
|
|
4613
|
+
question: {
|
|
4614
|
+
type: "string",
|
|
4615
|
+
description: "What to look for or answer from the page. Leave it out for a description of the whole page."
|
|
4616
|
+
},
|
|
4617
|
+
fullPage: {
|
|
4618
|
+
type: "boolean",
|
|
4619
|
+
description: "The whole page, top to bottom, not just what is on screen."
|
|
4620
|
+
},
|
|
4621
|
+
cursor: {
|
|
4622
|
+
type: "boolean",
|
|
4623
|
+
description: "Keep the cursor in the shot, where the last action landed."
|
|
4624
|
+
}
|
|
4625
|
+
}
|
|
4626
|
+
},
|
|
4627
|
+
execute: async (input) => this.describeScreen(screenshots, input)
|
|
4628
|
+
});
|
|
4629
|
+
}
|
|
4630
|
+
return tools;
|
|
4631
|
+
}
|
|
4632
|
+
/**
|
|
4633
|
+
* The screenshot tool: the shot into the store, then a vision model's
|
|
4634
|
+
* description of it. A description that fails still answers with the kept
|
|
4635
|
+
* shot's key, so the model can name it.
|
|
4636
|
+
*/
|
|
4637
|
+
async describeScreen(store, input) {
|
|
4638
|
+
const question = typeof input.question === "string" && input.question.trim() ? input.question.trim() : void 0;
|
|
4639
|
+
const shot = await this.screenshot({
|
|
4640
|
+
store,
|
|
4641
|
+
fullPage: input.fullPage === true,
|
|
4642
|
+
cursor: input.cursor === true
|
|
4643
|
+
});
|
|
4644
|
+
try {
|
|
4645
|
+
const { analysis } = await this.steps("analyzeImage", {
|
|
4646
|
+
imageUrl: shot.url,
|
|
4647
|
+
prompt: describeScreenPrompt(question)
|
|
4648
|
+
});
|
|
4649
|
+
return { key: shot.key, description: analysis };
|
|
4650
|
+
} catch (err) {
|
|
4651
|
+
return {
|
|
4652
|
+
key: shot.key,
|
|
4653
|
+
error: `The screenshot was kept, but describing it failed: ${err instanceof Error ? err.message : "unknown error"}`
|
|
4654
|
+
};
|
|
4655
|
+
}
|
|
4656
|
+
}
|
|
4657
|
+
/** The tool's steps, in order, stopping at the first that fails. */
|
|
4658
|
+
async runSteps(raw, secrets) {
|
|
4659
|
+
const steps = Array.isArray(raw) ? raw : [];
|
|
4660
|
+
if (steps.length === 0 || steps.length > MAX_TOOL_STEPS) {
|
|
4661
|
+
throw new MindStudioError(
|
|
4662
|
+
`Give between 1 and ${MAX_TOOL_STEPS} steps.`,
|
|
4663
|
+
"invalid_steps",
|
|
4664
|
+
400
|
|
4665
|
+
);
|
|
4666
|
+
}
|
|
4667
|
+
const outcomes = [];
|
|
4668
|
+
let last;
|
|
4669
|
+
let failure;
|
|
4670
|
+
for (const step of steps) {
|
|
4671
|
+
const command = typeof step.command === "string" ? step.command : "";
|
|
4672
|
+
try {
|
|
4673
|
+
const answer = await this.runStep(command, step, secrets);
|
|
4674
|
+
last = answer;
|
|
4675
|
+
outcomes.push({
|
|
4676
|
+
command,
|
|
4677
|
+
ok: true,
|
|
4678
|
+
..."matched" in answer ? { matched: answer.matched } : {}
|
|
4679
|
+
});
|
|
4680
|
+
} catch (err) {
|
|
4681
|
+
failure = err instanceof Error ? err : new Error(String(err));
|
|
4682
|
+
outcomes.push({ command, ok: false, error: failure.message });
|
|
4683
|
+
break;
|
|
4684
|
+
}
|
|
4685
|
+
}
|
|
4686
|
+
if (failure) {
|
|
4687
|
+
const stepError = failure;
|
|
4688
|
+
last = await this.snapshot().catch(() => {
|
|
4689
|
+
throw stepError;
|
|
4690
|
+
});
|
|
4691
|
+
}
|
|
4692
|
+
return {
|
|
4693
|
+
steps: outcomes,
|
|
4694
|
+
...outcomes.length < steps.length ? { notRun: steps.length - outcomes.length } : {},
|
|
4695
|
+
page: {
|
|
4696
|
+
url: last.url,
|
|
4697
|
+
title: last.title,
|
|
4698
|
+
snapshot: last.snapshot,
|
|
4699
|
+
...last.dialogs ? { dialogs: last.dialogs } : {}
|
|
4700
|
+
}
|
|
4701
|
+
};
|
|
4702
|
+
}
|
|
4703
|
+
runStep(command, step, secrets) {
|
|
4704
|
+
const str = (v) => typeof v === "string" ? v : void 0;
|
|
4705
|
+
const target = () => {
|
|
4706
|
+
if (str(step.ref)) {
|
|
4707
|
+
return str(step.ref);
|
|
4708
|
+
}
|
|
4709
|
+
const described = {
|
|
4710
|
+
...command !== "type" && str(step.text) ? { text: str(step.text) } : {},
|
|
4711
|
+
...str(step.role) ? { role: str(step.role) } : {},
|
|
4712
|
+
...str(step.label) ? { label: str(step.label) } : {},
|
|
4713
|
+
...str(step.selector) ? { selector: str(step.selector) } : {}
|
|
4714
|
+
};
|
|
4715
|
+
if (Object.keys(described).length === 0) {
|
|
4716
|
+
throw new MindStudioError(
|
|
4717
|
+
`${command} needs an element: a ref, text, role, label or selector.`,
|
|
4718
|
+
"invalid_steps",
|
|
4719
|
+
400
|
|
4720
|
+
);
|
|
4721
|
+
}
|
|
4722
|
+
return described;
|
|
4723
|
+
};
|
|
4724
|
+
switch (command) {
|
|
4725
|
+
case "snapshot":
|
|
4726
|
+
return this.snapshot();
|
|
4727
|
+
case "navigate":
|
|
4728
|
+
return this.goto(str(step.url) ?? "");
|
|
4729
|
+
case "click":
|
|
4730
|
+
return this.click(target());
|
|
4731
|
+
case "type": {
|
|
4732
|
+
const secret = str(step.secret);
|
|
4733
|
+
if (secret !== void 0 && !(secret in secrets)) {
|
|
4734
|
+
throw new MindStudioError(
|
|
4735
|
+
`No secret named "${secret}".`,
|
|
4736
|
+
"unknown_secret",
|
|
4737
|
+
400
|
|
4738
|
+
);
|
|
4739
|
+
}
|
|
4740
|
+
return this.type(
|
|
4741
|
+
target(),
|
|
4742
|
+
secret !== void 0 ? secrets[secret] : str(step.text) ?? "",
|
|
4743
|
+
{
|
|
4744
|
+
clear: step.clear === true || secret !== void 0,
|
|
4745
|
+
submit: step.submit === true
|
|
4746
|
+
}
|
|
4747
|
+
);
|
|
4748
|
+
}
|
|
4749
|
+
case "select":
|
|
4750
|
+
return this.select(target(), str(step.option) ?? "");
|
|
4751
|
+
case "press":
|
|
4752
|
+
return this.press(str(step.key) ?? "");
|
|
4753
|
+
case "scroll": {
|
|
4754
|
+
const hasTarget = str(step.ref) ?? str(step.label) ?? str(step.selector) ?? str(step.text);
|
|
4755
|
+
return this.scroll({
|
|
4756
|
+
...hasTarget ? { to: target() } : {},
|
|
4757
|
+
...typeof step.by === "number" ? { by: step.by } : {}
|
|
4758
|
+
});
|
|
4759
|
+
}
|
|
4760
|
+
case "wait":
|
|
4761
|
+
return this.waitFor(str(step.text) ?? "", {
|
|
4762
|
+
...typeof step.timeoutSeconds === "number" ? { timeoutSeconds: step.timeoutSeconds } : {}
|
|
4763
|
+
});
|
|
4764
|
+
default:
|
|
4765
|
+
throw new MindStudioError(
|
|
4766
|
+
`Unknown command "${command}".`,
|
|
4767
|
+
"invalid_steps",
|
|
4768
|
+
400
|
|
4769
|
+
);
|
|
4770
|
+
}
|
|
4771
|
+
}
|
|
4772
|
+
/**
|
|
4773
|
+
* A grant to watch this session live, for your frontend: return it from a
|
|
4774
|
+
* method and hand its `token` to `browser.view()` in
|
|
4775
|
+
* `@mindstudio-ai/interface`. Whoever holds it sees the page until it
|
|
4776
|
+
* expires, so check your own access first, as for `events.grant()`.
|
|
4777
|
+
* `ttlSeconds` is clamped to 60 through 3600 (900 by default); the frontend
|
|
4778
|
+
* asks again through your method when it runs out.
|
|
4779
|
+
*
|
|
4780
|
+
* ```ts
|
|
4781
|
+
* export async function watchRun({ runId }: { runId: string }) {
|
|
4782
|
+
* auth.requireRole('admin');
|
|
4783
|
+
* const run = await Runs.get(runId);
|
|
4784
|
+
* const session = await browser.session(run.browserSessionId);
|
|
4785
|
+
* return session.grantView();
|
|
4786
|
+
* }
|
|
4787
|
+
* ```
|
|
4788
|
+
*/
|
|
4789
|
+
grantView(options = {}) {
|
|
4790
|
+
return this.call("view", { sessionId: this.id, ...options });
|
|
4791
|
+
}
|
|
4792
|
+
/** Close the session and its browser. */
|
|
4793
|
+
async close() {
|
|
4794
|
+
await this.call("close", { sessionId: this.id });
|
|
4795
|
+
}
|
|
4796
|
+
};
|
|
4797
|
+
function createBrowser(call, steps) {
|
|
4798
|
+
return {
|
|
4799
|
+
async open(options = {}) {
|
|
4800
|
+
const { session: session2 } = await call("open", {
|
|
4801
|
+
...options.viewport !== void 0 ? { viewport: options.viewport } : {},
|
|
4802
|
+
...options.idleTimeoutSeconds !== void 0 ? { idleTimeoutSeconds: options.idleTimeoutSeconds } : {}
|
|
4803
|
+
});
|
|
4804
|
+
let ready = session2.ready;
|
|
4805
|
+
let failures = 0;
|
|
4806
|
+
while (!ready) {
|
|
4807
|
+
try {
|
|
4808
|
+
({ ready } = await call("ready", { sessionId: session2.id }));
|
|
4809
|
+
failures = 0;
|
|
4810
|
+
} catch (err) {
|
|
4811
|
+
const answered = err instanceof MindStudioError && err.status < 500;
|
|
4812
|
+
if (answered || err.code === "browser_unavailable" || ++failures > 5) {
|
|
4813
|
+
throw err;
|
|
4814
|
+
}
|
|
4815
|
+
await new Promise((r) => setTimeout(r, 2e3));
|
|
4816
|
+
}
|
|
4817
|
+
}
|
|
4818
|
+
const opened = new BrowserSession(session2, call, steps);
|
|
4819
|
+
if (options.url) {
|
|
4820
|
+
try {
|
|
4821
|
+
await opened.goto(options.url);
|
|
4822
|
+
} catch (err) {
|
|
4823
|
+
await opened.close().catch(() => {
|
|
4824
|
+
});
|
|
4825
|
+
throw err;
|
|
4826
|
+
}
|
|
4827
|
+
}
|
|
4828
|
+
return opened;
|
|
4829
|
+
},
|
|
4830
|
+
async session(id) {
|
|
4831
|
+
const { session: session2 } = await call("session", { sessionId: id });
|
|
4832
|
+
return new BrowserSession(session2, call, steps);
|
|
4833
|
+
}
|
|
4834
|
+
};
|
|
4835
|
+
}
|
|
4836
|
+
|
|
4837
|
+
// src/task/local.ts
|
|
4838
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
4326
4839
|
|
|
4327
4840
|
// src/generated/metadata.ts
|
|
4328
4841
|
var stepMetadata = {
|
|
@@ -4548,9 +5061,9 @@ var stepMetadata = {
|
|
|
4548
5061
|
"downloadVideo": {
|
|
4549
5062
|
stepType: "downloadVideo",
|
|
4550
5063
|
description: "Download a video file",
|
|
4551
|
-
usageNotes: "- Works with YouTube, TikTok, etc., by using ytdlp behind the scenes\n- Can save as mp4 or mp3",
|
|
4552
|
-
inputSchema: { "type": "object", "properties": { "videoUrl": { "type": "string", "description": "URL of the video to download (supports YouTube, TikTok, etc. via yt-dlp)" }, "format": { "enum": ["mp4", "mp3"], "type": "string", "description": "Output format for the downloaded file" } }, "required": ["videoUrl", "format"] },
|
|
4553
|
-
outputSchema: { "type": "object", "properties": { "videoUrl": { "type": "string", "description": "URL of the downloaded and re-hosted video file" } }, "required": ["videoUrl"] }
|
|
5064
|
+
usageNotes: "- Works with YouTube, TikTok, etc., by using ytdlp behind the scenes\n- Can save as mp4 or mp3\n- `item` picks one item of a URL that holds several: a carousel, a playlist, a post with several videos. A link to one slide of a carousel downloads that slide. `itemCount` in the output says how many there are, so download item 1 and loop for the rest\n- `start` and `duration` (seconds) download a clip. An mp4 clip is cut without re-encoding, so it can start a moment before `start` (for an exact cut, trim the result with `trimMedia`); an mp3 clip is exact\n- Videos and audio only: a photo (a photo post, a photo in a carousel) fails as `video_unavailable`, and so does an item the URL doesn't have",
|
|
5065
|
+
inputSchema: { "type": "object", "properties": { "videoUrl": { "type": "string", "description": "URL of the video to download (supports YouTube, TikTok, etc. via yt-dlp)" }, "format": { "enum": ["mp4", "mp3"], "type": "string", "description": "Output format for the downloaded file" }, "item": { "type": "number", "description": "Which item to download from a URL that holds several (a carousel, a playlist, a post with several videos), counting from 1. Omit for the one the URL names, else the first" }, "start": { "type": "number", "description": "Start of a clip, in seconds. Omit to start at the beginning" }, "duration": { "type": "number", "description": "Length of a clip, in seconds. Omit to run to the end" } }, "required": ["videoUrl", "format"] },
|
|
5066
|
+
outputSchema: { "type": "object", "properties": { "videoUrl": { "type": "string", "description": "URL of the downloaded and re-hosted video file" }, "title": { "type": "string", "description": "The video's title, as the site gives it" }, "durationSec": { "type": "number", "description": "How long the downloaded file runs, in seconds: a clip's own length" }, "itemCount": { "type": "number", "description": "How many items the URL holds: 1 for a single video. Absent for a playlist the site doesn't count up front (a channel, a long playlist)" } }, "required": ["videoUrl"] }
|
|
4554
5067
|
},
|
|
4555
5068
|
"enhanceImageGenerationPrompt": {
|
|
4556
5069
|
stepType: "enhanceImageGenerationPrompt",
|
|
@@ -5698,6 +6211,9 @@ var DEFAULT_MAX_TURNS = 20;
|
|
|
5698
6211
|
var MAX_TURNS_LIMIT = 100;
|
|
5699
6212
|
var MAX_SCHEMA_REPAIR_ATTEMPTS = 3;
|
|
5700
6213
|
var MAX_TOOL_OUTPUT_CHARS = 5e4;
|
|
6214
|
+
var SUPERSEDED_RESULT = JSON.stringify({
|
|
6215
|
+
superseded: "An earlier result, replaced by a newer one from the same tool further on. Work from the latest."
|
|
6216
|
+
});
|
|
5701
6217
|
var MAX_TURN_ATTEMPTS = 5;
|
|
5702
6218
|
var INITIAL_BACKOFF_MS = 1e3;
|
|
5703
6219
|
var STALL_TIMEOUT_MS = 3e5;
|
|
@@ -5933,9 +6449,14 @@ ${structuredOutputExample}
|
|
|
5933
6449
|
const toolKinds = /* @__PURE__ */ new Map();
|
|
5934
6450
|
const toolDefaults = /* @__PURE__ */ new Map();
|
|
5935
6451
|
const functionTools = /* @__PURE__ */ new Map();
|
|
6452
|
+
const supersedeKeys = /* @__PURE__ */ new Map();
|
|
6453
|
+
const latestByKey = /* @__PURE__ */ new Map();
|
|
5936
6454
|
for (const t of options.tools) {
|
|
5937
6455
|
if (typeof t === "object" && "execute" in t) {
|
|
5938
6456
|
functionTools.set(t.name, t.execute);
|
|
6457
|
+
if (t.supersedes) {
|
|
6458
|
+
supersedeKeys.set(t.name, t.supersedes);
|
|
6459
|
+
}
|
|
5939
6460
|
}
|
|
5940
6461
|
}
|
|
5941
6462
|
for (const t of wireTools) {
|
|
@@ -5958,7 +6479,7 @@ ${structuredOutputExample}
|
|
|
5958
6479
|
MAX_TURNS_LIMIT
|
|
5959
6480
|
);
|
|
5960
6481
|
const maxToolOutputChars = options.maxToolOutputChars ?? MAX_TOOL_OUTPUT_CHARS;
|
|
5961
|
-
const traceId =
|
|
6482
|
+
const traceId = randomUUID3();
|
|
5962
6483
|
const messages = [
|
|
5963
6484
|
{ role: "user", content: JSON.stringify(options.input) }
|
|
5964
6485
|
];
|
|
@@ -6137,6 +6658,17 @@ ${structuredOutputExample}
|
|
|
6137
6658
|
toolCallId: toolCall.id,
|
|
6138
6659
|
...isError && { isToolError: true }
|
|
6139
6660
|
});
|
|
6661
|
+
const key = supersedeKeys.get(toolCall.name);
|
|
6662
|
+
if (key !== void 0 && !isError) {
|
|
6663
|
+
const earlier = latestByKey.get(key);
|
|
6664
|
+
if (earlier !== void 0) {
|
|
6665
|
+
messages[earlier] = {
|
|
6666
|
+
...messages[earlier],
|
|
6667
|
+
content: SUPERSEDED_RESULT
|
|
6668
|
+
};
|
|
6669
|
+
}
|
|
6670
|
+
latestByKey.set(key, messages.length - 1);
|
|
6671
|
+
}
|
|
6140
6672
|
onEvent?.({
|
|
6141
6673
|
type: "tool_call_result",
|
|
6142
6674
|
id: toolCall.id,
|
|
@@ -6788,6 +7320,7 @@ var MindStudioAgent = class {
|
|
|
6788
7320
|
_analytics;
|
|
6789
7321
|
_email;
|
|
6790
7322
|
_events;
|
|
7323
|
+
_browser;
|
|
6791
7324
|
/** @internal Auth type — 'internal' for CALLBACK_TOKEN (managed mode), 'apiKey' otherwise. */
|
|
6792
7325
|
_authType;
|
|
6793
7326
|
/** @internal Usage source sent on step executions (from MINDSTUDIO_REQUEST_SOURCE).
|
|
@@ -7854,6 +8387,17 @@ var MindStudioAgent = class {
|
|
|
7854
8387
|
get events() {
|
|
7855
8388
|
return this._events ??= createEvents(this._eventsRequest.bind(this));
|
|
7856
8389
|
}
|
|
8390
|
+
/**
|
|
8391
|
+
* The `browser` namespace — a real Chrome your app drives. `open()` starts
|
|
8392
|
+
* a session; every action answers with the page afterward. See
|
|
8393
|
+
* {@link Browser}.
|
|
8394
|
+
*/
|
|
8395
|
+
get browser() {
|
|
8396
|
+
return this._browser ??= createBrowser(
|
|
8397
|
+
this._browserRequest.bind(this),
|
|
8398
|
+
(stepType, step) => this.executeStep(stepType, step)
|
|
8399
|
+
);
|
|
8400
|
+
}
|
|
7857
8401
|
/**
|
|
7858
8402
|
* Outbound email: sending, the per-recipient delivery log, blast stats, and
|
|
7859
8403
|
* this app's unsubscribe list.
|
|
@@ -8051,6 +8595,17 @@ var MindStudioAgent = class {
|
|
|
8051
8595
|
fallbackCode: "events_error"
|
|
8052
8596
|
});
|
|
8053
8597
|
}
|
|
8598
|
+
/**
|
|
8599
|
+
* @internal Transport for the `browser` namespace —
|
|
8600
|
+
* POST /_internal/v2/browser/<op> with the raw hook token. No 429 retry:
|
|
8601
|
+
* an action repeated is a second click.
|
|
8602
|
+
*/
|
|
8603
|
+
async _browserRequest(op, body) {
|
|
8604
|
+
return this._brokeredRequest("browser", op, body, {
|
|
8605
|
+
fallbackMessage: "Browser call failed",
|
|
8606
|
+
fallbackCode: "browser_error"
|
|
8607
|
+
});
|
|
8608
|
+
}
|
|
8054
8609
|
async _analyticsRequest(op, body) {
|
|
8055
8610
|
const call = () => this._brokeredRequest("analytics", op, body, {
|
|
8056
8611
|
fallbackMessage: "Analytics read failed",
|
|
@@ -9547,6 +10102,13 @@ var events = new Proxy({}, {
|
|
|
9547
10102
|
return typeof value === "function" ? value.bind(target) : value;
|
|
9548
10103
|
}
|
|
9549
10104
|
});
|
|
10105
|
+
var browser = new Proxy({}, {
|
|
10106
|
+
get(_, prop) {
|
|
10107
|
+
const target = mindstudio.browser;
|
|
10108
|
+
const value = Reflect.get(target, prop, target);
|
|
10109
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
10110
|
+
}
|
|
10111
|
+
});
|
|
9550
10112
|
var session = new Proxy(
|
|
9551
10113
|
{},
|
|
9552
10114
|
{
|
|
@@ -9565,6 +10127,7 @@ var prerender = {
|
|
|
9565
10127
|
};
|
|
9566
10128
|
export {
|
|
9567
10129
|
AuthContext,
|
|
10130
|
+
BrowserSession,
|
|
9568
10131
|
HttpResponse,
|
|
9569
10132
|
MAX_EVENT_PAYLOAD_CHARS,
|
|
9570
10133
|
MindStudioAgent2 as MindStudioAgent,
|
|
@@ -9574,6 +10137,7 @@ export {
|
|
|
9574
10137
|
analytics,
|
|
9575
10138
|
auth,
|
|
9576
10139
|
blockTypeAliases,
|
|
10140
|
+
browser,
|
|
9577
10141
|
dataSources,
|
|
9578
10142
|
db,
|
|
9579
10143
|
index_default as default,
|