Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
/** Models sometimes send `new_str` instead of `insert_text` */
?
:
string
;
}
interface
IDeleteParams
{
:
'delete'
;
:
string
;
}
interface
IRenameParams
{
:
'rename'
;
?
:
string
;
:
string
;
/** Models sometimes send `path` instead of `old_path` */
?
:
string
;
}
type
MemoryToolParams
=
IViewParams
|
ICreateParams
|
IStrReplaceParams
|
IInsertParams
|
IDeleteParams
|
IRenameParams
;
function
normalizePath
(
:
string
)
:
string
{
return
.
endsWith
(
'/'
)
?
:
+
'/'
;
}
function
isMemoriesRoot
(
:
string
)
:
boolean
{
return
normalizePath
(
)
===
'/memories/'
;
}
function
validatePath
(
:
string
)
:
string
|
undefined
{
if
(
!
normalizePath
(
)
.
startsWith
(
'/memories/'
)
)
{
return
'Error: All memory paths must start with /memories/'
;
}
if
(
.
includes
(
'..'
)
)
{
return
'Error: Path traversal is not allowed'
;
}
// Reject paths with empty segments (e.g. /memories//etc) or that resolve outside the base
const
=
.
split
(
'/'
)
.
filter
(
=>
.
length
>
0
)
;
if
(
.
some
(
=>
===
'.'
)
)
{
return
'Error: Path traversal is not allowed'
;
}
// After splitting, first segment must be "memories"
if
(
[
0
]
!==
'memories'
)
{
return
'Error: All memory paths must start with /memories/'
;
}
return
undefined
;
}
function
isRepoPath
(
:
string
)
:
boolean
{
return
===
REPO_PATH_PREFIX
||
.
startsWith
(
REPO_PATH_PREFIX
+
'/'
)
;
}
function
isSessionPath
(
:
string
)
:
boolean
{
return
===
SESSION_PATH_PREFIX
||
.
startsWith
(
SESSION_PATH_PREFIX
+
'/'
)
;
}
/**
* Extracts a safe directory name from a chatSessionResource URI string.
* The URI is typically like `vscode-chat-session://local/<sessionId>`.
*/
export
function
extractSessionId
(
:
string
)
:
string
{
const
=
URI
.
parse
(
)
;
// Extract the last path segment as the session ID
const
=
.
path
.
replace
(
/^\//
,
''
)
.
split
(
'/'
)
;
const
=
[
.
length
-
1
]
||
.
authority
||
'unknown'
;
// Sanitize to only safe characters for a directory name
return
.
replace
(
/[^a-zA-Z0-9_.-]/g
,
'_'
)
;
}
function
formatLineNumber
(
:
number
)
:
string
{
return
String
(
)
.
padStart
(
6
,
' '
)
;
}
function
formatFileContent
(
:
string
,
:
string
)
:
string
{
const
=
.
split
(
'\n'
)
;
const
=
.
map
(
(
,
)
=>
`${formatLineNumber(i +1)}\t${line}`
)
;
return
`Here's the content of ${path} with line numbers:\n${numbered.join('\n')}`
;
}
function
makeSnippet
(
:
string
,
:
number
,
:
string
)
:
string
{
const
=
.
split
(
'\n'
)
;
const
=
4
;
const
=
Math
.
max
(
0
,
-
1
-
)
;
const
=
Math
.
min
(
.
length
,
-
1
+
+
1
)
;
const
=
.
slice
(
,
)
;
const
=
.
map
(
(
,
)
=>
`${formatLineNumber(start + i +1)}\t${line}`
)
;
return
`The memory file has been edited. Here's the result of running \`cat -n\` on a snippet of ${path}:\n${numbered.join('\n')}`
;
}
// --- Tool implementation ---
type
MemoryToolOutcome
=
'success'
|
'error'
|
'notFound'
;
interface
MemoryToolResult
{
:
string
;
:
MemoryToolOutcome
;
}
export
class
MemoryTool
implements
ICopilotTool<MemoryToolParams>
{
public
static
readonly
=
ToolName
.
Memory
;
public
static
readonly
=
true
;
constructor
(
IFileSystemService
private
readonly
:
IFileSystemService
,
IMemoryCleanupService
private
readonly
:
IMemoryCleanupService
,
IVSCodeExtensionContext
private
readonly
:
IVSCodeExtensionContext
,
ILogService
private
readonly
:
ILogService
,
ITelemetryService
private
readonly
:
ITelemetryService
,
)
{
this
.
memoryCleanupService
.
start
(
)
;
}
prepareInvocation
(
:
.
LanguageModelToolInvocationPrepareOptions
<
MemoryToolParams
>
,
:
CancellationToken
)
:
.
ProviderResult
<
.
PreparedToolInvocation
>
{
const
=
.
input
.
command
;
const
=
===
'rename'
?
(
.
input
as
IRenameParams
)
.
old_path
??
(
.
input
as
IRenameParams
)
.
path
:
.
input
.
path
;
return
this
.
_prepareLocalInvocation
(
,
??
'/memories/'
,
.
chatSessionResource
)
;
}
private
_prepareLocalInvocation
(
:
string
,
:
string
,
?
:
.
Uri
)
:
.
PreparedToolInvocation
{
// Directory paths (e.g. /memories/, /memories/session/, /memories/session) — show verb only, no file widget.
// Use normalizePath to handle paths with or without trailing slash consistently.
const
=
normalizePath
(
)
;
if
(
.
endsWith
(
'/'
)
||
===
'/memories/session/'
||
===
'/memories/repo/'
||
isMemoriesRoot
(
)
)
{
switch
(
)
{
case
'view'
:
return
{
:
.
t
(
'Reading memory'
)
,
:
.
t
(
'Read memory'
)
}
;
case
'delete'
:
return
{
:
.
t
(
'Deleting memory'
)
,
:
.
t
(
'Deleted memory'
)
}
;
default
:
return
{
:
.
t
(
'Updating memory'
)
,
:
.
t
(
'Updated memory'
)
}
;
}
}
const
=
this
.
_resolveFileWidget
(
,
)
;
switch
(
)
{
case
'view'
:
return
{
:
new
MarkdownString
(
.
t
(
'Reading memory {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Read memory {0}'
,
)
)
}
;
case
'create'
:
return
{
:
new
MarkdownString
(
.
t
(
'Creating memory file {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Created memory file {0}'
,
)
)
}
;
case
'str_replace'
:
return
{
:
new
MarkdownString
(
.
t
(
'Updating memory file {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Updated memory file {0}'
,
)
)
}
;
case
'insert'
:
return
{
:
new
MarkdownString
(
.
t
(
'Inserting into memory file {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Inserted into memory file {0}'
,
)
)
}
;
case
'delete'
:
return
{
:
new
MarkdownString
(
.
t
(
'Deleting memory {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Deleted memory {0}'
,
)
)
}
;
case
'rename'
:
return
{
:
new
MarkdownString
(
.
t
(
'Renaming memory {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Renamed memory {0}'
,
)
)
}
;
default
:
return
{
:
new
MarkdownString
(
.
t
(
'Updating memory {0}'
,
)
)
,
:
new
MarkdownString
(
.
t
(
'Updated memory {0}'
,
)
)
}
;
}
}
/**
* Resolves a local memory path to a file widget string for display in invocation messages.
* Constructs the URI directly from storage URIs to avoid validation that may throw.
*/
private
_resolveFileWidget
(
:
string
,
?
:
.
Uri
)
:
string
{
const
=
.
split
(
'/'
)
.
filter
(
=>
.
length
>
0
)
;
if
(
isSessionPath
(
)
)
{
const
=
this
.
extensionContext
.
storageUri
;
if
(
!
)
{
return
;
}
// Session paths: /memories/session/foo.md → skip 'memories' and 'session'
const
=
.
slice
(
2
)
;
const
=
URI
.
file
(
URI
.
from
(
)
.
path
)
;
const
=
?
[
MEMORY_BASE_DIR
,
extractSessionId
(
URI
.
from
(
)
.
toString
(
)
)
,
...
]
:
[
MEMORY_BASE_DIR
,
...
]
;
return
formatUriForFileWidget
(
URI
.
joinPath
(
,
...
)
)
;
}
if
(
isRepoPath
(
)
)
{
const
=
this
.
extensionContext
.
storageUri
;
if
(
!
)
{
return
;
}
// Repo paths: /memories/repo/foo.md → skip 'memories' and 'repo'
const
=
.
slice
(
2
)
;
const
=
URI
.
file
(
URI
.
from
(
)
.
path
)
;
return
formatUriForFileWidget
(
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
'repo'
,
...
)
)
;
}
const
=
this
.
extensionContext
.
globalStorageUri
;
if
(
!
)
{
return
;
}
// User paths: /memories/foo.md → skip 'memories'
const
=
.
slice
(
1
)
;
const
=
URI
.
file
(
URI
.
from
(
)
.
path
)
;
return
formatUriForFileWidget
(
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
...
)
)
;
}
async
invoke
(
:
.
LanguageModelToolInvocationOptions
<
MemoryToolParams
>
,
:
CancellationToken
)
:
Promise
<
.
LanguageModelToolResult
>
{
const
=
.
input
;
const
=
.
chatSessionResource
?.
toString
(
)
;
const
=
await
this
.
_dispatch
(
,
,
.
chatRequestId
,
.
model
)
;
return
new
LanguageModelToolResult
(
[
new
LanguageModelTextPart
(
)
]
)
;
}
private
async
_dispatch
(
:
MemoryToolParams
,
?
:
string
,
?
:
string
,
?
:
.
LanguageModelChat
)
:
Promise
<
string
>
{
const
=
.
command
===
'rename'
?
(
.
old_path
??
.
path
)
:
.
path
;
if
(
!
)
{
this
.
_sendLocalTelemetry
(
.
command
,
'user'
,
'error'
,
,
)
;
return
'Error: Missing required path parameter.'
;
}
const
=
validatePath
(
)
;
if
(
)
{
this
.
_sendLocalTelemetry
(
.
command
,
'user'
,
'error'
,
,
)
;
return
;
}
// Route /memories/repo/* to local file-based repo memory
if
(
isRepoPath
(
)
)
{
const
=
await
this
.
_dispatchLocal
(
,
'repo'
,
)
;
this
.
_sendLocalTelemetry
(
.
command
,
'repo'
,
.
outcome
,
,
)
;
return
.
text
;
}
// Route /memories/session/* to session-scoped local storage
// Everything else under /memories/* goes to user-scoped storage
const
:
MemoryScope
=
isSessionPath
(
)
?
'session'
:
'user'
;
const
=
await
this
.
_dispatchLocal
(
,
,
)
;
this
.
_sendLocalTelemetry
(
.
command
,
,
.
outcome
,
,
)
;
return
.
text
;
}
private
_resolveUri
(
:
string
,
:
MemoryScope
,
?
:
string
)
:
URI
{
// Validate path format and extract relative path components safely
const
=
validatePath
(
)
;
if
(
)
{
throw
new
Error
(
)
;
}
// Extract path components by splitting, skipping empty and special segments
const
=
.
split
(
'/'
)
.
filter
(
=>
.
length
>
0
)
;
// segments[0] is 'memories', segments[1] might be 'session' or 'repo', rest are file path components
let
:
string
[
]
;
if
(
===
'session'
)
{
const
=
this
.
extensionContext
.
storageUri
;
if
(
!
)
{
throw
new
Error
(
'No workspace storage available. Session memory operations require an active workspace.'
)
;
}
// For session paths: /memories/session/foo.md → ['memories', 'session', 'foo.md']
// Skip 'memories' and 'session', keep rest
=
.
slice
(
2
)
;
const
=
URI
.
from
(
)
;
let
:
URI
;
if
(
)
{
const
=
extractSessionId
(
)
;
=
.
length
>
0
?
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
,
...
)
:
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
)
;
}
else
{
=
.
length
>
0
?
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
...
)
:
URI
.
joinPath
(
,
MEMORY_BASE_DIR
)
;
}
if
(
!
this
.
memoryCleanupService
.
isMemoryUri
(
)
)
{
throw
new
Error
(
'Resolved path escapes the memory storage directory.'
)
;
}
return
;
}
if
(
===
'repo'
)
{
const
=
this
.
extensionContext
.
storageUri
;
if
(
!
)
{
throw
new
Error
(
'No workspace storage available. Repository memory operations require an active workspace.'
)
;
}
// For repo paths: /memories/repo/foo.md → ['memories', 'repo', 'foo.md']
// Skip 'memories' and 'repo', keep rest
=
.
slice
(
2
)
;
const
=
URI
.
from
(
)
;
const
=
.
length
>
0
?
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
'repo'
,
...
)
:
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
'repo'
)
;
if
(
!
this
.
memoryCleanupService
.
isMemoryUri
(
)
)
{
throw
new
Error
(
'Resolved path escapes the memory storage directory.'
)
;
}
return
;
}
// User scope: /memories/foo.md → ['memories', 'foo.md']
// Skip 'memories', keep rest
=
.
slice
(
1
)
;
const
=
this
.
extensionContext
.
globalStorageUri
;
if
(
!
)
{
throw
new
Error
(
'No global storage available. User memory operations require global storage.'
)
;
}
const
=
.
length
>
0
?
URI
.
joinPath
(
,
MEMORY_BASE_DIR
,
...
)
:
URI
.
joinPath
(
,
MEMORY_BASE_DIR
)
;
return
;
}
private
async
_dispatchLocal
(
:
MemoryToolParams
,
:
MemoryScope
,
?
:
string
)
:
Promise
<
MemoryToolResult
>
{
try
{
switch
(
.
command
)
{
case
'view'
:
return
this
.
_localView
(
.
path
,
.
view_range
,
,
)
;
case
'create'
:
return
this
.
_localCreate
(
,
,
)
;
case
'str_replace'
:
return
this
.
_localStrReplace
(
,
,
)
;
case
'insert'
:
return
this
.
_localInsert
(
,
,
)
;
case
'delete'
:
return
this
.
_localDelete
(
.
path
,
,
)
;
case
'rename'
:
return
this
.
_localRename
(
,
,
)
;
default
:
return
{
:
`Error: Unknown command '${(params as MemoryToolParams).command}'.`
,
:
'error'
}
;
}
}
catch
(
)
{
this
.
logService
.
error
(
'[MemoryTool] Local operation error:'
,
)
;
return
{
:
`Error: ${error.message}`
,
:
'error'
}
;
}
}
private
async
_localView
(
:
string
,
?
:
[
number
,
number
]
,
:
MemoryScope
=
'user'
,
?
:
string
)
:
Promise
<
MemoryToolResult
>
{
// When viewing the top-level /memories/ directory with user scope, merge user + session contents
if
(
===
'user'
&&
isMemoriesRoot
(
)
)
{
return
this
.
_localViewMergedRoot
(
,
)
;
}
const
=
this
.
_resolveUri
(
,
,
)
;
if
(
===
'session'
)
{
this
.
memoryCleanupService
.
markAccessed
(
)
;
}
let
:
.
FileStat
;
try
{
=
await
this
.
fileSystemService
.
stat
(
)
;
}
catch
{
this
.
logService
.
debug
(
`[MemoryTool] Failed to stat ${path}`
)
;
if
(
isMemoriesRoot
(
)
)
{
return
{
:
'No memories found.'
,
:
'notFound'
}
;
}
return
{
:
`No memories found in ${path}.`
,
:
'notFound'
}
;
}
if
(
.
type
===
FileType
.
Directory
)
{
return
{
:
await
this
.
_listDirectory
(
,
)
,
:
'success'
}
;
}
// Read file contents with line numbers
const
=
await
this
.
fileSystemService
.
readFile
(
)
;
const
=
new
TextDecoder
(
)
.
decode
(
)
;
this
.
logService
.
debug
(
`[MemoryTool] Viewed memory file: ${path}`
)
;
if
(
)
{
const
=
.
split
(
'\n'
)
;
const
[
,
]
=
;
if
(
<
1
||
>
.
length
)
{
return
{
:
`Error: Invalid view_range: start line ${start} is out of range [1, ${lines.length}].`
,
:
'error'
}
;
}
if
(
<
||
>
.
length
)
{
return
{
:
`Error: Invalid view_range: end line ${end} is out of range [${start}, ${lines.length}].`
,
:
'error'
}
;
}
const
=
.
slice
(
-
1
,
)
;
const
=
.
map
(
(
,
)
=>
`${formatLineNumber(start + i)}\t${line}`
)
;
return
{
:
`Here's the content of ${path} (lines ${start}-${end}) with line numbers:\n${numbered.join('\n')}`
,
:
'success'
}
;
}
return
{
:
formatFileContent
(
,
)
,
:
'success'
}
;
}
private
async
_localViewMergedRoot
(
:
string
,
?
:
string
)
:
Promise
<
MemoryToolResult
>
{
const
:
string
[
]
=
[
]
;
let
=
false
;
// List user-scoped files
try
{
const
=
this
.
_resolveUri
(
'/memories/'
,
'user'
)
;
const
=
await
this
.
fileSystemService
.
readDirectory
(
)
;
for
(
const
[
,
]
of
)
{
if
(
.
startsWith
(
'.'
)
)
{
continue
;
}
=
true
;
if
(
===
FileType
.
Directory
)
{
.
push
(
`/memories/${name}/`
)
;
}
else
{
.
push
(
`/memories/${name}`
)
;
}
}
}
catch
{
// User storage may not exist yet
}
// List current session's files under session/
if
(
)
{
try
{
const
=
this
.
_resolveUri
(
'/memories/session/'
,
'session'
,
)
;
const
=
await
this
.
fileSystemService
.
readDirectory
(
)
;
const
:
string
[
]
=
[
]
;
for
(
const
[
,
]
of
)
{
if
(
.
startsWith
(
'.'
)
)
{
continue
;
}
if
(
===
FileType
.
File
)
{
.
push
(
`/memories/session/${name}`
)
;
}
}
// Add session/ header if there are files
if
(
.
length
>
0
)
{
=
true
;
.
push
(
'/memories/session/'
)
;
.
push
(
...
)
;
}
else
{
// Add session/ entry even if empty, to show it exists
.
push
(
'/memories/session/'
)
;
}
}
catch
{
// Session storage may not exist yet, but still mention it
.
push
(
'/memories/session/'
)
;
}
}
else
{
// No session resource, still mention session directory exists
.
push
(
'/memories/session/'
)
;
}
// List local repo memory files under repo/
try
{
const
=
this
.
_resolveUri
(
'/memories/repo/'
,
'repo'
)
;
const
=
await
this
.
fileSystemService
.
readDirectory
(
)
;
const
:
string
[
]
=
[
]
;
for
(
const
[
,
]
of
)
{
if
(
.
startsWith
(
'.'
)
)
{
continue
;
}
if
(
===
FileType
.
Directory
)
{
.
push
(
`/memories/repo/${name}/`
)
;
}
else
{
.
push
(
`/memories/repo/${name}`
)
;
}
}
if
(
.
length
>
0
)
{
=
true
;
.
push
(
'/memories/repo/'
)
;
.
push
(
...
)
;
}
else
{
.
push
(
'/memories/repo/'
)
;
}
}
catch
{
// Repo storage may not exist yet, but still mention it
.
push
(
'/memories/repo/'
)
;
}
if
(
!
)
{
return
{
:
'No memories found.'
,
:
'notFound'
}
;
}
return
{
:
`Here are the files and directories in ${path}:\n${lines.join('\n')}`
,
:
'success'
}
;
}
private
async
_listDirectory
(
:
string
,
:
URI
,
:
number
=
2
,
:
number
=
0
)
:
Promise
<
string
>
{
if
(
>=
)
{
return
''
;
}
const
=
await
this
.
fileSystemService
.
readDirectory
(
)
;
const
:
string
[
]
=
[
]
;
// Sort: directories first, then files. Exclude hidden items.
const
=
.
filter
(
(
[
]
)
=>
!
.
startsWith
(
'.'
)
)
.
sort
(
(
[
,
]
,
[
,
]
)
=>
{
if
(
===
FileType
.
Directory
&&
!==
FileType
.
Directory
)
{
return
-
1
;
}
if
(
!==
FileType
.
Directory
&&
===
FileType
.
Directory
)
{
return
1
;
}
return
0
;
}
)
;
for
(
const
[
,
]
of
)
{
const
=
URI
.
joinPath
(
,
)
;
const
=
.
endsWith
(
'/'
)
?
`${path}${name}`
:
`${path}/${name}`
;
const
=
' '
.
repeat
(
)
;
if
(
===
FileType
.
Directory
)
{
.
push
(
`${prefix}${name}/`
)
;
const
=
await
this
.
_listDirectory
(
,
,
,
+
1
)
;
if
(
)
{
.
push
(
)
;
}
}
else
{
try
{
const
=
await
this
.
fileSystemService
.
stat
(
)
;
.
push
(
`${prefix}${stat.size}\t${childPath}`
)
;
}
catch
{
.
push
(
`${prefix}${name}`
)
;
}
}
}
if
(
===
0
)
{
return
`Here are the files and directories up to 2 levels deep in ${path}, excluding hidden items:\n${lines.join('\n')}`