Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
/**
* Tracks whether a caller-initiated Playwright action is currently in flight.
*/
export
interface
IPlaywrightActionScope
{
:
number
;
}
const
DEFERRED_RESULT_CLEANUP_MS
=
5
*
60_000
;
// 5 minutes
const
SESSION_INACTIVITY_MS
=
30
*
60_000
;
// 30 minutes
const
OPEN_PAGE_NAVIGATION_TIMEOUT_MS
=
30_000
;
/**
* Narrow a raw Playwright transport payload to a {@link CDPRequest}.
*
* Playwright types the `send` payload as `object` but passes structured CDP
* messages (not JSON strings) for a caller-supplied transport, so this guard
* is expected to always hold. It exists to fail loudly (the caller throws)
* should a future Playwright version change the wire format, rather than
* silently forwarding malformed messages.
*/
function
isCDPRequest
(
:
)
:
is
{
const
=
as
<
>
;
return
typeof
.
===
'number'
&&
typeof
.
===
'string'
&&
(
.
===
undefined
||
typeof
.
===
'string'
)
;
}
/**
* Shared-process implementation of {@link IPlaywrightService}.
*
* Manages {@link PlaywrightSession} instances keyed by session ID.
* Each session has its own Playwright browser connection and browser view
* group, created eagerly by the service when the session is first requested.
*
* Each session receives an independent CDP group whose membership is driven by
* the main-process browser-view audience state.
*/
export
class
PlaywrightService
extends
Disposable
implements
IPlaywrightService
{
declare
readonly
:
undefined
;
private
readonly
=
this
.
_register
(
new
DisposableMap<string, PlaywrightSession>
(
)
)
;
/** In-flight session initializations keyed by session ID. */
private
readonly
=
new
Map<string,Promise<PlaywrightSession>>
(
)
;
/** Inactivity timers keyed by session ID. */
private
readonly
=
this
.
_register
(
new
DisposableMap<string, IDisposable>
(
)
)
;
constructor
(
private
readonly
:
number
,
private
readonly
:
,
private
readonly
:
,
private
readonly
:
,
private
readonly
:
,
)
{
super
(
)
;
}
/**
* Get or create a fully-initialized {@link PlaywrightSession} for the
* given session ID. Creates the CDP group and Playwright browser
* connection if the session does not already exist.
*/
private
async
_getOrCreateSession
(
:
string
)
:
Promise
<
>
{
const
=
this
.
.
get
(
)
;
if
(
)
{
this
.
_touchSession
(
)
;
return
;
}
// De-duplicate concurrent initialization for the same session.
const
=
this
.
.
get
(
)
;
if
(
)
{
return
;
}
const
=
this
.
_initSession
(
)
;
this
.
.
set
(
,
)
;
try
{
return
await
;
}
finally
{
this
.
.
delete
(
)
;
}
}
/**
* Create and fully initialize a new session: browser view group,
* Playwright CDP connection, and page replay.
*/
/**
* Reset the inactivity timer for a session. After
* {@link SESSION_INACTIVITY_MS} of no activity the session is
* automatically disposed.
*/
private
_touchSession
(
:
string
)
:
void
{
this
.
.
deleteAndDispose
(
)
;
const
=
disposableTimeout
(
(
)
=>
{
this
.
.
debug
(
`[PlaywrightService] Session ${sessionId} inactive for ${SESSION_INACTIVITY_MS/60_000}m, disposing`
)
;
this
.
.
deleteAndDispose
(
)
;
this
.
.
deleteAndDispose
(
)
;
}
,
SESSION_INACTIVITY_MS
,
)
;
this
.
.
set
(
,
)
;
}
}
/**
* A single session's Playwright browser connection, page tracking, and
* page-matching logic.
*
* Receives an already-connected {@link Browser} and {@link IBrowserViewGroup}
* from the parent {@link PlaywrightService}. Correlates browser view IDs with
* Playwright {@link Page} instances by their Chromium target IDs.
*/
class
PlaywrightSession
extends
Disposable
{
// --- Page matching ---
private
readonly
=
new
Map<string, Page>
(
)
;
private
readonly
=
new
WeakMap<Page,string>
(
)
;
private
readonly
=
new
WeakMap<Page, PlaywrightTab>
(
)
;
private
readonly
=
new
Map<Page,Promise<string>>
(
)
;
private
readonly
=
new
WeakSet<BrowserContext>
(
)
;
/** In-flight deferred results keyed by their generated ID. */
/** Register a disposable to be cleaned up when this session is disposed. */
registerDisposable
(
:
)
:
void
{
this
.
_register
(
)
;
}
// --- Page operations ---
async
waitForPageAndGetSummary
(
:
string
,
:
string
,
:
number
)
:
Promise
<
string
>
{
const
=
await
this
.
_waitForPage
(
,
.
now
(
)
+
)
;
try
{
if
(
!==
'about:blank'
&&
.
url
(
)
===
'about:blank'
)
{
await
.
waitForURL
(
=>
.
toString
(
)
!==
'about:blank'
,
{
:
'domcontentloaded'
,
:
OPEN_PAGE_NAVIGATION_TIMEOUT_MS
}
)
;
}
else
{
await
.
waitForLoadState
(
'domcontentloaded'
,
{
:
OPEN_PAGE_NAVIGATION_TIMEOUT_MS
}
)
;
}
}
catch
(
)
{
if
(
!
isNavigationTimeoutError
(
)
)
{
throw
;
}
throw
new
Error
(
`Timed out waiting for browser page "${pageId}" to navigate to "${expectedUrl}". The page is open and can be reused.`
,
{
:
}
)
;
}
return
this
.
_getSummary
(
)
;
}
async
getSummary
(
:
string
)
:
Promise
<
string
>
{
return
this
.
_getSummary
(
,
true
)
;
}
async
invokeFunctionRaw<T>
(
:
string
,
:
string
,
...
:
unknown
[
]
)
:
Promise
<
T
>
{
const
=
await
this
.
_compileFunction
(
)
;
return
this
.
_runAgainstPage
(
,
(
)
=>
fn
(
,
)
as
T
)
;
}
async
invokeFunction
(
:
string
,
:
string
,
:
unknown
[
]
=
[
]
,
?
:
number
)
:
Promise
<
>
{
this
.
.
info
(
`[PlaywrightSession] Invoking function on view ${pageId}`
)
;
const
:
=
{
:
.
now
(
)
,
:
.
,
:
.
split
(
'\n'
)
.
,
:
new
Map<string,number>
(
)
,
:
false
,
:
0
,
:
false
,
}
;
let
;
try
{
=
await
this
.
_compileFunction
(
)
;
}
catch
(
:
unknown
)
{
// Surface compile/syntax errors as { error, summary }, like other execution failures.
this
.
_logExecution
(
,
false
)
;
const
=
await
this
.
_getSummary
(
)
;
return
{
:
instanceof
Error
?
.
:
String
(
)
,
}
;
}
const
wrappedCallback
=
async
(
:
)
=>
fn
(
createPageApiProxy
(
,
.
)
,
)
;
if
(
!==
undefined
)
{
return
this
.
_runWithDeferral
(
,
,
,
undefined
,
)
;
}
let
,
;
try
{
=
await
this
.
_runAgainstPage
(
,
)
;
}
catch
(
:
unknown
)
{
=
instanceof
Error
?
.
:
String
(
)
;
}
this
.
_logExecution
(
,
!
)
;
const
=
await
this
.
_getSummary
(
)
;
return
{
,
,
}
;
}
async
waitForDeferredResult
(
:
string
,
:
number
)
:
Promise
<
>
{
const
=
this
.
.
get
(
)
;
if
(
!
)
{
throw
new
Error
(
`No deferred result found with ID "${deferredResultId}". It may have been cleaned up or already consumed.`
)
;
}
const
{
,
,
}
=
;
if
(
)
{
.
++
;
}
this
.
.
deleteAndDispose
(
)
;
return
this
.
_runWithDeferral
(
,
(
)
=>
,
,
,
)
;
}
async
replyToFileChooser
(
:
string
,
:
string
[
]
)
:
Promise
<
{
:
string
}
>
{
const
=
await
this
.
_getPage
(
)
;
const
=
this
.
.
get
(
)
;
if
(
!
)
{
throw
new
Error
(
'Failed to reply to file chooser'
)
;
}
await
.
replyToFileChooser
(
)
;
const
=
await
.
getSummary
(
)
;
return
{
}
;
}
async
replyToDialog
(
:
string
,
:
boolean
,
?
:
string
)
:
Promise
<
{
:
string
}
>
{
const
=
await
this
.
_getPage
(
)
;
const
=
this
.
.
get
(
)
;
if
(
!
)
{
throw
new
Error
(
'Failed to reply to dialog'
)
;
}
await
.
replyToDialog
(
,
)
;
const
=
await
.
getSummary
(
)
;
return
{
}
;
}
// --- Private: page operations ---
private
async
_getSummary
(
:
string
,
=
false
)
:
Promise
<
string
>
{
const
=
await
this
.
_getPage
(
)
;
const
=
this
.
.
get
(
)
;
if
(
!
)
{
throw
new
Error
(
'Failed to get page summary'
)
;
}
return
.
getSummary
(
)
;
}
private
async
_runAgainstPage<T>
(
:
string
,
callback
:
(
:
)
=>
T
|
Promise
<
T
>
)
:
Promise
<
T
>
{
const
=
await
this
.
_getPage
(
)
;
const
=
this
.
.
get
(
)
;
if
(
!
)
{
throw
new
Error
(
'Failed to execute function against page'
)
;
}
return
.
safeRunAgainstPage
(
async
(
)
=>
callback
(
)
)
;
}
private
async
_runWithDeferral
(
:
string
,
callback
:
(
:
)
=>
Promise
<
unknown
>
,
:
number
,
?
:
string
,
?
:
)
:
Promise
<
>
{
const
=
new
DeferredPromise
(
)
;
.
.
catch
(
(
)
=>
{
/* waitForDeferredResult observes the rejection when resumed */
}
)
;
// Attach settlement logging once, on the initiating call: `deferred.p` settles
// when the page work finishes no matter how many times the result is deferred,
// resumed, or abandoned, so a deferred run is still logged once it settles.
// `_logExecution` is idempotent, so this is a no-op if the synchronous path
// below already logged a non-deferred completion.
if
(
===
undefined
&&
)
{
.
.
then
(
(
)
=>
this
.
_logExecution
(
,
true
)
,
(
)
=>
this
.
_logExecution
(
,
false
)
)
;
}
const
=
this
.
_runAgainstPage
(
,
async
(
)
=>
{
const
=
callback
(
)
;
.
catch
(
(
)
=>
{
/* prevent unhandled rejection if deferred */
}
)
;
.
settleWith
(
)
;
return
;
}
)
;
let
,
;
let
=
false
;
try
{
=
await
raceTimeout
(
,
,
(
)
=>
{
=
true
;
}
)
;
}
catch
(
:
unknown
)
{
if
(
instanceof
DialogInterruptedError
)
{
=
true
;
}
=
instanceof
Error
?
.
:
String
(
)
;
}
let
:
string
|
undefined
;
if
(
)
{
if
(
)
{
.
=
true
;
}
=
??
generateUuid
(
)
;
const
=
disposableTimeout
(
(
)
=>
this
.
.
deleteAndDispose
(
!
)
,
DEFERRED_RESULT_CLEANUP_MS
)
;
this
.
.
set
(
,
{
,
:
.
,
,
dispose
:
(
)
=>
.
dispose
(
)
}
)
;
this
.
.
info
(
`[PlaywrightSession] Execution interrupted, deferred as ${deferredResultId}`
)
;
}
else
if
(
)
{
// Completed or failed within the timeout: log the outcome now rather than
// relying on the settlement promise, which never settles if the page work
// threw before `settleWith` ran (e.g. the page could not be resolved).
this
.
_logExecution
(
,
!
)
;
}
const
=
await
this
.
_getSummary
(
)
;
return
{
,
,
,
}
;
}
/**
* Emit completion telemetry for a single {@link invokeFunction} call, once the
* page work settles. Idempotent: only the first call for a given context emits,
* so the synchronous and settlement-promise paths can both call it safely.
*/
`Timed out waiting for browser page "${viewId}" to become available. The page is open and can be reused.`
)
;
}
const
=
await
raceTimeout
(
this
.
_tryGetPage
(
)
,
)
;
if
(
)
{
return
;
}
const
=
.
min
(
50
,
-
.
now
(
)
)
;
if
(
>
0
)
{
await
timeout
(
)
;
}
}
}
private
async
_tryGetPage
(
:
string
)
:
Promise
<
|
undefined
>
{
const
=
this
.
.
get
(
)
;
if
(
)
{
return
;
}
this
.
_scanForNewContexts
(
)
;
await
Promise
.
allSettled
(
[
...
this
.
.
values
(
)
]
)
;
const
=
this
.
.
get
(
)
;
if
(
)
{
return
;
}
return
undefined
;
}
private
_onPageAdded
(
:
)
:
Promise
<
string
>
{
const
=
this
.
.
get
(
)
;
if
(
)
{
return
Promise
.
resolve
(
)
;
}
const
=
this
.
.
get
(
)
;
if
(
)
{
return
;
}
const
=
this
.
_resolvePage
(
)
.
finally
(
(
)
=>
{
if
(
this
.
.
get
(
)
===
)
{
this
.
.
delete
(
)
;
}
}
)
;
this
.
.
set
(
,
)
;
return
;
}
private
async
_resolvePage
(
:
)
:
Promise
<
string
>
{
.
once
(
'close'
,
(
)
=>
this
.
_onPageRemoved
(
)
)
;
.
setDefaultTimeout
(
10000
)
;
this
.
.
set
(
,
new
PlaywrightTab
(
,
this
.
,
this
.
)
)
;
const
=
await
this
.
_getPageViewId
(
)
;
if
(
.
isClosed
(
)
)
{
throw
new
Error
(
`Page "${viewId}" closed before it could be resolved`
)
;
}
this
.
_bindPage
(
,
)
;
return
;
}
private
_onPageRemoved
(
:
)
:
void
{
this
.
.
delete
(
)
;
const
=
this
.
.
get
(
)
;
if
(
)
{
this
.
.
delete
(
)
;
}
this
.
.
delete
(
)
;
}
private
async
_getPageViewId
(
:
)
:
Promise
<
string
>
{
const
=
await
.
context
(
)
.
newCDPSession
(
)
;
try
{
const
=
await
.
send
(
'Target.getTargetInfo'
)
;
const
:
=
.
;
const
=
.
??
''
;
if
(
!
)
{
throw
new
Error
(
`CDP target ${targetInfo.targetId} is not an integrated browser view`
)
;
}
return
;
}
finally
{
try
{
await
.
detach
(
)
;
}
catch
(
)
{
this
.
.
warn
(
'[PlaywrightSession] Failed to detach page identity CDP session'
,
)
;
}
}
}
private
_bindPage
(
:
string
,
:
)
:
void
{
this
.
.
set
(
,
)
;
this
.
.
set
(
,
)
;
this
.
.
debug
(
`[PlaywrightSession] Resolved Playwright page to view ${viewId}`
)
;
}
private
_onContextAdded
(
:
)
:
void
{
if
(
!
this
.
.
has
(
)
)
{
this
.
.
add
(
)
;
.
on
(
'page'
,
=>
{
void
this
.
_onPageAdded
(
)
.
catch
(
=>
{
this
.
.
error
(
'[PlaywrightSession] Failed to resolve page'
,
)
;
}
)
;
}
)
;
.
on
(
'close'
,
(
)
=>
this
.
.
delete
(
)
)
;
}
for
(
const
of
.
pages
(
)
)
{
void
this
.
_onPageAdded
(
)
.
catch
(
=>
{
this
.
.
error
(
'[PlaywrightSession] Failed to resolve page'
,
)
;
}
)
;
}
}
// --- Private: context scanning ---
private
_scanForNewContexts
(
)
:
void
{
for
(
const
of
this
.
.
contexts
(
)
)
{
this
.
_onContextAdded
(
)
;
}
}
dispose
(
)
:
void
{
this
.
?.
close
(
)
.
catch
(
(
)
=>
{
/* ignore */
}
)
;
super
.
dispose
(
)
;
}
}
function
isNavigationTimeoutError
(
:
unknown
)
:
boolean
{
if
(
!
(
instanceof
Error
)
)
{
return
false
;
}
return
.
===
'TimeoutError'
||
/Timeout \d+ms exceeded/
.
test
(
.
)
||
/navigation timeout/i
.
test
(
.
)
;
}
/**
* Per-invocation state threaded through {@link PlaywrightSession.invokeFunction}
* and its deferral machinery so completion telemetry can be emitted exactly once
* when the underlying page work settles - even for deferred runs the caller
* never resumes.
*/
interface
IExecutionLogContext
{
/** {@link Date.now} timestamp captured when the invocation began. */
readonly
:
number
;
/** Character length of the executed function source. */
readonly
:
number
;
/** Line count of the executed function source. */
readonly
:
number
;
/** Per-method call counts accumulated by {@link createPageApiProxy}. */
readonly
:
<
string
,
number
>
;
/** Set once the execution is interrupted and deferred at least once. */
:
boolean
;
/** Number of times the caller resumed this execution via {@link PlaywrightSession.waitForDeferredResult}. */
:
number
;
/** Guards against double-logging; set by {@link PlaywrightSession._logExecution}. */
:
boolean
;
}
type
RunPlaywrightCodeEvent
=
{
:
string
;
:
number
;
:
number
;
:
number
;
:
number
;
:
number
;
:
number
;
:
number
;
:
number
;
}
;
type
RunPlaywrightCodeClassification
=
{
:
{
:
'SystemMetaData'
;
:
'FeatureInsight'
;
:
'JSON object mapping dotted `page.*` method names to their call counts (e.g. `{"click":2,"keyboard.press":5}`), in first-observed order.'
}
;
:
{
:
'SystemMetaData'
;
:
'FeatureInsight'
;
:
true
;
:
'Number of distinct `page.*` methods invoked.'
}
;
:
{
:
'SystemMetaData'
;
:
'FeatureInsight'
;
:
true
;
:
'Total `page.*` method calls including duplicates (sum of all per-method counts).'
}
;
:
{
:
'SystemMetaData'
;
:
'PerformanceAndHealth'
;
:
true
;
:
'1 if the code completed without error, 0 otherwise.'
}
;
:
{
:
'SystemMetaData'
;
:
'PerformanceAndHealth'
;
:
true
;
:
'1 if the execution was interrupted and deferred at least once, 0 otherwise.'
}
;
:
{
:
'SystemMetaData'
;
:
'PerformanceAndHealth'
;
:
true
;
:
'Number of times the caller resumed this execution by polling for its deferred result. 0 means the run either completed within the first timeout or was deferred and never resumed (settled in the background).'
}
;
:
{
:
'SystemMetaData'
;
:
'PerformanceAndHealth'
;
:
true
;
:
'Wall-clock time in milliseconds from invocation start until the page work settled.'
}
;
:
{
:
'SystemMetaData'
;
:
'FeatureInsight'
;
:
true
;
:
'Character length of the executed function source.'
}
;
:
{
:
'SystemMetaData'
;
:
'FeatureInsight'
;
:
true
;
:
'Line count of the executed function source.'
}
;
:
'jruales'
;
:
'Tracks how the run_playwright_code chat tool is exercised.'
;
}
;
/**
* Property names that are skipped by {@link createPageApiProxy} so that JS
* runtime/idiomatic accesses don't show up as fake API usage. Includes
* `then`/`catch`/`finally` (so awaiting the proxy never records noise),
* conversion hooks, and `constructor`.
*/
const
PAGE_PROXY_IGNORED_PROPS
=
new
Set<string>
(
[
'then'
,
'catch'
,
'finally'
,
'toJSON'
,
'toString'
,
'valueOf'
,
'constructor'
,
]
)
;
/**
* Maximum nesting depth for the recursive page proxy. The Playwright `page`
* surface only nests one level deep in practice (e.g. `page.keyboard.press`),
* so 3 is generously above any real workload while preventing pathological
* cases on cyclic structures.
*/
const
PAGE_PROXY_MAX_DEPTH
=
3
;
/**
* Wrap a Playwright `page` so every call through the proxy increments a counter
* in {@link methodCalls}, keyed by the dotted path from `page` (e.g. `click`,
* `keyboard.press`). Object properties are proxied recursively (capped at
* {@link PAGE_PROXY_MAX_DEPTH}) so calls on namespaces like `keyboard` and
* `mouse` are visible; symbol keys, `_`-prefixed internals, and
* {@link PAGE_PROXY_IGNORED_PROPS} are skipped to avoid noise.
*
* Wrappers and nested proxies are cached per property so repeated reads return
* the same value, preserving Playwright's object identity (e.g.
* `page.keyboard === page.keyboard`).
*/