获取 Session

更新时间:
复制 MD 格式

返回会话元数据、嵌入的智能体快照与资源列表。事件历史与消息内容通过事件 API 获取。

前提

已完成 Endpoint 与鉴权配置,详见API 总览与认证。

接口

GET/sessions/{session_id}

路径参数

session_idstring必填

Session ID,格式 sesn_<ULID>

响应字段

响应为 Session 对象。字段如下:

idstring

会话 ID,格式 sesn_<ULID>

typestring

固定为 session

statusstring

会话状态:idle / running / terminated

stop_reasonobject / null

会话回到 idle 的原因,判别联合。null(running,或 idle 刚创建尚未产生结束原因)/ {"type":"end_turn"}(模型主动结束)/ {"type":"retries_exhausted"}(重试耗尽)/ {"type":"requires_action","pending_batch_id":"...","pending_call_ids":[...]}(等待工具审批,pending_call_ids 为待裁决调用列表)。仅在 status=idle 时用于判断交互状态;running 时为 null;terminated 是终态,客户端不应依赖其 stop_reason

typestring

end_turn / requires_action / retries_exhausted

pending_batch_idstring

待处理的工具审批批次 ID

pending_call_idsarray<string>

待裁决的工具调用 ID 列表

agentobject

智能体配置完整快照,含 id / version / name / model / system / tools 等;会话级覆盖(若已设置)已并入快照

idstring

Agent ID,格式 agent_xxx

versioninteger

要锁定的版本号;不传使用最新版本

environment_idstring

绑定的运行环境 ID

sandboxobject

沙箱快照持久化配置与当前快照状态;未配置时省略

enable_snapshotboolean

当前 Session 是否开启快照持久化

snapshot_persistent_secondsinteger

单份快照配置的保留时间,单位秒;未配置时不返回

is_persistingboolean

当前 Session 是否正在保留可用快照;尚未取得统计时可能不返回

predict_release_timestring / null

当前快照预计释放时间,ISO 8601 UTC;没有当前快照时为 null

usage.sandboxobject

沙箱快照时长统计。创建响应中可能省略;已开启快照但尚未生成快照时,内部时长字段为 null。

session_total_persisted_hoursinteger / null

历史快照累计保留小时数;尚无统计时为 null

latest_snapshot_persisted_hoursinteger / null

当前最新快照已保留小时数;没有当前快照时为 null

titlestring

会话标题

metadataobject

业务自定义键值

resourcesarray of object

挂载的资源列表。按 type 区分文件、记忆库和 Git 仓库;Git 仓库不返回 authorization_token。

  • id:资源 ID,格式 sesrsc_<ULID>
  • type:file / memory_store / git_repository
  • file_id:文件资源返回内部副本 ID;记忆库资源为 null
  • memory_store_id / name / access / instructions:记忆库资源的元数据与挂载配置
  • url:Git 仓库的 HTTPS 地址,不含凭据
  • mount_path:挂载路径。文件带 /mnt/session/uploads 前缀;Git 仓库为实际检出路径,例如 /workspace/project
  • checkout:Git 仓库创建时指定的检出目标,包含 type 与 name(分支)或 sha(提交);省略时为 null
  • created_at / updated_at:资源创建与最近更新时间,ISO 8601
archived_atstring / null

归档时间,未归档时为 null

created_atstring

创建时间,ISO 8601

updated_atstring

最近更新时间,ISO 8601

environment_variablesobject

会话运行时注入的环境变量,字符串键值对,沙箱代码中可直接按名读取

vault_idsarray of string

绑定的 Vault ID 列表

mcp_configsarray of object

会话级 MCP 请求头,每项含 mcp_server_name 与 headers;未配置时为空数组

providerstring

default 或 agentstudio_memory

entity_idstring

百炼记忆的挂载绑定实体 ID

project_idsarray<string>

百炼记忆项目 ID;省略或为空时使用默认规则

profile_schemastring

用户画像模板 ID

memory_store_idstring

记忆库 ID

accessstring

记忆库访问权限

instructionsstring

记忆使用说明

请求示例

curl "$AGENTSTUDIO_URL/sessions/sesn_xxx" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY"
session = client.sessions.retrieve("sesn_xxx")
print(session.id, session.status, session.title)
Session session = client.sessions().retrieve("sesn_xxx");
System.out.println(session.getId() + " " + session.getStatus() + " " + session.getTitle());

响应示例

{
  "id": "sesn_xxx",
  "type": "session",
  "status": "idle",
  "stop_reason": {
    "type": "requires_action",
    "pending_batch_id": "response_xxx:9f2c...",
    "pending_call_ids": [
      "call_xxx"
    ]
  },
  "agent": {
    "id": "agent_xxx",
    "type": "agent",
    "version": 1,
    "name": "data-analyst",
    "description": null,
    "model": {"id": "qwen3.7-max"},
    "system": "你是数据分析专家,使用 pandas 处理 CSV 文件。",
    "tools": []
  },
  "environment_id": "env_xxx",
  "title": "Q3 销售数据分析",
  "resources": [
    {
      "id": "sesrsc_zzz",
      "type": "git_repository",
      "url": "https://gitlab.com/group/project",
      "mount_path": "/workspace/project",
      "checkout": {"type": "branch", "name": "main"},
      "created_at": "2026-11-28T02:00:55Z",
      "updated_at": "2026-11-28T02:00:55Z"
    }
  ],
  "metadata": {"biz_ticket_id": "1234"},
  "archived_at": null,
  "created_at": "2026-05-28T08:23:11Z",
  "updated_at": "2026-05-28T08:23:25Z",
  "request_id": "xxx",
  "environment_variables": {"API_BASE_URL": "https://api.example.com", "LOG_LEVEL": "info"},
  "vault_ids": ["vlt_xxx"],
  "mcp_configs": [{"mcp_server_name": "crm", "headers": {"X-Tenant-Id": "tenant-demo"}}]
}

沙箱快照响应示例

当前存在可用快照时:

{
  "sandbox": {
    "enable_snapshot": true,
    "snapshot_persistent_seconds": 172800,
    "is_persisting": true,
    "predict_release_time": "2026-11-17T06:09:50Z"
  },
  "usage": {
    "sandbox": {
      "session_total_persisted_hours": 3,
      "latest_snapshot_persisted_hours": 1
    }
  }
}

已开启快照但尚未生成快照时:

{
  "sandbox": {
    "enable_snapshot": true,
    "snapshot_persistent_seconds": 172800,
    "is_persisting": false,
    "predict_release_time": null
  },
  "usage": {
    "sandbox": {
      "session_total_persisted_hours": null,
      "latest_snapshot_persisted_hours": null
    }
  }
}