通过


会话 Python 插件迁移指南 - 2025 年 5 月

SessionsPythonPlugin 更新为使用 Azure 代码解释器动态会话 API 的最新版本(2024-10-02-preview)。 新 API 引入了破坏性变更,这些变更反映在插件的公共 API 接口中。

此迁移指南将帮助你将现有代码迁移到最新版本的插件。

插件初始化

插件构造函数签名已更改,现在接受 CancellationToken 作为 authTokenProvider 函数的参数。 此更改允许根据需要取消令牌生成过程:

// Before
static async Task<string> GetAuthTokenAsync()
{
    return tokenProvider.GetToken();
}

// After
static async Task<string> GetAuthTokenAsync(CancellationToken ct)
{
    return tokenProvider.GetToken(ct);
}

var plugin = new SessionsPythonPlugin(settings, httpClientFactory, GetAuthTokenAsync);

UploadFileAsync 方法

方法 UploadFileAsync 签名已更改为更好地表示参数的用途:

// Before
string remoteFilePath = "your_file.txt";
string? localFilePath = "C:\documents\your_file.txt";

await plugin.UploadFileAsync(remoteFilePath: remoteFilePath, localFilePath: localFilePath);

// After
string remoteFileName = "your_file.txt";
string localFilePath = "C:\documents\your_file.txt";

await plugin.UploadFileAsync(remoteFileName: remoteFileName, localFilePath: localFilePath);

DownloadFileAsync 方法

同样,方法 DownloadFileAsync 签名已更改为更好地表示参数的用途:

// Before
string remoteFilePath = "your_file.txt";
await plugin.DownloadFileAsync(remoteFilePath: remoteFilePath);

// After
string remoteFileName = "your_file.txt";
await plugin.DownloadFileAsync(remoteFileName: remoteFileName);

ExecuteCodeAsync 方法

方法 ExecuteCodeAsync 签名已更改,以提供用于处理执行结果的结构化方法:

// Before
string result = await plugin.ExecuteCodeAsync(code: "print('Hello, world!')");

// After
SessionsPythonCodeExecutionResult result = await plugin.ExecuteCodeAsync(code: "print('Hello, world!')");
string status = result.Status;
string? executionResult = result.Result?.ExecutionResult;
string? stdout = result.Result?.StdOut;
string? stderr = result.Result?.StdErr;

SessionsRemoteFileMetadata 类

SessionsRemoteFileMetadata模型类由UploadFileAsyncListFilesAsync方法使用,已更新,以更好地表示远程文件和目录的元数据:

// Before
SessionsRemoteFileMetadata file = await plugin.UploadFileAsync(...);
string fileName = file.Filename;
long fileSize = file.Size;
DateTime? lastModified = file.LastModifiedTime;

// After
SessionsRemoteFileMetadata file = await plugin.UploadFileAsync(...);
string name = file.Name;
long? size = file.SizeInBytes;
DateTime lastModified = file.LastModifiedAt;