Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderInstanceRegistryLive.fixture.mjs

97 lines3.2 KB

1#!/usr/bin/env node
2import * as NodeFS from "node:fs";
3import * as NodeReadline from "node:readline";
4const requiredCwd = process.env.T3_CLAUDE_REQUIRED_CWD;
5if (requiredCwd && process.cwd() !== NodeFS.realpathSync(requiredCwd)) {
6 process.stderr.write(`claude: refusing to run outside ${requiredCwd}\n`);
7 process.exit(1);
8}
9if (process.argv.includes("--version")) {
10 process.stdout.write("claude 2.1.219\n");
11 process.exit(0);
13const loginMarker = process.env.T3_CLAUDE_LOGIN_MARKER;
14const signedOut = loginMarker !== undefined && !NodeFS.existsSync(loginMarker);
15if (process.env.T3_CLAUDE_PROBE_LOG)
16 NodeFS.appendFileSync(process.env.T3_CLAUDE_PROBE_LOG, "probe\n");
17const lines = NodeReadline.createInterface({ input: process.stdin });
18lines.on("line", (line) => {
19 const message = JSON.parse(line);
20 if (message.type !== "control_request") return;
21 if (message.request?.subtype === "get_usage" && signedOut) {
22 process.stdout.write(
23 JSON.stringify({
24 type: "control_response",
25 response: {
26 subtype: "success",
27 request_id: message.request_id,
28 response: { session: {}, rate_limits_available: false, rate_limits: null },
29 },
30 }) + "\n",
31 );
32 return;
33 }
34 if (message.request?.subtype === "get_usage") {
35 const marker = process.env.T3_CLAUDE_RESET_MARKER;
36 const usageFailsUntil = process.env.T3_CLAUDE_USAGE_FAILS_UNTIL;
37 if (
38 (usageFailsUntil && !NodeFS.existsSync(usageFailsUntil)) ||
39 (process.env.T3_CLAUDE_USAGE_FAILS_AFTER_CLAIM && marker && NodeFS.existsSync(marker))
40 ) {
41 process.stdout.write(
42 JSON.stringify({
43 type: "control_response",
44 response: { subtype: "error", request_id: message.request_id, error: "usage failed" },
45 }) + "\n",
46 );
47 return;
48 }
49 process.stdout.write(
50 JSON.stringify({
51 type: "control_response",
52 response: {
53 subtype: "success",
54 request_id: message.request_id,
55 response: {
56 session: {},
57 subscription_type: "pro",
58 rate_limits_available: true,
59 rate_limits: {
60 five_hour: {
61 utilization: marker && NodeFS.existsSync(marker) ? 0 : 100,
62 resets_at: null,
63 },
64 },
65 },
66 },
67 }) + "\n",
68 );
69 return;
70 }
71 if (message.request?.subtype !== "initialize") return;
72 process.stdout.write(
73 JSON.stringify({
74 type: "control_response",
75 response: {
76 subtype: "success",
77 request_id: message.request_id,
78 response: {
79 commands: [],
80 agents: [],
81 models: [],
82 output_style: "default",
83 available_output_styles: ["default"],
84 account: signedOut
85 ? { tokenSource: "none", apiProvider: "firstParty" }
86 : { email: "test@example.com", subscriptionType: "pro", tokenSource: "oauth" },
87 },
88 },
89 }) + "\n",
90 );
91});
92// Stay alive for control requests only while the parent lives; SDK abort does not await child exit.
93const keepAlive = setInterval(() => {}, 1_000);
94lines.on("close", () => {
95 clearInterval(keepAlive);
96 process.exit(0);
97});