phi-code-ai 0.56.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/README.md +1198 -0
  2. package/bedrock-provider.d.ts +1 -0
  3. package/bedrock-provider.js +1 -0
  4. package/dist/api-registry.d.ts +20 -0
  5. package/dist/api-registry.d.ts.map +1 -0
  6. package/dist/api-registry.js +44 -0
  7. package/dist/api-registry.js.map +1 -0
  8. package/dist/bedrock-provider.d.ts +5 -0
  9. package/dist/bedrock-provider.d.ts.map +1 -0
  10. package/dist/bedrock-provider.js +6 -0
  11. package/dist/bedrock-provider.js.map +1 -0
  12. package/dist/cli.d.ts +3 -0
  13. package/dist/cli.d.ts.map +1 -0
  14. package/dist/cli.js +116 -0
  15. package/dist/cli.js.map +1 -0
  16. package/dist/env-api-keys.d.ts +9 -0
  17. package/dist/env-api-keys.d.ts.map +1 -0
  18. package/dist/env-api-keys.js +104 -0
  19. package/dist/env-api-keys.js.map +1 -0
  20. package/dist/index.d.ts +23 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +21 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/models.d.ts +24 -0
  25. package/dist/models.d.ts.map +1 -0
  26. package/dist/models.generated.d.ts +13572 -0
  27. package/dist/models.generated.d.ts.map +1 -0
  28. package/dist/models.generated.js +13368 -0
  29. package/dist/models.generated.js.map +1 -0
  30. package/dist/models.js +55 -0
  31. package/dist/models.js.map +1 -0
  32. package/dist/oauth.d.ts +2 -0
  33. package/dist/oauth.d.ts.map +1 -0
  34. package/dist/oauth.js +2 -0
  35. package/dist/oauth.js.map +1 -0
  36. package/dist/providers/amazon-bedrock.d.ts +15 -0
  37. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  38. package/dist/providers/amazon-bedrock.js +597 -0
  39. package/dist/providers/amazon-bedrock.js.map +1 -0
  40. package/dist/providers/anthropic.d.ts +33 -0
  41. package/dist/providers/anthropic.d.ts.map +1 -0
  42. package/dist/providers/anthropic.js +729 -0
  43. package/dist/providers/anthropic.js.map +1 -0
  44. package/dist/providers/azure-openai-responses.d.ts +15 -0
  45. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  46. package/dist/providers/azure-openai-responses.js +184 -0
  47. package/dist/providers/azure-openai-responses.js.map +1 -0
  48. package/dist/providers/github-copilot-headers.d.ts +8 -0
  49. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  50. package/dist/providers/github-copilot-headers.js +29 -0
  51. package/dist/providers/github-copilot-headers.js.map +1 -0
  52. package/dist/providers/google-gemini-cli.d.ts +74 -0
  53. package/dist/providers/google-gemini-cli.d.ts.map +1 -0
  54. package/dist/providers/google-gemini-cli.js +754 -0
  55. package/dist/providers/google-gemini-cli.js.map +1 -0
  56. package/dist/providers/google-shared.d.ts +65 -0
  57. package/dist/providers/google-shared.d.ts.map +1 -0
  58. package/dist/providers/google-shared.js +300 -0
  59. package/dist/providers/google-shared.js.map +1 -0
  60. package/dist/providers/google-vertex.d.ts +15 -0
  61. package/dist/providers/google-vertex.d.ts.map +1 -0
  62. package/dist/providers/google-vertex.js +371 -0
  63. package/dist/providers/google-vertex.js.map +1 -0
  64. package/dist/providers/google.d.ts +13 -0
  65. package/dist/providers/google.d.ts.map +1 -0
  66. package/dist/providers/google.js +352 -0
  67. package/dist/providers/google.js.map +1 -0
  68. package/dist/providers/mistral.d.ts +22 -0
  69. package/dist/providers/mistral.d.ts.map +1 -0
  70. package/dist/providers/mistral.js +495 -0
  71. package/dist/providers/mistral.js.map +1 -0
  72. package/dist/providers/openai-codex-responses.d.ts +9 -0
  73. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  74. package/dist/providers/openai-codex-responses.js +701 -0
  75. package/dist/providers/openai-codex-responses.js.map +1 -0
  76. package/dist/providers/openai-completions.d.ts +15 -0
  77. package/dist/providers/openai-completions.d.ts.map +1 -0
  78. package/dist/providers/openai-completions.js +702 -0
  79. package/dist/providers/openai-completions.js.map +1 -0
  80. package/dist/providers/openai-responses-shared.d.ts +17 -0
  81. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  82. package/dist/providers/openai-responses-shared.js +442 -0
  83. package/dist/providers/openai-responses-shared.js.map +1 -0
  84. package/dist/providers/openai-responses.d.ts +13 -0
  85. package/dist/providers/openai-responses.d.ts.map +1 -0
  86. package/dist/providers/openai-responses.js +198 -0
  87. package/dist/providers/openai-responses.js.map +1 -0
  88. package/dist/providers/register-builtins.d.ts +10 -0
  89. package/dist/providers/register-builtins.d.ts.map +1 -0
  90. package/dist/providers/register-builtins.js +138 -0
  91. package/dist/providers/register-builtins.js.map +1 -0
  92. package/dist/providers/simple-options.d.ts +8 -0
  93. package/dist/providers/simple-options.d.ts.map +1 -0
  94. package/dist/providers/simple-options.js +35 -0
  95. package/dist/providers/simple-options.js.map +1 -0
  96. package/dist/providers/transform-messages.d.ts +8 -0
  97. package/dist/providers/transform-messages.d.ts.map +1 -0
  98. package/dist/providers/transform-messages.js +155 -0
  99. package/dist/providers/transform-messages.js.map +1 -0
  100. package/dist/stream.d.ts +8 -0
  101. package/dist/stream.d.ts.map +1 -0
  102. package/dist/stream.js +27 -0
  103. package/dist/stream.js.map +1 -0
  104. package/dist/types.d.ts +284 -0
  105. package/dist/types.d.ts.map +1 -0
  106. package/dist/types.js +2 -0
  107. package/dist/types.js.map +1 -0
  108. package/dist/utils/event-stream.d.ts +21 -0
  109. package/dist/utils/event-stream.d.ts.map +1 -0
  110. package/dist/utils/event-stream.js +77 -0
  111. package/dist/utils/event-stream.js.map +1 -0
  112. package/dist/utils/hash.d.ts +3 -0
  113. package/dist/utils/hash.d.ts.map +1 -0
  114. package/dist/utils/hash.js +14 -0
  115. package/dist/utils/hash.js.map +1 -0
  116. package/dist/utils/json-parse.d.ts +9 -0
  117. package/dist/utils/json-parse.d.ts.map +1 -0
  118. package/dist/utils/json-parse.js +29 -0
  119. package/dist/utils/json-parse.js.map +1 -0
  120. package/dist/utils/oauth/anthropic.d.ts +17 -0
  121. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  122. package/dist/utils/oauth/anthropic.js +104 -0
  123. package/dist/utils/oauth/anthropic.js.map +1 -0
  124. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  125. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  126. package/dist/utils/oauth/github-copilot.js +281 -0
  127. package/dist/utils/oauth/github-copilot.js.map +1 -0
  128. package/dist/utils/oauth/google-antigravity.d.ts +26 -0
  129. package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
  130. package/dist/utils/oauth/google-antigravity.js +373 -0
  131. package/dist/utils/oauth/google-antigravity.js.map +1 -0
  132. package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
  133. package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
  134. package/dist/utils/oauth/google-gemini-cli.js +478 -0
  135. package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
  136. package/dist/utils/oauth/index.d.ts +61 -0
  137. package/dist/utils/oauth/index.d.ts.map +1 -0
  138. package/dist/utils/oauth/index.js +131 -0
  139. package/dist/utils/oauth/index.js.map +1 -0
  140. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  141. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  142. package/dist/utils/oauth/openai-codex.js +380 -0
  143. package/dist/utils/oauth/openai-codex.js.map +1 -0
  144. package/dist/utils/oauth/pkce.d.ts +13 -0
  145. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  146. package/dist/utils/oauth/pkce.js +31 -0
  147. package/dist/utils/oauth/pkce.js.map +1 -0
  148. package/dist/utils/oauth/types.d.ts +47 -0
  149. package/dist/utils/oauth/types.d.ts.map +1 -0
  150. package/dist/utils/oauth/types.js +2 -0
  151. package/dist/utils/oauth/types.js.map +1 -0
  152. package/dist/utils/overflow.d.ts +52 -0
  153. package/dist/utils/overflow.d.ts.map +1 -0
  154. package/dist/utils/overflow.js +116 -0
  155. package/dist/utils/overflow.js.map +1 -0
  156. package/dist/utils/sanitize-unicode.d.ts +22 -0
  157. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  158. package/dist/utils/sanitize-unicode.js +26 -0
  159. package/dist/utils/sanitize-unicode.js.map +1 -0
  160. package/dist/utils/typebox-helpers.d.ts +17 -0
  161. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  162. package/dist/utils/typebox-helpers.js +21 -0
  163. package/dist/utils/typebox-helpers.js.map +1 -0
  164. package/dist/utils/validation.d.ts +18 -0
  165. package/dist/utils/validation.d.ts.map +1 -0
  166. package/dist/utils/validation.js +72 -0
  167. package/dist/utils/validation.js.map +1 -0
  168. package/package.json +80 -0
@@ -0,0 +1,77 @@
1
+ // Generic event stream class for async iteration
2
+ export class EventStream {
3
+ constructor(isComplete, extractResult) {
4
+ this.isComplete = isComplete;
5
+ this.extractResult = extractResult;
6
+ this.queue = [];
7
+ this.waiting = [];
8
+ this.done = false;
9
+ this.finalResultPromise = new Promise((resolve) => {
10
+ this.resolveFinalResult = resolve;
11
+ });
12
+ }
13
+ push(event) {
14
+ if (this.done)
15
+ return;
16
+ if (this.isComplete(event)) {
17
+ this.done = true;
18
+ this.resolveFinalResult(this.extractResult(event));
19
+ }
20
+ // Deliver to waiting consumer or queue it
21
+ const waiter = this.waiting.shift();
22
+ if (waiter) {
23
+ waiter({ value: event, done: false });
24
+ }
25
+ else {
26
+ this.queue.push(event);
27
+ }
28
+ }
29
+ end(result) {
30
+ this.done = true;
31
+ if (result !== undefined) {
32
+ this.resolveFinalResult(result);
33
+ }
34
+ // Notify all waiting consumers that we're done
35
+ while (this.waiting.length > 0) {
36
+ const waiter = this.waiting.shift();
37
+ waiter({ value: undefined, done: true });
38
+ }
39
+ }
40
+ async *[Symbol.asyncIterator]() {
41
+ while (true) {
42
+ if (this.queue.length > 0) {
43
+ yield this.queue.shift();
44
+ }
45
+ else if (this.done) {
46
+ return;
47
+ }
48
+ else {
49
+ const result = await new Promise((resolve) => this.waiting.push(resolve));
50
+ if (result.done)
51
+ return;
52
+ yield result.value;
53
+ }
54
+ }
55
+ }
56
+ result() {
57
+ return this.finalResultPromise;
58
+ }
59
+ }
60
+ export class AssistantMessageEventStream extends EventStream {
61
+ constructor() {
62
+ super((event) => event.type === "done" || event.type === "error", (event) => {
63
+ if (event.type === "done") {
64
+ return event.message;
65
+ }
66
+ else if (event.type === "error") {
67
+ return event.error;
68
+ }
69
+ throw new Error("Unexpected event type for final result");
70
+ });
71
+ }
72
+ }
73
+ /** Factory function for AssistantMessageEventStream (for use in extensions) */
74
+ export function createAssistantMessageEventStream() {
75
+ return new AssistantMessageEventStream();
76
+ }
77
+ //# sourceMappingURL=event-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-stream.js","sourceRoot":"","sources":["../../src/utils/event-stream.ts"],"names":[],"mappings":"AAEA,iDAAiD;AACjD,MAAM,OAAO,WAAW;IAOvB,YACS,UAAiC,EACjC,aAA8B;QAD9B,eAAU,GAAV,UAAU,CAAuB;QACjC,kBAAa,GAAb,aAAa,CAAiB;QAR/B,UAAK,GAAQ,EAAE,CAAC;QAChB,YAAO,GAA2C,EAAE,CAAC;QACrD,SAAI,GAAG,KAAK,CAAC;QAQpB,IAAI,CAAC,kBAAkB,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACjD,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC;QACnC,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,KAAQ;QACZ,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO;QAEtB,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;QACpD,CAAC;QAED,0CAA0C;QAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC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AC,kBAAkB,CAAC;IAChC,CAAC;CACD;AAED,MAAM,OAAO,2BAA4B,SAAQ,WAAoD;IACpG;QACC,KAAK,CACJ,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAC1D,CAAC,KAAK,EAAE,EAAE;YACT,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC3B,OAAO,KAAK,CAAC,OAAO,CAAC;YACtB,CAAC;iBAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBACnC,OAAO,KAAK,CAAC,KAAK,CAAC;YACpB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;QAC3D,CAAC,CACD,CAAC;IACH,CAAC;CACD;AAED,+EAA+E;AAC/E,MAAM,UAAU,iCAAiC;IAChD,OAAO,IAAI,2BAA2B,EAAE,CAAC;AAC1C,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent } from \"../types.js\";\n\n// Generic event stream class for async iteration\nexport class EventStream<T, R = T> implements AsyncIterable<T> {\n\tprivate queue: T[] = [];\n\tprivate waiting: ((value: IteratorResult<T>) => void)[] = [];\n\tprivate done = false;\n\tprivate finalResultPromise: Promise<R>;\n\tprivate resolveFinalResult!: (result: R) => void;\n\n\tconstructor(\n\t\tprivate isComplete: (event: T) => boolean,\n\t\tprivate extractResult: (event: T) => R,\n\t) {\n\t\tthis.finalResultPromise = new Promise((resolve) => {\n\t\t\tthis.resolveFinalResult = resolve;\n\t\t});\n\t}\n\n\tpush(event: T): void {\n\t\tif (this.done) return;\n\n\t\tif (this.isComplete(event)) {\n\t\t\tthis.done = true;\n\t\t\tthis.resolveFinalResult(this.extractResult(event));\n\t\t}\n\n\t\t// Deliver to waiting consumer or queue it\n\t\tconst waiter = this.waiting.shift();\n\t\tif (waiter) {\n\t\t\twaiter({ value: event, done: false });\n\t\t} else {\n\t\t\tthis.queue.push(event);\n\t\t}\n\t}\n\n\tend(result?: R): void {\n\t\tthis.done = true;\n\t\tif (result !== undefined) {\n\t\t\tthis.resolveFinalResult(result);\n\t\t}\n\t\t// Notify all waiting consumers that we're done\n\t\twhile (this.waiting.length > 0) {\n\t\t\tconst waiter = this.waiting.shift()!;\n\t\t\twaiter({ value: undefined as any, done: true });\n\t\t}\n\t}\n\n\tasync *[Symbol.asyncIterator](): AsyncIterator<T> {\n\t\twhile (true) {\n\t\t\tif (this.queue.length > 0) {\n\t\t\t\tyield this.queue.shift()!;\n\t\t\t} else if (this.done) {\n\t\t\t\treturn;\n\t\t\t} else {\n\t\t\t\tconst result = await new Promise<IteratorResult<T>>((resolve) => this.waiting.push(resolve));\n\t\t\t\tif (result.done) return;\n\t\t\t\tyield result.value;\n\t\t\t}\n\t\t}\n\t}\n\n\tresult(): Promise<R> {\n\t\treturn this.finalResultPromise;\n\t}\n}\n\nexport class AssistantMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {\n\tconstructor() {\n\t\tsuper(\n\t\t\t(event) => event.type === \"done\" || event.type === \"error\",\n\t\t\t(event) => {\n\t\t\t\tif (event.type === \"done\") {\n\t\t\t\t\treturn event.message;\n\t\t\t\t} else if (event.type === \"error\") {\n\t\t\t\t\treturn event.error;\n\t\t\t\t}\n\t\t\t\tthrow new Error(\"Unexpected event type for final result\");\n\t\t\t},\n\t\t);\n\t}\n}\n\n/** Factory function for AssistantMessageEventStream (for use in extensions) */\nexport function createAssistantMessageEventStream(): AssistantMessageEventStream {\n\treturn new AssistantMessageEventStream();\n}\n"]}
@@ -0,0 +1,3 @@
1
+ /** Fast deterministic hash to shorten long strings */
2
+ export declare function shortHash(str: string): string;
3
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/utils/hash.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAW7C"}
@@ -0,0 +1,14 @@
1
+ /** Fast deterministic hash to shorten long strings */
2
+ export function shortHash(str) {
3
+ let h1 = 0xdeadbeef;
4
+ let h2 = 0x41c6ce57;
5
+ for (let i = 0; i < str.length; i++) {
6
+ const ch = str.charCodeAt(i);
7
+ h1 = Math.imul(h1 ^ ch, 2654435761);
8
+ h2 = Math.imul(h2 ^ ch, 1597334677);
9
+ }
10
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
11
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
12
+ return (h2 >>> 0).toString(36) + (h1 >>> 0).toString(36);
13
+ }
14
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/utils/hash.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,MAAM,UAAU,SAAS,CAAC,GAAW;IACpC,IAAI,EAAE,GAAG,UAAU,CAAC;IACpB,IAAI,EAAE,GAAG,UAAU,CAAC;IACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,EAAE,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC7B,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;QACpC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;IACrC,CAAC;IACD,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;IACvF,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;IACvF,OAAO,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC1D,CAAC","sourcesContent":["/** Fast deterministic hash to shorten long strings */\nexport function shortHash(str: string): string {\n\tlet h1 = 0xdeadbeef;\n\tlet h2 = 0x41c6ce57;\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst ch = str.charCodeAt(i);\n\t\th1 = Math.imul(h1 ^ ch, 2654435761);\n\t\th2 = Math.imul(h2 ^ ch, 1597334677);\n\t}\n\th1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);\n\th2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);\n\treturn (h2 >>> 0).toString(36) + (h1 >>> 0).toString(36);\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Attempts to parse potentially incomplete JSON during streaming.
3
+ * Always returns a valid object, even if the JSON is incomplete.
4
+ *
5
+ * @param partialJson The partial JSON string from streaming
6
+ * @returns Parsed object or empty object if parsing fails
7
+ */
8
+ export declare function parseStreamingJson<T = any>(partialJson: string | undefined): T;
9
+ //# sourceMappingURL=json-parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-parse.d.ts","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,GAAG,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,CAAC,CAkB9E"}
@@ -0,0 +1,29 @@
1
+ import { parse as partialParse } from "partial-json";
2
+ /**
3
+ * Attempts to parse potentially incomplete JSON during streaming.
4
+ * Always returns a valid object, even if the JSON is incomplete.
5
+ *
6
+ * @param partialJson The partial JSON string from streaming
7
+ * @returns Parsed object or empty object if parsing fails
8
+ */
9
+ export function parseStreamingJson(partialJson) {
10
+ if (!partialJson || partialJson.trim() === "") {
11
+ return {};
12
+ }
13
+ // Try standard parsing first (fastest for complete JSON)
14
+ try {
15
+ return JSON.parse(partialJson);
16
+ }
17
+ catch {
18
+ // Try partial-json for incomplete JSON
19
+ try {
20
+ const result = partialParse(partialJson);
21
+ return (result ?? {});
22
+ }
23
+ catch {
24
+ // If all parsing fails, return empty object
25
+ return {};
26
+ }
27
+ }
28
+ }
29
+ //# sourceMappingURL=json-parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-parse.js","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAU,WAA+B;IAC1E,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,OAAO,EAAO,CAAC;IAChB,CAAC;IAED,yDAAyD;IACzD,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAM,CAAC;IACrC,CAAC;IAAC,MAAM,CAAC;QACR,uCAAuC;QACvC,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;YACzC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAM,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACR,4CAA4C;YAC5C,OAAO,EAAO,CAAC;QAChB,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { parse as partialParse } from \"partial-json\";\n\n/**\n * Attempts to parse potentially incomplete JSON during streaming.\n * Always returns a valid object, even if the JSON is incomplete.\n *\n * @param partialJson The partial JSON string from streaming\n * @returns Parsed object or empty object if parsing fails\n */\nexport function parseStreamingJson<T = any>(partialJson: string | undefined): T {\n\tif (!partialJson || partialJson.trim() === \"\") {\n\t\treturn {} as T;\n\t}\n\n\t// Try standard parsing first (fastest for complete JSON)\n\ttry {\n\t\treturn JSON.parse(partialJson) as T;\n\t} catch {\n\t\t// Try partial-json for incomplete JSON\n\t\ttry {\n\t\t\tconst result = partialParse(partialJson);\n\t\t\treturn (result ?? {}) as T;\n\t\t} catch {\n\t\t\t// If all parsing fails, return empty object\n\t\t\treturn {} as T;\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Anthropic OAuth flow (Claude Pro/Max)
3
+ */
4
+ import type { OAuthCredentials, OAuthProviderInterface } from "./types.js";
5
+ /**
6
+ * Login with Anthropic OAuth (device code flow)
7
+ *
8
+ * @param onAuthUrl - Callback to handle the authorization URL (e.g., open browser)
9
+ * @param onPromptCode - Callback to prompt user for the authorization code
10
+ */
11
+ export declare function loginAnthropic(onAuthUrl: (url: string) => void, onPromptCode: () => Promise<string>): Promise<OAuthCredentials>;
12
+ /**
13
+ * Refresh Anthropic OAuth token
14
+ */
15
+ export declare function refreshAnthropicToken(refreshToken: string): Promise<OAuthCredentials>;
16
+ export declare const anthropicOAuthProvider: OAuthProviderInterface;
17
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/utils/oauth/anthropic.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAuB,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAShG;;;;;GAKG;AACH,wBAAsB,cAAc,CACnC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAChC,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GACjC,OAAO,CAAC,gBAAgB,CAAC,CA8D3B;AAED;;GAEG;AACH,wBAAsB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA2B3F;AAED,eAAO,MAAM,sBAAsB,EAAE,sBAkBpC,CAAC"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Anthropic OAuth flow (Claude Pro/Max)
3
+ */
4
+ import { generatePKCE } from "./pkce.js";
5
+ const decode = (s) => atob(s);
6
+ const CLIENT_ID = decode("OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl");
7
+ const AUTHORIZE_URL = "https://claude.ai/oauth/authorize";
8
+ const TOKEN_URL = "https://console.anthropic.com/v1/oauth/token";
9
+ const REDIRECT_URI = "https://console.anthropic.com/oauth/code/callback";
10
+ const SCOPES = "org:create_api_key user:profile user:inference";
11
+ /**
12
+ * Login with Anthropic OAuth (device code flow)
13
+ *
14
+ * @param onAuthUrl - Callback to handle the authorization URL (e.g., open browser)
15
+ * @param onPromptCode - Callback to prompt user for the authorization code
16
+ */
17
+ export async function loginAnthropic(onAuthUrl, onPromptCode) {
18
+ const { verifier, challenge } = await generatePKCE();
19
+ // Build authorization URL
20
+ const authParams = new URLSearchParams({
21
+ code: "true",
22
+ client_id: CLIENT_ID,
23
+ response_type: "code",
24
+ redirect_uri: REDIRECT_URI,
25
+ scope: SCOPES,
26
+ code_challenge: challenge,
27
+ code_challenge_method: "S256",
28
+ state: verifier,
29
+ });
30
+ const authUrl = `${AUTHORIZE_URL}?${authParams.toString()}`;
31
+ // Notify caller with URL to open
32
+ onAuthUrl(authUrl);
33
+ // Wait for user to paste authorization code (format: code#state)
34
+ const authCode = await onPromptCode();
35
+ const splits = authCode.split("#");
36
+ const code = splits[0];
37
+ const state = splits[1];
38
+ // Exchange code for tokens
39
+ const tokenResponse = await fetch(TOKEN_URL, {
40
+ method: "POST",
41
+ headers: {
42
+ "Content-Type": "application/json",
43
+ },
44
+ body: JSON.stringify({
45
+ grant_type: "authorization_code",
46
+ client_id: CLIENT_ID,
47
+ code: code,
48
+ state: state,
49
+ redirect_uri: REDIRECT_URI,
50
+ code_verifier: verifier,
51
+ }),
52
+ });
53
+ if (!tokenResponse.ok) {
54
+ const error = await tokenResponse.text();
55
+ throw new Error(`Token exchange failed: ${error}`);
56
+ }
57
+ const tokenData = (await tokenResponse.json());
58
+ // Calculate expiry time (current time + expires_in seconds - 5 min buffer)
59
+ const expiresAt = Date.now() + tokenData.expires_in * 1000 - 5 * 60 * 1000;
60
+ // Save credentials
61
+ return {
62
+ refresh: tokenData.refresh_token,
63
+ access: tokenData.access_token,
64
+ expires: expiresAt,
65
+ };
66
+ }
67
+ /**
68
+ * Refresh Anthropic OAuth token
69
+ */
70
+ export async function refreshAnthropicToken(refreshToken) {
71
+ const response = await fetch(TOKEN_URL, {
72
+ method: "POST",
73
+ headers: { "Content-Type": "application/json" },
74
+ body: JSON.stringify({
75
+ grant_type: "refresh_token",
76
+ client_id: CLIENT_ID,
77
+ refresh_token: refreshToken,
78
+ }),
79
+ });
80
+ if (!response.ok) {
81
+ const error = await response.text();
82
+ throw new Error(`Anthropic token refresh failed: ${error}`);
83
+ }
84
+ const data = (await response.json());
85
+ return {
86
+ refresh: data.refresh_token,
87
+ access: data.access_token,
88
+ expires: Date.now() + data.expires_in * 1000 - 5 * 60 * 1000,
89
+ };
90
+ }
91
+ export const anthropicOAuthProvider = {
92
+ id: "anthropic",
93
+ name: "Anthropic (Claude Pro/Max)",
94
+ async login(callbacks) {
95
+ return loginAnthropic((url) => callbacks.onAuth({ url }), () => callbacks.onPrompt({ message: "Paste the authorization code:" }));
96
+ },
97
+ async refreshToken(credentials) {
98
+ return refreshAnthropicToken(credentials.refresh);
99
+ },
100
+ getApiKey(credentials) {
101
+ return credentials.access;
102
+ },
103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S,EAAE,SAAS;YACpB,aAAa,EAAE,YAAY;SAC3B,CAAC;KACF,CAAC,CAAC;IAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,mCAAmC,KAAK,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAIlC,CAAC;IAEF,OAAO;QACN,OAAO,EAAE,IAAI,CAAC,aAAa;QAC3B,MAAM,EAAE,IAAI,CAAC,YAAY;QACzB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI;KAC5D,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,sBAAsB,GAA2B;IAC7D,EAAE,EAAE,WAAW;IACf,IAAI,EAAE,4BAA4B;IAElC,KAAK,CAAC,KAAK,CAAC,SAA8B;QACzC,OAAO,cAAc,CACpB,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAClC,GAAG,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,+BAA+B,EAAE,CAAC,CACtE,CAAC;IACH,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,WAA6B;QAC/C,OAAO,qBAAqB,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACnD,CAAC;IAED,SAAS,CAAC,WAA6B;QACtC,OAAO,WAAW,CAAC,MAAM,CAAC;IAC3B,CAAC;CACD,CAAC","sourcesContent":["/**\n * Anthropic OAuth flow (Claude Pro/Max)\n */\n\nimport { generatePKCE } from \"./pkce.js\";\nimport type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from \"./types.js\";\n\nconst decode = (s: string) => atob(s);\nconst CLIENT_ID = decode(\"OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl\");\nconst AUTHORIZE_URL = \"https://claude.ai/oauth/authorize\";\nconst TOKEN_URL = \"https://console.anthropic.com/v1/oauth/token\";\nconst REDIRECT_URI = \"https://console.anthropic.com/oauth/code/callback\";\nconst SCOPES = \"org:create_api_key user:profile user:inference\";\n\n/**\n * Login with Anthropic OAuth (device code flow)\n *\n * @param onAuthUrl - Callback to handle the authorization URL (e.g., open browser)\n * @param onPromptCode - Callback to prompt user for the authorization code\n */\nexport async function loginAnthropic(\n\tonAuthUrl: (url: string) => void,\n\tonPromptCode: () => Promise<string>,\n): Promise<OAuthCredentials> {\n\tconst { verifier, challenge } = await generatePKCE();\n\n\t// Build authorization URL\n\tconst authParams = new URLSearchParams({\n\t\tcode: \"true\",\n\t\tclient_id: CLIENT_ID,\n\t\tresponse_type: \"code\",\n\t\tredirect_uri: REDIRECT_URI,\n\t\tscope: SCOPES,\n\t\tcode_challenge: challenge,\n\t\tcode_challenge_method: \"S256\",\n\t\tstate: verifier,\n\t});\n\n\tconst authUrl = `${AUTHORIZE_URL}?${authParams.toString()}`;\n\n\t// Notify caller with URL to open\n\tonAuthUrl(authUrl);\n\n\t// Wait for user to paste authorization code (format: code#state)\n\tconst authCode = await onPromptCode();\n\tconst splits = authCode.split(\"#\");\n\tconst code = splits[0];\n\tconst state = splits[1];\n\n\t// Exchange code for tokens\n\tconst tokenResponse = await fetch(TOKEN_URL, {\n\t\tmethod: \"POST\",\n\t\theaders: {\n\t\t\t\"Content-Type\": \"application/json\",\n\t\t},\n\t\tbody: JSON.stringify({\n\t\t\tgrant_type: \"authorization_code\",\n\t\t\tclient_id: CLIENT_ID,\n\t\t\tcode: code,\n\t\t\tstate: state,\n\t\t\tredirect_uri: REDIRECT_URI,\n\t\t\tcode_verifier: verifier,\n\t\t}),\n\t});\n\n\tif (!tokenResponse.ok) {\n\t\tconst error = await tokenResponse.text();\n\t\tthrow new Error(`Token exchange failed: ${error}`);\n\t}\n\n\tconst tokenData = (await tokenResponse.json()) as {\n\t\taccess_token: string;\n\t\trefresh_token: string;\n\t\texpires_in: number;\n\t};\n\n\t// Calculate expiry time (current time + expires_in seconds - 5 min buffer)\n\tconst expiresAt = Date.now() + tokenData.expires_in * 1000 - 5 * 60 * 1000;\n\n\t// Save credentials\n\treturn {\n\t\trefresh: tokenData.refresh_token,\n\t\taccess: tokenData.access_token,\n\t\texpires: expiresAt,\n\t};\n}\n\n/**\n * Refresh Anthropic OAuth token\n */\nexport async function refreshAnthropicToken(refreshToken: string): Promise<OAuthCredentials> {\n\tconst response = await fetch(TOKEN_URL, {\n\t\tmethod: \"POST\",\n\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\tbody: JSON.stringify({\n\t\t\tgrant_type: \"refresh_token\",\n\t\t\tclient_id: CLIENT_ID,\n\t\t\trefresh_token: refreshToken,\n\t\t}),\n\t});\n\n\tif (!response.ok) {\n\t\tconst error = await response.text();\n\t\tthrow new Error(`Anthropic token refresh failed: ${error}`);\n\t}\n\n\tconst data = (await response.json()) as {\n\t\taccess_token: string;\n\t\trefresh_token: string;\n\t\texpires_in: number;\n\t};\n\n\treturn {\n\t\trefresh: data.refresh_token,\n\t\taccess: data.access_token,\n\t\texpires: Date.now() + data.expires_in * 1000 - 5 * 60 * 1000,\n\t};\n}\n\nexport const anthropicOAuthProvider: OAuthProviderInterface = {\n\tid: \"anthropic\",\n\tname: \"Anthropic (Claude Pro/Max)\",\n\n\tasync login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {\n\t\treturn loginAnthropic(\n\t\t\t(url) => callbacks.onAuth({ url }),\n\t\t\t() => callbacks.onPrompt({ message: \"Paste the authorization code:\" }),\n\t\t);\n\t},\n\n\tasync refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {\n\t\treturn refreshAnthropicToken(credentials.refresh);\n\t},\n\n\tgetApiKey(credentials: OAuthCredentials): string {\n\t\treturn credentials.access;\n\t},\n};\n"]}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * GitHub Copilot OAuth flow
3
+ */
4
+ import type { OAuthCredentials, OAuthProviderInterface } from "./types.js";
5
+ export declare function normalizeDomain(input: string): string | null;
6
+ export declare function getGitHubCopilotBaseUrl(token?: string, enterpriseDomain?: string): string;
7
+ /**
8
+ * Refresh GitHub Copilot token
9
+ */
10
+ export declare function refreshGitHubCopilotToken(refreshToken: string, enterpriseDomain?: string): Promise<OAuthCredentials>;
11
+ /**
12
+ * Login with GitHub Copilot OAuth (device code flow)
13
+ *
14
+ * @param options.onAuth - Callback with URL and optional instructions (user code)
15
+ * @param options.onPrompt - Callback to prompt user for input
16
+ * @param options.onProgress - Optional progress callback
17
+ * @param options.signal - Optional AbortSignal for cancellation
18
+ */
19
+ export declare function loginGitHubCopilot(options: {
20
+ onAuth: (url: string, instructions?: string) => void;
21
+ onPrompt: (prompt: {
22
+ message: string;
23
+ placeholder?: string;
24
+ allowEmpty?: boolean;
25
+ }) => Promise<string>;
26
+ onProgress?: (message: string) => void;
27
+ signal?: AbortSignal;
28
+ }): Promise<OAuthCredentials>;
29
+ export declare const githubCopilotOAuthProvider: OAuthProviderInterface;
30
+ //# sourceMappingURL=github-copilot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"github-copilot.d.ts","sourceRoot":"","sources":["../../../src/utils/oauth/github-copilot.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,gBAAgB,EAAuB,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAoChG,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAS5D;AA4BD,wBAAgB,uBAAuB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CASzF;AAsID;;GAEG;AACH,wBAAsB,yBAAyB,CAC9C,YAAY,EAAE,MAAM,EACpB,gBAAgB,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,gBAAgB,CAAC,CA6B3B;AA8CD;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE;IACjD,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACrD,QAAQ,EAAE,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACvG,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAkC5B;AAED,eAAO,MAAM,0BAA0B,EAAE,sBA4BxC,CAAC"}
@@ -0,0 +1,281 @@
1
+ /**
2
+ * GitHub Copilot OAuth flow
3
+ */
4
+ import { getModels } from "../../models.js";
5
+ const decode = (s) => atob(s);
6
+ const CLIENT_ID = decode("SXYxLmI1MDdhMDhjODdlY2ZlOTg=");
7
+ const COPILOT_HEADERS = {
8
+ "User-Agent": "GitHubCopilotChat/0.35.0",
9
+ "Editor-Version": "vscode/1.107.0",
10
+ "Editor-Plugin-Version": "copilot-chat/0.35.0",
11
+ "Copilot-Integration-Id": "vscode-chat",
12
+ };
13
+ export function normalizeDomain(input) {
14
+ const trimmed = input.trim();
15
+ if (!trimmed)
16
+ return null;
17
+ try {
18
+ const url = trimmed.includes("://") ? new URL(trimmed) : new URL(`https://${trimmed}`);
19
+ return url.hostname;
20
+ }
21
+ catch {
22
+ return null;
23
+ }
24
+ }
25
+ function getUrls(domain) {
26
+ return {
27
+ deviceCodeUrl: `https://${domain}/login/device/code`,
28
+ accessTokenUrl: `https://${domain}/login/oauth/access_token`,
29
+ copilotTokenUrl: `https://api.${domain}/copilot_internal/v2/token`,
30
+ };
31
+ }
32
+ /**
33
+ * Parse the proxy-ep from a Copilot token and convert to API base URL.
34
+ * Token format: tid=...;exp=...;proxy-ep=proxy.individual.githubcopilot.com;...
35
+ * Returns API URL like https://api.individual.githubcopilot.com
36
+ */
37
+ function getBaseUrlFromToken(token) {
38
+ const match = token.match(/proxy-ep=([^;]+)/);
39
+ if (!match)
40
+ return null;
41
+ const proxyHost = match[1];
42
+ // Convert proxy.xxx to api.xxx
43
+ const apiHost = proxyHost.replace(/^proxy\./, "api.");
44
+ return `https://${apiHost}`;
45
+ }
46
+ export function getGitHubCopilotBaseUrl(token, enterpriseDomain) {
47
+ // If we have a token, extract the base URL from proxy-ep
48
+ if (token) {
49
+ const urlFromToken = getBaseUrlFromToken(token);
50
+ if (urlFromToken)
51
+ return urlFromToken;
52
+ }
53
+ // Fallback for enterprise or if token parsing fails
54
+ if (enterpriseDomain)
55
+ return `https://copilot-api.${enterpriseDomain}`;
56
+ return "https://api.individual.githubcopilot.com";
57
+ }
58
+ async function fetchJson(url, init) {
59
+ const response = await fetch(url, init);
60
+ if (!response.ok) {
61
+ const text = await response.text();
62
+ throw new Error(`${response.status} ${response.statusText}: ${text}`);
63
+ }
64
+ return response.json();
65
+ }
66
+ async function startDeviceFlow(domain) {
67
+ const urls = getUrls(domain);
68
+ const data = await fetchJson(urls.deviceCodeUrl, {
69
+ method: "POST",
70
+ headers: {
71
+ Accept: "application/json",
72
+ "Content-Type": "application/json",
73
+ "User-Agent": "GitHubCopilotChat/0.35.0",
74
+ },
75
+ body: JSON.stringify({
76
+ client_id: CLIENT_ID,
77
+ scope: "read:user",
78
+ }),
79
+ });
80
+ if (!data || typeof data !== "object") {
81
+ throw new Error("Invalid device code response");
82
+ }
83
+ const deviceCode = data.device_code;
84
+ const userCode = data.user_code;
85
+ const verificationUri = data.verification_uri;
86
+ const interval = data.interval;
87
+ const expiresIn = data.expires_in;
88
+ if (typeof deviceCode !== "string" ||
89
+ typeof userCode !== "string" ||
90
+ typeof verificationUri !== "string" ||
91
+ typeof interval !== "number" ||
92
+ typeof expiresIn !== "number") {
93
+ throw new Error("Invalid device code response fields");
94
+ }
95
+ return {
96
+ device_code: deviceCode,
97
+ user_code: userCode,
98
+ verification_uri: verificationUri,
99
+ interval,
100
+ expires_in: expiresIn,
101
+ };
102
+ }
103
+ /**
104
+ * Sleep that can be interrupted by an AbortSignal
105
+ */
106
+ function abortableSleep(ms, signal) {
107
+ return new Promise((resolve, reject) => {
108
+ if (signal?.aborted) {
109
+ reject(new Error("Login cancelled"));
110
+ return;
111
+ }
112
+ const timeout = setTimeout(resolve, ms);
113
+ signal?.addEventListener("abort", () => {
114
+ clearTimeout(timeout);
115
+ reject(new Error("Login cancelled"));
116
+ }, { once: true });
117
+ });
118
+ }
119
+ async function pollForGitHubAccessToken(domain, deviceCode, intervalSeconds, expiresIn, signal) {
120
+ const urls = getUrls(domain);
121
+ const deadline = Date.now() + expiresIn * 1000;
122
+ let intervalMs = Math.max(1000, Math.floor(intervalSeconds * 1000));
123
+ while (Date.now() < deadline) {
124
+ if (signal?.aborted) {
125
+ throw new Error("Login cancelled");
126
+ }
127
+ const raw = await fetchJson(urls.accessTokenUrl, {
128
+ method: "POST",
129
+ headers: {
130
+ Accept: "application/json",
131
+ "Content-Type": "application/json",
132
+ "User-Agent": "GitHubCopilotChat/0.35.0",
133
+ },
134
+ body: JSON.stringify({
135
+ client_id: CLIENT_ID,
136
+ device_code: deviceCode,
137
+ grant_type: "urn:ietf:params:oauth:grant-type:device_code",
138
+ }),
139
+ });
140
+ if (raw && typeof raw === "object" && typeof raw.access_token === "string") {
141
+ return raw.access_token;
142
+ }
143
+ if (raw && typeof raw === "object" && typeof raw.error === "string") {
144
+ const err = raw.error;
145
+ if (err === "authorization_pending") {
146
+ await abortableSleep(intervalMs, signal);
147
+ continue;
148
+ }
149
+ if (err === "slow_down") {
150
+ intervalMs += 5000;
151
+ await abortableSleep(intervalMs, signal);
152
+ continue;
153
+ }
154
+ throw new Error(`Device flow failed: ${err}`);
155
+ }
156
+ await abortableSleep(intervalMs, signal);
157
+ }
158
+ throw new Error("Device flow timed out");
159
+ }
160
+ /**
161
+ * Refresh GitHub Copilot token
162
+ */
163
+ export async function refreshGitHubCopilotToken(refreshToken, enterpriseDomain) {
164
+ const domain = enterpriseDomain || "github.com";
165
+ const urls = getUrls(domain);
166
+ const raw = await fetchJson(urls.copilotTokenUrl, {
167
+ headers: {
168
+ Accept: "application/json",
169
+ Authorization: `Bearer ${refreshToken}`,
170
+ ...COPILOT_HEADERS,
171
+ },
172
+ });
173
+ if (!raw || typeof raw !== "object") {
174
+ throw new Error("Invalid Copilot token response");
175
+ }
176
+ const token = raw.token;
177
+ const expiresAt = raw.expires_at;
178
+ if (typeof token !== "string" || typeof expiresAt !== "number") {
179
+ throw new Error("Invalid Copilot token response fields");
180
+ }
181
+ return {
182
+ refresh: refreshToken,
183
+ access: token,
184
+ expires: expiresAt * 1000 - 5 * 60 * 1000,
185
+ enterpriseUrl: enterpriseDomain,
186
+ };
187
+ }
188
+ /**
189
+ * Enable a model for the user's GitHub Copilot account.
190
+ * This is required for some models (like Claude, Grok) before they can be used.
191
+ */
192
+ async function enableGitHubCopilotModel(token, modelId, enterpriseDomain) {
193
+ const baseUrl = getGitHubCopilotBaseUrl(token, enterpriseDomain);
194
+ const url = `${baseUrl}/models/${modelId}/policy`;
195
+ try {
196
+ const response = await fetch(url, {
197
+ method: "POST",
198
+ headers: {
199
+ "Content-Type": "application/json",
200
+ Authorization: `Bearer ${token}`,
201
+ ...COPILOT_HEADERS,
202
+ "openai-intent": "chat-policy",
203
+ "x-interaction-type": "chat-policy",
204
+ },
205
+ body: JSON.stringify({ state: "enabled" }),
206
+ });
207
+ return response.ok;
208
+ }
209
+ catch {
210
+ return false;
211
+ }
212
+ }
213
+ /**
214
+ * Enable all known GitHub Copilot models that may require policy acceptance.
215
+ * Called after successful login to ensure all models are available.
216
+ */
217
+ async function enableAllGitHubCopilotModels(token, enterpriseDomain, onProgress) {
218
+ const models = getModels("github-copilot");
219
+ await Promise.all(models.map(async (model) => {
220
+ const success = await enableGitHubCopilotModel(token, model.id, enterpriseDomain);
221
+ onProgress?.(model.id, success);
222
+ }));
223
+ }
224
+ /**
225
+ * Login with GitHub Copilot OAuth (device code flow)
226
+ *
227
+ * @param options.onAuth - Callback with URL and optional instructions (user code)
228
+ * @param options.onPrompt - Callback to prompt user for input
229
+ * @param options.onProgress - Optional progress callback
230
+ * @param options.signal - Optional AbortSignal for cancellation
231
+ */
232
+ export async function loginGitHubCopilot(options) {
233
+ const input = await options.onPrompt({
234
+ message: "GitHub Enterprise URL/domain (blank for github.com)",
235
+ placeholder: "company.ghe.com",
236
+ allowEmpty: true,
237
+ });
238
+ if (options.signal?.aborted) {
239
+ throw new Error("Login cancelled");
240
+ }
241
+ const trimmed = input.trim();
242
+ const enterpriseDomain = normalizeDomain(input);
243
+ if (trimmed && !enterpriseDomain) {
244
+ throw new Error("Invalid GitHub Enterprise URL/domain");
245
+ }
246
+ const domain = enterpriseDomain || "github.com";
247
+ const device = await startDeviceFlow(domain);
248
+ options.onAuth(device.verification_uri, `Enter code: ${device.user_code}`);
249
+ const githubAccessToken = await pollForGitHubAccessToken(domain, device.device_code, device.interval, device.expires_in, options.signal);
250
+ const credentials = await refreshGitHubCopilotToken(githubAccessToken, enterpriseDomain ?? undefined);
251
+ // Enable all models after successful login
252
+ options.onProgress?.("Enabling models...");
253
+ await enableAllGitHubCopilotModels(credentials.access, enterpriseDomain ?? undefined);
254
+ return credentials;
255
+ }
256
+ export const githubCopilotOAuthProvider = {
257
+ id: "github-copilot",
258
+ name: "GitHub Copilot",
259
+ async login(callbacks) {
260
+ return loginGitHubCopilot({
261
+ onAuth: (url, instructions) => callbacks.onAuth({ url, instructions }),
262
+ onPrompt: callbacks.onPrompt,
263
+ onProgress: callbacks.onProgress,
264
+ signal: callbacks.signal,
265
+ });
266
+ },
267
+ async refreshToken(credentials) {
268
+ const creds = credentials;
269
+ return refreshGitHubCopilotToken(creds.refresh, creds.enterpriseUrl);
270
+ },
271
+ getApiKey(credentials) {
272
+ return credentials.access;
273
+ },
274
+ modifyModels(models, credentials) {
275
+ const creds = credentials;
276
+ const domain = creds.enterpriseUrl ? (normalizeDomain(creds.enterpriseUrl) ?? undefined) : undefined;
277
+ const baseUrl = getGitHubCopilotBaseUrl(creds.access, domain);
278
+ return models.map((m) => (m.provider === "github-copilot" ? { ...m, baseUrl } : m));
279
+ },
280
+ };
281
+ //# sourceMappingURL=github-copilot.js.map