Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
// File-system or maintenance side effects (e.g. VACUUM INTO copies the DB to a chosen path)
/
/
,
/\bREINDEX\b/i
,
/\bANALYZE\b/i
,
// Native-code load via SQL function — would be RCE if SQLite is built with extension loading
/
/
,
// Transaction control — meaningless via prepare().all() but reject for clarity
/
b
(
BEGIN
|
COMMIT
|
ROLLBACK
|
SAVEPOINT
|
RELEASE
)
/
,
]
;
/** Strip leading SQL line and block comments plus whitespace, used by the allowlist check. */
function
stripLeadingCommentsAndWhitespace
(
:
string
)
:
string
{
let
=
;
let
:
string
;
do
{
=
;
=
.
replace
(
/^\s+/
,
''
)
;
=
.
replace
(
/^--[^\n]*\n?/
,
''
)
;
=
.
replace
(
/^\/\*[\s\S]*?\*\//
,
''
)
;
}
while
(
!==
)
;
return
;
}
export
interface
SessionStoreSqlParams
{
readonly
?
:
'query'
|
'reindex'
;
readonly
?
:
string
;
readonly
?
:
boolean
;
readonly
:
string
;
/** Originating /chronicle slash command (e.g. 'tips', 'cost-tips', 'search', 'improve'). Used for telemetry attribution only. */
readonly
?
:
'standup'
|
'tips'
|
'cost-tips'
|
'search'
|
'improve'
|
'reindex'
;
}
/** Model description when cloud sync is enabled — uses DuckDB SQL syntax. */
const
CLOUD_MODEL_DESCRIPTION
=
`Query the cloud session store containing ALL past coding sessions across devices and agents. Uses DuckDB syntax (NOT SQLite). SQL queries are read-only — only SELECT and WITH are allowed. Use \`now() - INTERVAL '1 day'\` for date math (NOT \`datetime('now', '-1 day')\` — that's SQLite-only), \`ILIKE\` for text search (no FTS5/MATCH).
Tables: \`sessions\`, \`turns\`, \`session_files\`, \`session_refs\`, \`checkpoints\`, \`events\`, \`tool_requests\`. For column details and query patterns, use the **chronicle** skill.
Actions: 'query' (execute DuckDB SQL), 'reindex' (rebuild index + cloud sync).`
;
class
SessionStoreSqlTool
implements
ICopilotTool<SessionStoreSqlParams>
{
public
static
readonly
=
.
;
public
static
readonly
=
true
;
private
readonly
:
;
constructor
(
@ISessionStore
private
readonly
:
,
@ICopilotTokenManager
private
readonly
:
,
@IAuthenticationService
private
readonly
:
,
@IConfigurationService
:
,
@ITelemetryService
private
readonly
:
,
@IFetcherService
private
readonly
:
,
@IChatDebugFileLoggerService
private
readonly
:
,
@IRunCommandExecutionService
private
readonly
:
,
)
{
this
.
=
new
SessionIndexingPreference
(
)
;
}
async
invoke
(
:
.
<
>
,
:
,
)
:
Promise
<
.
>
{
const
=
.
.
??
'query'
;
const
=
.
.
;
switch
(
)
{
case
'reindex'
:
return
this
.
_invokeReindex
(
.
.
??
false
,
??
'reindex'
,
)
;
default
:
return
this
.
_invokeQuery
(
.
.
??
''
,
,
)
;
}
}
private
async
_invokeQuery
(
:
string
,
:
[
'subcommand'
]
,
:
)
:
Promise
<
.
>
{
// Strip trailing semicolons — models often append them
'Error: Blocked SQL statement. Only SELECT or WITH queries are allowed.'
)
,
]
)
;
}
}
// Allowlist: model-supplied SQL must be a SELECT or WITH (CTE) statement. Strip leading
// comments first so a comment prefix cannot smuggle a non-query past the check.
const
=
stripLeadingCommentsAndWhitespace
(
)
;
if
(
!
/^(SELECT|WITH)\b/i
.
test
(
)
)
{
this
.
_sendTelemetry
(
{
:
'query'
,
,
:
'local'
,
:
true
,
:
0
,
:
0
,
:
false
,
:
'blocked_not_select_or_with'
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
'Error: Blocked SQL statement. Only SELECT or WITH queries are allowed.'
)
,
]
)
;
}
// Block multiple statements — only one query per call
if
(
.
includes
(
';'
)
)
{
this
.
_sendTelemetry
(
{
:
'query'
,
,
:
'local'
,
:
true
,
:
0
,
:
0
,
:
false
,
:
'multiple_statements'
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
'Error: Only one SQL statement per call. Remove semicolons and split into separate calls.'
)
,
]
)
;
}
// Determine query target based on consent
const
=
this
.
.
hasCloudConsent
(
)
;
const
=
.
now
(
)
;
let
=
?
'cloud'
:
'local'
;
let
:
'local'
|
'cloud'
=
?
'cloud'
:
'local'
;
let
=
false
;
try
{
let
:
<
string
,
unknown
>
[
]
;
let
=
false
;
if
(
)
{
// Cloud is enabled — model receives DuckDB description via alternativeDefinition
const
=
new
CloudSessionStoreClient
(
this
.
,
this
.
,
this
.
)
;
const
=
await
.
executeQuery
(
)
;
if
(
&&
'error'
in
)
{
// Cloud query failed — surface the error so model can fix its query
this
.
_sendTelemetry
(
{
:
'query'
,
,
:
'cloud'
,
:
0
,
:
.
now
(
)
-
,
:
false
,
:
.
.
substring
(
0
,
100
)
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
`Error from cloud: ${cloudResult.error}\n\nReminder: Cloud uses DuckDB SQL syntax. Use \`now() - INTERVAL '1 day'\` for date math, \`ILIKE\` for text search (no FTS5/MATCH).`
)
]
)
;
}
else
if
(
!
)
{
// Auth/network failure — fall back to local
=
'local_fallback'
;
=
'local'
;
=
true
;
=
this
.
_executeLocal
(
)
;
}
else
{
=
.
;
=
.
;
}
}
else
{
=
this
.
_executeLocal
(
)
;
}
// Cap rows
if
(
.
>
MAX_ROWS
)
{
=
.
slice
(
0
,
MAX_ROWS
)
;
=
true
;
}
this
.
_sendTelemetry
(
{
:
'query'
,
,
,
,
:
.
,
:
.
now
(
)
-
,
:
true
}
)
;
// Format as table
const
=
formatSqlResult
(
,
,
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
)
]
)
;
}
catch
(
)
{
const
=
instanceof
Error
?
.
:
String
(
)
;
this
.
_sendTelemetry
(
{
:
'query'
,
,
,
,
:
0
,
:
.
now
(
)
-
,
:
false
,
:
.
substring
(
0
,
100
)
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
`Error: ${message}`
)
]
)
;
}
}
/**
* Execute a model-supplied SQL query against the local SQLite session store.
* The query has already been validated by the allowlist + blocklist in `_invokeQuery`.
* `executeReadOnly` adds engine-level enforcement via `setAuthorizer` when available.
*/
private
_executeLocal
(
:
string
)
:
<
string
,
unknown
>
[
]
{
return
this
.
.
executeReadOnly
(
)
;
}
/**
* Reindex action: rebuild the local session store from debug logs,
* then trigger cloud sync if enabled.
*/
// Cloud reindex phase — gated by cloud sync settings in RemoteSessionExporter
if
(
!
.
&&
!
.
)
{
try
{
const
=
await
this
.
.
executeCommand
(
'github.copilot.sessionSync.reindex'
,
(
)
=>
{
/* progress not streamed for tool results */
}
,
,
)
as
{
:
number
;
:
number
;
:
number
;
:
number
}
|
undefined
;
if
(
&&
.
>
0
)
{
.
push
(
`${cloudResult.created} session(s) synced to cloud.`
)
;
}
}
catch
{
// Cloud phase failure is non-fatal — local reindex already succeeded
}
}
this
.
_sendTelemetry
(
{
:
'reindex'
,
,
,
:
.
,
:
.
now
(
)
-
,
:
true
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
.
join
(
'\n'
)
)
]
)
;
}
catch
(
)
{
const
=
instanceof
Error
?
.
:
String
(
)
;
this
.
_sendTelemetry
(
{
:
'reindex'
,
,
,
:
0
,
:
.
now
(
)
-
,
:
false
,
:
.
substring
(
0
,
100
)
}
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
`Error during reindex: ${message}`
)
]
)
;
}
}
private
_sendTelemetry
(
:
{
:
'query'
|
'reindex'
;
?
:
[
'subcommand'
]
;
:
'local'
|
'cloud'
;
?
:
boolean
;
?
:
boolean
;
:
number
;
:
number
;
:
boolean
;
?
:
string
;
}
)
:
void
{
const
{
,
,
,
,
,
,
,
,
}
=
;
// Back-compat: derive the original `source` value so existing dashboards keep working.
const
=
?
'blocked'
:
?
'local_fallback'
:
===
'query'
?
:
;
const
=
{
,
:
??
'unknown'
,
,
,
:
?
'true'
:
'false'
,
}
;
const
=
{
,
}
;
if
(
)
{
/* __GDPR__
"chronicle.sqlQuery" : {
"owner": "vijayu",
"comment": "Tracks chronicle session-store tool invocations (query/reindex) and outcomes",
"command": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Tool action invoked: query or reindex." },
"subcommand": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Originating /chronicle slash command (standup, tips, cost-tips, search, improve, reindex) or 'unknown' for ad-hoc model calls." },
"target": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Whether the invocation primarily targeted the local SQLite store or the cloud session store." },
"source": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Fine-grained source: local, cloud, local_fallback, blocked, or reindex (kept for back-compat)." },
"success": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Whether the invocation succeeded (true/false)." },
"error": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth", "comment": "Truncated error message." },
"rowCount": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "comment": "Number of rows returned." },
"durationMs": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "comment": "Invocation duration in milliseconds." }
}
*/
this
.
.
sendMSFTTelemetryEvent
(
'chronicle.sqlQuery'
,
,
)
;
}
else
{
this
.
.
sendMSFTTelemetryErrorEvent
(
'chronicle.sqlQuery'
,
{
...
,
:
??
'unknown'
,
}
,
)
;
}
}
prepareInvocation
(
:
.
<
>
,
:
,
)
{
const
=
.
.
??
'query'
;
switch
(
)
{
case
'reindex'
:
return
{
:
.
t
(
'Reindexing session store'
)
,
:
.
t
(
'Reindexed session store'
)
,
}
;
default
:
return
{
:
.
t
(
'Querying session store'
)
,
:
.
t
(
'Queried session store'
)
,
}
;
}
}
alternativeDefinition
(
:
.
)
:
.
{
const
=
this
.
.
hasCloudConsent
(
)
;
if
(
!
)
{
return
;
}
// When cloud is enabled, swap the description and inputSchema to use DuckDB syntax
const
=
{
...
.
,
:
{
...
(
.
as
<
string
,
unknown
>
)
.
as
<
string
,
unknown
>
,
:
{
:
'string'
,
:
'A single DuckDB SQL query to execute. Required when action is \'query\'. Read-only queries only (SELECT, WITH). Use now() - INTERVAL for date math, ILIKE for text search. Only one statement per call — do not combine multiple queries with semicolons.'
,
}
,
}
,
}
;
return
{
...
,
:
CLOUD_MODEL_DESCRIPTION
,
:
,
}
;
}
}
/** Max total characters for the formatted result to avoid blowing up the context window. */
const
TOTAL_FORMAT_BUDGET
=
30_000
;
function
formatSqlResult
(
:
<
string
,
unknown
>
[
]
,
:
boolean
,
:
string
)
:
string
{
if
(
.
===
0
)
{
return
`No results found (source: ${source}).`
;
}
const
=
.
keys
(
[
0
]
)
;
// Adaptive per-cell limit: distribute budget evenly across all cells