☰
Continue SDK Python 客户端 GetFreeTrialStatus200Response 模型详解:免费试用额度与用量查询
2026/10/9 13:30:50 网站建设 项目流程

Continue SDK Python 客户端 GetFreeTrialStatus200Response 模型详解:免费试用额度与用量查询

【免费下载链接】continueopen-source coding agent项目地址: https://gitcode.com/GitHub_Trending/co/continue

GetFreeTrialStatus200Response是 Continue Hub IDE API(OpenAPI 版本 1.0.0)中GET /ide/free-trial-status接口的 200 响应模型,由 OpenAPI Generator 自动生成并随 Continue 官方 Python SDK(openapi_client包)分发。它承载用户在 Continue Hub 免费试用期内的订阅状态、聊天与自动补全(autocomplete)的已用量及上限数据。本文基于仓库内的 模型文档、接口定义、Pydantic 模型实现 与 API 客户端实现,完整讲解该模型的全部字段语义、类型约束、序列化行为与端到端调用方法,读完即可在自己的 Python 项目中正确解析和消费免费试用状态数据。

模型定位:免费试用状态查询的响应载体

Continue SDK 的 Python 包为 IDE 扩展(VS Code、JetBrains)访问 Continue Hub 提供了程序化接口,其 OpenAPI 客户端由 OpenAPI Generator 中查看(API 版本 1.0.0、生成器版本 7.12.0、要求 Python 3.8+)。

GetFreeTrialStatus200Response正是免费试用状态查询的唯一成功响应类型。调用 DefaultApi.get_free_trial_status 方法(对应GET /ide/free-trial-status)后,SDK 会把响应 JSON 反序列化为该模型的实例,开发者无需手工解析字典。

注意:Continue SDK 目前处于实验阶段(详见 SDK README 的 EXPERIMENTAL 提示),接口与模型可能在没有预告的情况下发生破坏性变更,集成时建议锁定版本。

模型属性全览

原文档定义了 5 个字段,其中 3 个为必填(required),2 个为可选(optional),并标注了对应的 JSON 别名(alias):

NameTypeDescriptionNotes
opted_in_to_free_trialboolWhether the user has opted into the free trial必填
chat_countfloatCurrent number of chat messages used可选(nullable)
autocomplete_countfloatCurrent number of autocomplete requests used可选(nullable)
chat_limitfloatMaximum number of chat messages allowed in free trial必填
autocomplete_limitfloatMaximum number of autocomplete requests allowed in free trial必填

对照 OpenAPI 原始定义,可以确认:

  • 必填字段为optedInToFreeTrial、chatLimit、autocompleteLimit三个,若响应缺少它们,Pydantic 校验会直接失败;
  • 可选字段chatCount、autocompleteCount在 schema 中标记为nullable: true,表示服务端可能显式返回null;
  • 五个字段在 JSON 层面均使用camelCase 别名(optedInToFreeTrial、chatCount、autocompleteCount、chatLimit、autocompleteLimit),而 Python 模型属性为snake_case。

各字段语义解读

  • opted_in_to_free_trial(bool,必填):用户是否已主动选择加入免费试用。它是判断免费试用资格的总开关;如果为false,通常意味着后续计数字段无实际意义。
  • chat_count(float,可选):当前已消耗的聊天消息条数。类型声明为float,但实际底层约束是“数字”(number),既可以是整数也可以是浮点数(详见下文模型实现)。
  • autocomplete_count(float,可选):当前已消耗的自动补全请求次数。
  • chat_limit(float,必填):免费试用期内允许的聊天消息条数上限,用于与chat_count相减得到剩余额度。
  • autocomplete_limit(float,必填):免费试用期内允许的自动补全请求次数上限。

源码视角:Pydantic 模型实现细节

模型的完整实现位于 get_free_trial_status200_response.py,其中几处细节值得注意:

1. 字段声明与 JSON 别名

class GetFreeTrialStatus200Response(BaseModel): opted_in_to_free_trial: StrictBool = Field( description="Whether the user has opted into the free trial", alias="optedInToFreeTrial", ) chat_count: Optional[Union[StrictFloat, StrictInt]] = Field( default=None, description="Current number of chat messages used", alias="chatCount" ) autocomplete_count: Optional[Union[StrictFloat, StrictInt]] = Field( default=None, description="Current number of autocomplete requests used", alias="autocompleteCount", ) chat_limit: Union[StrictFloat, StrictInt] = Field( description="Maximum number of chat messages allowed in free trial", alias="chatLimit" ) autocomplete_limit: Union[StrictFloat, StrictInt] = Field( description="Maximum number of autocomplete requests allowed in free trial", alias="autocompleteLimit", )
  • 数字字段类型是Union[StrictFloat, StrictInt],即 JSON 中的整数(如42)和浮点数(如42.5)都会被接受并保持原样,这解释了为何文档中类型标注为float但实际接受整数;
  • 可选字段显式设置default=None,在响应未携带该字段时不会触发校验错误;
  • model_config开启了populate_by_name=True,意味着构造实例时既可以用 snake_case(chat_count)也可以用 camelCase(chatCount)作为关键字参数;
  • validate_assignment=True保证实例创建后的属性赋值同样经过类型校验。

2. 可选字段的 null 处理

to_dict()方法对两个可空字段做了特殊处理:正常情况下None值会被exclude_none=True过滤掉,但如果用户显式将chat_count或autocomplete_count赋值为None(即该字段被设置过),to_dict()仍会在输出字典中保留"chatCount": None这一键。这一行为保证客户端可以区分“字段缺失”与“服务端明确返回 null”两种情况。

从 JSON 与字典创建实例:官方示例逐行解析

原文档给出了完整的模型使用示例(位于 GetFreeTrialStatus200Response.md),完整继承了该示例并补充注释如下:

from openapi_client.models.get_free_trial_status200_response import GetFreeTrialStatus200Response # 用一个 JSON 字符串创建模型实例 json = "{}" # TODO: 将上面的 json 字符串替换为真实的响应体,例如: # json = '{"optedInToFreeTrial": true, "chatCount": 3, "chatLimit": 50, "autocompleteCount": 12, "autocompleteLimit": 100}' get_free_trial_status200_response_instance = GetFreeTrialStatus200Response.from_json(json) # 打印模型的 JSON 字符串表示(输出使用 camelCase 别名) print(GetFreeTrialStatus200Response.to_json()) # 将模型对象转换为 dict get_free_trial_status200_response_dict = get_free_trial_status200_response_instance.to_dict() # 从 dict 创建模型实例 get_free_trial_status200_response_from_dict = GetFreeTrialStatus200Response.from_dict(get_free_trial_status200_response_dict)

调用链说明(可对照 模型源码):

  • from_json(json_str)内部先json.loads解析字符串,再委托给from_dict;
  • from_dict(obj)通过cls.model_validate({...})按 camelCase 键逐一取值并交给 Pydantic 校验,缺失的可选字段自动取None;
  • to_json()/to_dict()都使用by_alias=True,输出键为 camelCase 形式,与服务端响应保持一致,可直接用于调试或二次传输。

端到端实战:完整调用免费试用状态接口

模型通常不会独立使用,而是作为get_free_trial_status接口的返回值被消费。完整调用流程如下(整理自 DefaultApi.md 与 API README 的示例):

import os import openapi_client from openapi_client.models.get_free_trial_status200_response import GetFreeTrialStatus200Response from openapi_client.rest import ApiException from pprint import pprint # 1. 配置服务端地址(可选,默认 https://api.continue.dev) configuration = openapi_client.Configuration( host="https://api.continue.dev" ) # 2. 配置 Bearer 认证(apiKeyAuth) configuration = openapi_client.Configuration( access_token=os.environ["BEARER_TOKEN"] ) # 3. 进入 API 客户端上下文并调用接口 with openapi_client.ApiClient(configuration) as api_client: api_instance = openapi_client.DefaultApi(api_client) try: # 该方法不需要任何参数 api_response = api_instance.get_free_trial_status() print("The response of DefaultApi->get_free_trial_status:\n") pprint(api_response) # 直接访问模型属性 print("opted in:", api_response.opted_in_to_free_trial) print("chat used:", api_response.chat_count, "/", api_response.chat_limit) print("autocomplete used:", api_response.autocomplete_count, "/", api_response.autocomplete_limit) # 计算剩余额度 if api_response.opted_in_to_free_trial and api_response.chat_count is not None: remaining_chat = api_response.chat_limit - api_response.chat_count print("remaining chat messages:", remaining_chat) except Exception as e: print("Exception when calling DefaultApi->get_free_trial_status: %s\n" % e)

接口契约要点

  • HTTP 方法与路径:GET /ide/free-trial-status(可在 default_api.py 的_get_free_trial_status_serialize中确认resource_path='/ide/free-trial-status');
  • 认证方式:apiKeyAuth(Bearer token),通过Configuration.access_token注入,未认证会返回 401;
  • 请求参数:无;
  • 成功响应(200):反序列化为GetFreeTrialStatus200Response;
  • 错误响应(404):User not found,客户端会尝试反序列化为ListAssistants404Response(见 default_api.py);
  • 响应头:Accept: application/json,请求体类型未定义。

典型业务场景

拿到模型实例后,常见的业务判断逻辑包括:

  1. 展示试用状态:用opted_in_to_free_trial决定是否在界面上显示免费试用入口;
  2. 额度预警:当chat_count / chat_limit接近 1 时提示用户即将用尽额度;
  3. 用量统计看板:将chat_count、autocomplete_count与各自 limit 组合成进度条。

测试佐证:字段的可选性契约

仓库内置的单元测试桩 test_get_free_trial_status200_response.py 中,make_instance(include_optional=False)仅构造三个必填字段(opted_in_to_free_trial、chat_limit、autocomplete_limit),而include_optional=True时才补齐chat_count、autocomplete_count。这一测试结构与 OpenAPI schema 的required列表完全一致,印证了:两个 count 字段缺省时模型依然合法,开发者在消费数据时必须对其可能为None的情况做防御性处理(例如上例中的is not None判断)。

小结

GetFreeTrialStatus200Response虽然只是 Continue Hub IDE API 中的一个响应模型,却是理解 Continue 免费试用配额机制的最小入口。通过本文可以掌握:五个字段(两个必填布尔开关 + 两对 usage/limit)的完整语义、camelCase 别名与 snake_case 属性之间的映射、nullable 字段在to_dict()中的特殊行为,以及从鉴权配置到get_free_trial_status()调用的完整代码路径。相关源码与文档可进一步查阅:模型实现、接口定义、DefaultApi 文档。

【免费下载链接】continueopen-source coding agent项目地址: https://gitcode.com/GitHub_Trending/co/continue

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询