@agentorg/cli 0.0.4 → 0.0.6

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 (206) hide show
  1. package/README.md +18 -102
  2. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.d.ts +2 -7
  3. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.js +4 -111
  4. package/dist/node_modules/@agentorg/hub/dist/src/index.d.ts +1 -3
  5. package/dist/node_modules/@agentorg/hub/dist/src/index.js +1 -3
  6. package/dist/node_modules/@agentorg/hub/dist/src/router.d.ts +6 -57
  7. package/dist/node_modules/@agentorg/hub/dist/src/router.js +38 -413
  8. package/dist/node_modules/@agentorg/hub/dist/src/state.d.ts +20 -298
  9. package/dist/node_modules/@agentorg/hub/dist/src/state.js +73 -456
  10. package/dist/node_modules/@agentorg/hub/dist/src/validate.d.ts +1 -3
  11. package/dist/node_modules/@agentorg/hub/dist/src/validate.js +3 -15
  12. package/dist/node_modules/@agentorg/hub/package.json +0 -3
  13. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.d.ts +132 -0
  14. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.js +105 -0
  15. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.d.ts +42 -0
  16. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.js +53 -0
  17. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.d.ts +15 -0
  18. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.js +14 -0
  19. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.d.ts +16 -267
  20. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.js +17 -145
  21. package/dist/node_modules/@agentorg/interfaces/dist/src/index.d.ts +6 -8
  22. package/dist/node_modules/@agentorg/interfaces/dist/src/index.js +6 -8
  23. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.d.ts +2 -2
  24. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.js +2 -2
  25. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.d.ts +272 -0
  26. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.js +185 -0
  27. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.d.ts +88 -0
  28. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.js +78 -0
  29. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.d.ts +39 -0
  30. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.js +43 -0
  31. package/dist/node_modules/@agentorg/manager/dist/src/index.d.ts +1 -0
  32. package/dist/node_modules/@agentorg/manager/dist/src/index.js +1 -0
  33. package/dist/node_modules/@agentorg/manager/dist/src/manager.d.ts +21 -0
  34. package/dist/node_modules/@agentorg/manager/dist/src/manager.js +528 -0
  35. package/dist/node_modules/@agentorg/manager/dist/src/skills.d.ts +25 -0
  36. package/dist/node_modules/@agentorg/manager/dist/src/skills.js +145 -0
  37. package/dist/node_modules/@agentorg/manager/package.json +30 -0
  38. package/dist/node_modules/@agentorg/mcp/dist/src/connection.d.ts +33 -0
  39. package/dist/node_modules/@agentorg/mcp/dist/src/connection.js +290 -0
  40. package/dist/node_modules/@agentorg/mcp/dist/src/index.d.ts +1 -0
  41. package/dist/node_modules/@agentorg/mcp/dist/src/index.js +1 -0
  42. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.d.ts +50 -0
  43. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.js +69 -0
  44. package/dist/node_modules/@agentorg/mcp/dist/src/provider.d.ts +22 -0
  45. package/dist/node_modules/@agentorg/mcp/dist/src/provider.js +167 -0
  46. package/dist/node_modules/@agentorg/mcp/package.json +31 -0
  47. package/dist/node_modules/@agentorg/sdk/dist/src/cli.js +3 -23
  48. package/dist/node_modules/@agentorg/sdk/dist/src/client.d.ts +4 -25
  49. package/dist/node_modules/@agentorg/sdk/dist/src/client.js +5 -55
  50. package/dist/node_modules/@agentorg/sdk/dist/src/errors.d.ts +1 -1
  51. package/dist/node_modules/@agentorg/sdk/dist/src/errors.js +3 -3
  52. package/dist/node_modules/@agentorg/sdk/dist/src/index.d.ts +2 -2
  53. package/dist/node_modules/@agentorg/sdk/dist/src/index.js +1 -1
  54. package/dist/node_modules/@agentorg/sdk/dist/src/interface.d.ts +6 -34
  55. package/dist/node_modules/@agentorg/sdk/dist/src/interface.js +3 -24
  56. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.d.ts +8 -17
  57. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.js +15 -87
  58. package/dist/node_modules/@agentorg/sdk/dist/src/module.d.ts +10 -76
  59. package/dist/node_modules/@agentorg/sdk/dist/src/module.js +3 -23
  60. package/dist/node_modules/@agentorg/sdk/dist/src/person-commands.js +0 -5
  61. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.d.ts +3 -27
  62. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.js +5 -100
  63. package/dist/node_modules/@agentorg/sdk/dist/src/testing.d.ts +7 -8
  64. package/dist/node_modules/@agentorg/sdk/dist/src/testing.js +12 -24
  65. package/dist/node_modules/@agentorg/sdk/package.json +4 -37
  66. package/dist/node_modules/@agentorg/shell/dist/app.css +3435 -739
  67. package/dist/node_modules/@agentorg/shell/dist/app.js +204 -202
  68. package/dist/node_modules/@agentorg/shell/package.json +18 -10
  69. package/dist/node_modules/@agentorg/wire/dist/src/frames.d.ts +2 -3
  70. package/dist/node_modules/@agentorg/wire/dist/src/frames.js +2 -3
  71. package/dist/node_modules/@agentorg/wire/dist/src/ops.d.ts +5 -34
  72. package/dist/node_modules/@agentorg/wire/dist/src/ops.js +2 -17
  73. package/dist/node_modules/assistant/dist/src/agent.d.ts +62 -0
  74. package/dist/node_modules/assistant/dist/src/agent.js +226 -0
  75. package/dist/node_modules/assistant/dist/src/engine.d.ts +23 -0
  76. package/dist/node_modules/assistant/dist/src/engine.js +88 -0
  77. package/dist/node_modules/assistant/dist/src/history.d.ts +97 -0
  78. package/dist/node_modules/assistant/dist/src/history.js +235 -0
  79. package/dist/node_modules/assistant/dist/src/local-store.d.ts +3 -0
  80. package/dist/node_modules/assistant/dist/src/local-store.js +58 -0
  81. package/dist/node_modules/assistant/dist/src/provider.d.ts +21 -0
  82. package/dist/node_modules/assistant/dist/src/provider.js +231 -0
  83. package/dist/node_modules/assistant/dist/src/settings.d.ts +17 -0
  84. package/dist/node_modules/assistant/dist/src/settings.js +43 -0
  85. package/dist/node_modules/assistant/dist/src/tools.d.ts +74 -0
  86. package/dist/node_modules/assistant/dist/src/tools.js +123 -0
  87. package/dist/node_modules/assistant/package.json +29 -0
  88. package/dist/node_modules/chatgpt/dist/src/account.d.ts +52 -0
  89. package/dist/node_modules/chatgpt/dist/src/account.js +270 -0
  90. package/dist/node_modules/chatgpt/dist/src/encoding.d.ts +7 -0
  91. package/dist/node_modules/chatgpt/dist/src/encoding.js +20 -0
  92. package/dist/node_modules/chatgpt/dist/src/file-store.d.ts +9 -0
  93. package/dist/node_modules/chatgpt/dist/src/file-store.js +68 -0
  94. package/dist/node_modules/chatgpt/dist/src/index.d.ts +1 -0
  95. package/dist/node_modules/chatgpt/dist/src/index.js +1 -0
  96. package/dist/node_modules/chatgpt/dist/src/jwt.d.ts +15 -0
  97. package/dist/node_modules/chatgpt/dist/src/jwt.js +37 -0
  98. package/dist/node_modules/chatgpt/dist/src/oauth.d.ts +43 -0
  99. package/dist/node_modules/chatgpt/dist/src/oauth.js +104 -0
  100. package/dist/node_modules/chatgpt/dist/src/program.d.ts +23 -0
  101. package/dist/node_modules/chatgpt/dist/src/program.js +41 -0
  102. package/dist/node_modules/chatgpt/dist/src/responses.d.ts +54 -0
  103. package/dist/node_modules/chatgpt/dist/src/responses.js +300 -0
  104. package/dist/node_modules/chatgpt/dist/src/serve.d.ts +65 -0
  105. package/dist/node_modules/chatgpt/dist/src/serve.js +46 -0
  106. package/dist/node_modules/chatgpt/package.json +33 -0
  107. package/dist/src/api.d.ts +5 -34
  108. package/dist/src/api.js +6 -68
  109. package/dist/src/cli.js +3 -7
  110. package/dist/src/gateway.d.ts +6 -7
  111. package/dist/src/gateway.js +39 -285
  112. package/dist/src/index.d.ts +4 -5
  113. package/dist/src/index.js +3 -4
  114. package/dist/src/notes.d.ts +11 -0
  115. package/dist/src/notes.js +64 -0
  116. package/dist/src/oauth.d.ts +16 -0
  117. package/dist/src/oauth.js +46 -0
  118. package/dist/src/programs.d.ts +29 -0
  119. package/dist/src/programs.js +171 -0
  120. package/package.json +3 -11
  121. package/dist/bundled/index.json +0 -6
  122. package/dist/bundled/sha256-01d69bc3888c2a53963f4413d47de909d129ad27c78e9af6d830bb50c3478419.agentorg +0 -0
  123. package/dist/bundled/sha256-84165e3be5c904fc971d983dab31102e07cca725d8629a6fd7d118fa3a0a88b8.agentorg +0 -0
  124. package/dist/bundled/sha256-ade9132b3f34b4c39db92adeadc7e540dc745e4a81b5b54f5d45bf9ec92bca75.agentorg +0 -0
  125. package/dist/bundled/sha256-f4330f5987f37d8cf3aeb44d2f831b573641a87186daa2f811f0c35ba04804f0.agentorg +0 -0
  126. package/dist/node_modules/@agentorg/hub/dist/src/build.d.ts +0 -17
  127. package/dist/node_modules/@agentorg/hub/dist/src/build.js +0 -120
  128. package/dist/node_modules/@agentorg/hub/dist/src/logs.d.ts +0 -33
  129. package/dist/node_modules/@agentorg/hub/dist/src/logs.js +0 -162
  130. package/dist/node_modules/@agentorg/hub/dist/src/net.d.ts +0 -41
  131. package/dist/node_modules/@agentorg/hub/dist/src/net.js +0 -265
  132. package/dist/node_modules/@agentorg/hub/dist/src/processes.d.ts +0 -78
  133. package/dist/node_modules/@agentorg/hub/dist/src/processes.js +0 -485
  134. package/dist/node_modules/@agentorg/hub/dist/src/storage.d.ts +0 -38
  135. package/dist/node_modules/@agentorg/hub/dist/src/storage.js +0 -194
  136. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.d.ts +0 -6
  137. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.js +0 -7
  138. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.d.ts +0 -191
  139. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.js +0 -136
  140. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.d.ts +0 -47
  141. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.js +0 -52
  142. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.d.ts +0 -35
  143. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.js +0 -37
  144. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.d.ts +0 -23
  145. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.js +0 -31
  146. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.d.ts +0 -14
  147. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.js +0 -26
  148. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.d.ts +0 -27
  149. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.js +0 -26
  150. package/dist/node_modules/@agentorg/kit/dist/src/chat.d.ts +0 -37
  151. package/dist/node_modules/@agentorg/kit/dist/src/chat.js +0 -131
  152. package/dist/node_modules/@agentorg/kit/dist/src/components.d.ts +0 -94
  153. package/dist/node_modules/@agentorg/kit/dist/src/components.js +0 -66
  154. package/dist/node_modules/@agentorg/kit/dist/src/icons.d.ts +0 -17
  155. package/dist/node_modules/@agentorg/kit/dist/src/icons.js +0 -30
  156. package/dist/node_modules/@agentorg/kit/dist/src/index.d.ts +0 -6
  157. package/dist/node_modules/@agentorg/kit/dist/src/index.js +0 -10
  158. package/dist/node_modules/@agentorg/kit/dist/src/markdown.d.ts +0 -9
  159. package/dist/node_modules/@agentorg/kit/dist/src/markdown.js +0 -14
  160. package/dist/node_modules/@agentorg/kit/dist/src/resizable.d.ts +0 -5
  161. package/dist/node_modules/@agentorg/kit/dist/src/resizable.js +0 -8
  162. package/dist/node_modules/@agentorg/kit/dist/src/styles.d.ts +0 -11
  163. package/dist/node_modules/@agentorg/kit/dist/src/styles.js +0 -166
  164. package/dist/node_modules/@agentorg/kit/package.json +0 -44
  165. package/dist/node_modules/@agentorg/sdk/dist/frame/runtime.js +0 -753
  166. package/dist/node_modules/@agentorg/sdk/dist/host/runner.js +0 -21963
  167. package/dist/node_modules/@agentorg/sdk/dist/src/build.d.ts +0 -2
  168. package/dist/node_modules/@agentorg/sdk/dist/src/build.js +0 -2
  169. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.d.ts +0 -24
  170. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.js +0 -247
  171. package/dist/node_modules/@agentorg/sdk/dist/src/describe.d.ts +0 -13
  172. package/dist/node_modules/@agentorg/sdk/dist/src/describe.js +0 -16
  173. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.d.ts +0 -8
  174. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.js +0 -64
  175. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.d.ts +0 -1
  176. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.js +0 -35
  177. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.d.ts +0 -57
  178. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.js +0 -293
  179. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.d.ts +0 -124
  180. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.js +0 -16
  181. package/dist/node_modules/@agentorg/sdk/dist/src/frame.d.ts +0 -25
  182. package/dist/node_modules/@agentorg/sdk/dist/src/frame.js +0 -248
  183. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.d.ts +0 -1
  184. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.js +0 -60
  185. package/dist/node_modules/@agentorg/sdk/dist/src/host.d.ts +0 -25
  186. package/dist/node_modules/@agentorg/sdk/dist/src/host.js +0 -92
  187. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.d.ts +0 -2
  188. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.js +0 -2
  189. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.d.ts +0 -56
  190. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.js +0 -165
  191. package/dist/node_modules/@agentorg/sdk/dist/src/package.d.ts +0 -60
  192. package/dist/node_modules/@agentorg/sdk/dist/src/package.js +0 -538
  193. package/dist/node_modules/@agentorg/sdk/dist/src/tar.d.ts +0 -7
  194. package/dist/node_modules/@agentorg/sdk/dist/src/tar.js +0 -71
  195. package/dist/node_modules/@agentorg/shell/dist/agent-bindings.js +0 -59
  196. package/dist/node_modules/@agentorg/shell/src/agent-bindings.ts +0 -107
  197. package/dist/src/frames.d.ts +0 -23
  198. package/dist/src/frames.js +0 -78
  199. package/dist/src/layout.d.ts +0 -44
  200. package/dist/src/layout.js +0 -90
  201. package/dist/src/model.d.ts +0 -51
  202. package/dist/src/model.js +0 -230
  203. package/dist/src/rules.d.ts +0 -71
  204. package/dist/src/rules.js +0 -157
  205. package/dist/src/transfer.d.ts +0 -30
  206. package/dist/src/transfer.js +0 -110
@@ -0,0 +1,300 @@
1
+ import { Refused } from "@agentorg/sdk";
2
+ import { notEligible } from "./account.js";
3
+ import { word } from "./oauth.js";
4
+ const API = "https://api.openai.com/v1";
5
+ export const USAGE_SETTINGS = "https://chatgpt.com/settings/usage";
6
+ /**
7
+ * OpenAI's error, as a refusal: by its code first, then its HTTP status when there is one (an error
8
+ * inside the stream has none). Only sanitized words of OpenAI's reply are shown.
9
+ */
10
+ export function refusalOf(code, at = {}) {
11
+ const ref = at.requestId ? ` (request ${word(at.requestId)})` : "";
12
+ switch (code) {
13
+ case "subscription_sharing_usage_limit_exceeded":
14
+ return new Refused("usage_limit", `Usage limit reached. Review your plan or this app's limit in ChatGPT settings: ${USAGE_SETTINGS}`);
15
+ case "subscription_sharing_usage_unavailable":
16
+ case "subscription_sharing_user_unavailable":
17
+ return new Refused("unavailable", "ChatGPT plan usage is unavailable right now. Try again later.");
18
+ case "subscription_sharing_user_not_eligible":
19
+ return notEligible();
20
+ case "subscription_sharing_unsupported_capability":
21
+ return new Refused("unsupported", `The request asked for something plan usage doesn't support${at.param ? `: ${word(at.param)}` : ""}${ref}.`);
22
+ }
23
+ if (at.status === 429)
24
+ return refusalOf("subscription_sharing_usage_limit_exceeded", at);
25
+ if (at.status === 503)
26
+ return refusalOf("subscription_sharing_usage_unavailable", at);
27
+ const what = code ? ` ${word(code)}` : "";
28
+ return new Refused("failed", at.status === undefined
29
+ ? `OpenAI ended the response with an error${what}${ref}.`
30
+ : `OpenAI answered ${at.status}${what}${ref}.`);
31
+ }
32
+ async function errorOf(response) {
33
+ let error;
34
+ try {
35
+ error = (await response.json()).error;
36
+ }
37
+ catch {
38
+ // Admission errors may carry only `detail`, which is diagnostic and isn't echoed.
39
+ }
40
+ return refusalOf(typeof error?.code === "string" ? error.code : undefined, {
41
+ status: response.status,
42
+ requestId: response.headers.get("x-request-id"),
43
+ ...(typeof error?.param === "string" ? { param: error.param } : {}),
44
+ });
45
+ }
46
+ const BLANK_LINE = /\r?\n\r?\n/;
47
+ /** Server-sent events, one parsed `data` object at a time, until the stream or `signal` ends. */
48
+ async function* events(body, signal) {
49
+ const decoder = new TextDecoder();
50
+ const reader = body.getReader();
51
+ const stop = () => void reader.cancel().catch(() => { });
52
+ signal.addEventListener("abort", stop);
53
+ let buffer = "";
54
+ let done = false;
55
+ try {
56
+ while (!done) {
57
+ const read = await reader.read();
58
+ done = read.done;
59
+ if (signal.aborted)
60
+ return;
61
+ buffer += decoder.decode(read.value, { stream: !done });
62
+ for (let end = BLANK_LINE.exec(buffer); end; end = BLANK_LINE.exec(buffer)) {
63
+ const block = buffer.slice(0, end.index);
64
+ buffer = buffer.slice(end.index + end[0].length);
65
+ const data = block
66
+ .split(/\r?\n/)
67
+ .filter((line) => line.startsWith("data:"))
68
+ .map((line) => line.slice(5).replace(/^ /, ""))
69
+ .join("\n");
70
+ if (data && data !== "[DONE]") {
71
+ const parsed = JSON.parse(data);
72
+ if (typeof parsed === "object" && parsed !== null)
73
+ yield parsed;
74
+ }
75
+ }
76
+ }
77
+ }
78
+ finally {
79
+ signal.removeEventListener("abort", stop);
80
+ // A response that ended early leaves the rest of its body: close it.
81
+ if (!done)
82
+ await reader.cancel().catch(() => { });
83
+ reader.releaseLock();
84
+ }
85
+ }
86
+ const toApi = (item) => {
87
+ switch (item.type) {
88
+ case "message":
89
+ return { type: "message", role: item.role, content: item.content };
90
+ case "function_call":
91
+ return {
92
+ type: "function_call",
93
+ call_id: item.callId,
94
+ name: item.name,
95
+ arguments: item.arguments,
96
+ };
97
+ case "function_call_output":
98
+ return { type: "function_call_output", call_id: item.callId, output: item.output };
99
+ }
100
+ };
101
+ /** The final output items of a completed response: assistant text and function calls. */
102
+ function outputOf(response) {
103
+ const items = Array.isArray(response.output)
104
+ ? response.output
105
+ : [];
106
+ const output = [];
107
+ for (const item of items) {
108
+ if (item.type === "message" && Array.isArray(item.content)) {
109
+ const text = item.content
110
+ .map((part) => part.type === "output_text" && typeof part.text === "string"
111
+ ? part.text
112
+ : part.type === "refusal" && typeof part.refusal === "string"
113
+ ? part.refusal
114
+ : "")
115
+ .join("");
116
+ output.push({ type: "message", role: "assistant", content: text });
117
+ }
118
+ else if (item.type === "function_call" &&
119
+ typeof item.call_id === "string" &&
120
+ typeof item.name === "string" &&
121
+ typeof item.arguments === "string")
122
+ output.push({
123
+ type: "function_call",
124
+ callId: item.call_id,
125
+ name: item.name,
126
+ arguments: item.arguments,
127
+ });
128
+ // Reasoning items aren't carried: nothing is stored, and the next request sends the history.
129
+ }
130
+ return output;
131
+ }
132
+ const cancelled = () => new Refused("cancelled", "The response was cancelled.");
133
+ export function createSession(options) {
134
+ const { fetch, account } = options;
135
+ let listed;
136
+ /**
137
+ * Responses still running, by caller and request id, so a caller's `cancel` can stop its own and
138
+ * no other's.
139
+ */
140
+ const running = new Map();
141
+ const keyOf = (caller, requestId) => `${caller}\0${requestId}`;
142
+ /** A request with the access token, refreshed and tried once more if OpenAI says 401. */
143
+ const authorized = async (build, signal) => {
144
+ let response;
145
+ try {
146
+ response = await fetch(build(await account.token()));
147
+ if (response.status === 401) {
148
+ await response.body?.cancel();
149
+ response = await fetch(build(await account.token(true)));
150
+ }
151
+ }
152
+ catch (error) {
153
+ if (signal?.aborted)
154
+ throw cancelled();
155
+ if (error instanceof Refused)
156
+ throw error;
157
+ throw new Refused("unavailable", "OpenAI couldn't be reached. Try again later.");
158
+ }
159
+ if (!response.ok) {
160
+ const refused = await errorOf(response);
161
+ if (refused.code === "ineligible")
162
+ await account.ineligible();
163
+ throw refused;
164
+ }
165
+ return response;
166
+ };
167
+ const models = () => {
168
+ listed ??= (async () => {
169
+ const response = await authorized((token) => new Request(`${API}/models`, { headers: { authorization: `Bearer ${token}` } }));
170
+ const body = (await response.json().catch(() => ({})));
171
+ if (!Array.isArray(body.models))
172
+ throw refusalOf("bad_models", { requestId: response.headers.get("x-request-id") });
173
+ return body.models
174
+ .filter((model) => model?.visibility === "list" && typeof model.slug === "string")
175
+ .map((model) => ({
176
+ id: model.slug.slice(0, 128),
177
+ name: (typeof model.display_name === "string"
178
+ ? model.display_name
179
+ : model.slug).slice(0, 256),
180
+ }));
181
+ })().catch((error) => {
182
+ listed = undefined;
183
+ throw error;
184
+ });
185
+ return listed;
186
+ };
187
+ async function stream(input, caller, signal) {
188
+ const model = input.model ?? (await models())[0]?.id;
189
+ if (!model)
190
+ throw new Refused("failed", "No model is offered to this account.");
191
+ if (signal.aborted)
192
+ throw cancelled();
193
+ const body = JSON.stringify({
194
+ model,
195
+ instructions: input.instructions,
196
+ input: input.input.map(toApi),
197
+ ...(input.tools?.length
198
+ ? {
199
+ tools: input.tools.map((tool) => ({
200
+ type: "function",
201
+ name: tool.name,
202
+ ...(tool.description === undefined ? {} : { description: tool.description }),
203
+ parameters: tool.parameters,
204
+ strict: false,
205
+ })),
206
+ }
207
+ : {}),
208
+ stream: true,
209
+ store: false,
210
+ });
211
+ const response = await authorized((token) => new Request(`${API}/responses`, {
212
+ method: "POST",
213
+ headers: {
214
+ authorization: `Bearer ${token}`,
215
+ "content-type": "application/json",
216
+ accept: "text/event-stream",
217
+ },
218
+ body,
219
+ signal,
220
+ }), signal);
221
+ const requestId = response.headers.get("x-request-id");
222
+ if (!response.body)
223
+ throw refusalOf("empty_stream", { status: response.status, requestId });
224
+ try {
225
+ for await (const event of events(response.body, signal)) {
226
+ switch (event.type) {
227
+ case "response.output_text.delta":
228
+ if (typeof event.delta === "string")
229
+ options.delta(caller, input.requestId, event.delta);
230
+ break;
231
+ case "response.completed": {
232
+ const done = (event.response ?? {});
233
+ const usage = done.usage;
234
+ return {
235
+ output: outputOf(done),
236
+ ...(typeof usage?.input_tokens === "number" && typeof usage.output_tokens === "number"
237
+ ? { usage: { inputTokens: usage.input_tokens, outputTokens: usage.output_tokens } }
238
+ : {}),
239
+ };
240
+ }
241
+ case "response.failed": {
242
+ const error = (event.response?.error ??
243
+ {});
244
+ const refused = refusalOf(typeof error.code === "string" ? error.code : undefined, {
245
+ requestId,
246
+ });
247
+ if (refused.code === "ineligible")
248
+ await account.ineligible();
249
+ throw refused;
250
+ }
251
+ case "response.incomplete":
252
+ throw new Refused("failed", "The response ended incomplete.");
253
+ case "error":
254
+ throw refusalOf(typeof event.code === "string" ? event.code : undefined, { requestId });
255
+ }
256
+ }
257
+ }
258
+ catch (error) {
259
+ if (error instanceof Refused)
260
+ throw error;
261
+ if (signal.aborted)
262
+ throw cancelled();
263
+ throw new Refused("failed", "The response stream broke off.");
264
+ }
265
+ if (signal.aborted)
266
+ throw cancelled();
267
+ throw new Refused("failed", "The response ended before it completed.");
268
+ }
269
+ return {
270
+ /** Stop every response still running. */
271
+ stop() {
272
+ for (const controller of running.values())
273
+ controller.abort();
274
+ },
275
+ /** Forget the model list, so it is read again for the next account. */
276
+ reset() {
277
+ listed = undefined;
278
+ },
279
+ async models() {
280
+ return { models: await models() };
281
+ },
282
+ cancel({ requestId }, caller) {
283
+ running.get(keyOf(caller, requestId))?.abort();
284
+ return {};
285
+ },
286
+ async respond(input, caller) {
287
+ const key = keyOf(caller, input.requestId);
288
+ if (running.has(key))
289
+ throw new Refused("invalid", "A response with this requestId is already running.");
290
+ const controller = new AbortController();
291
+ running.set(key, controller);
292
+ try {
293
+ return await stream(input, caller, controller.signal);
294
+ }
295
+ finally {
296
+ running.delete(key);
297
+ }
298
+ },
299
+ };
300
+ }
@@ -0,0 +1,65 @@
1
+ import type { accountLogin, llmSession } from "@agentorg/interfaces";
2
+ import type { Context, Interface, Log } from "@agentorg/sdk";
3
+ import { type Store } from "./account.js";
4
+ import type { Fetch } from "./oauth.js";
5
+ type Exports = {
6
+ login: typeof accountLogin;
7
+ session: typeof llmSession;
8
+ };
9
+ export declare function serveChatGPT(exports: Context<Record<never, Interface>, Exports>["exports"], options: {
10
+ fetch: Fetch;
11
+ store: Store;
12
+ log: Log;
13
+ }): {
14
+ stop(): void;
15
+ reset(): void;
16
+ models(): Promise<{
17
+ models: {
18
+ id: string;
19
+ name: string;
20
+ }[];
21
+ }>;
22
+ cancel({ requestId }: {
23
+ requestId: string;
24
+ }, caller: string): {};
25
+ respond(input: {
26
+ requestId: string;
27
+ model?: string | undefined;
28
+ instructions: string;
29
+ input: ({
30
+ type: "message";
31
+ role: "assistant" | "user";
32
+ content: string;
33
+ } | {
34
+ type: "function_call";
35
+ callId: string;
36
+ name: string;
37
+ arguments: string;
38
+ } | {
39
+ type: "function_call_output";
40
+ callId: string;
41
+ output: string;
42
+ })[];
43
+ tools?: {
44
+ name: string;
45
+ description?: string | undefined;
46
+ parameters: Record<string, import("zod").JSONType>;
47
+ }[] | undefined;
48
+ }, caller: string): Promise<{
49
+ output: ({
50
+ type: "message";
51
+ content: string;
52
+ role: "assistant";
53
+ } | {
54
+ type: "function_call";
55
+ callId: string;
56
+ name: string;
57
+ arguments: string;
58
+ })[];
59
+ usage?: {
60
+ inputTokens: number;
61
+ outputTokens: number;
62
+ } | undefined;
63
+ }>;
64
+ };
65
+ export {};
@@ -0,0 +1,46 @@
1
+ import { createAccount } from "./account.js";
2
+ import { createSession } from "./responses.js";
3
+ export function serveChatGPT(exports, options) {
4
+ const { store, log } = options;
5
+ /** Every request's method, host, status and request id are logged; never its content. */
6
+ const fetch = async (request) => {
7
+ const { method } = request;
8
+ const { host } = new URL(request.url);
9
+ const response = await options.fetch(request);
10
+ log.info("request", {
11
+ method,
12
+ host,
13
+ status: response.status,
14
+ requestId: response.headers.get("x-request-id") ?? undefined,
15
+ });
16
+ return response;
17
+ };
18
+ let session;
19
+ const account = createAccount({
20
+ fetch,
21
+ store,
22
+ changed(state) {
23
+ session?.reset();
24
+ exports.login.emit("changed", { state });
25
+ },
26
+ });
27
+ const started = createSession({
28
+ fetch,
29
+ account,
30
+ // Each consumer's text goes to that consumer alone.
31
+ delta: (caller, requestId, text) => exports.session.emit("delta", { requestId, text }, { to: caller }),
32
+ });
33
+ session = started;
34
+ exports.login.implement({
35
+ status: () => account.status(),
36
+ start: (input) => account.start(input),
37
+ complete: (input) => account.complete(input),
38
+ logout: () => account.logout(),
39
+ });
40
+ exports.session.implement({
41
+ models: () => started.models(),
42
+ respond: (input, { caller }) => started.respond(input, caller),
43
+ cancel: (input, { caller }) => started.cancel(input, caller),
44
+ });
45
+ return started;
46
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "chatgpt",
3
+ "version": "0.0.1",
4
+ "private": true,
5
+ "description": "Sign in with ChatGPT, and model inference on the person's ChatGPT plan",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/src/index.d.ts",
11
+ "default": "./dist/src/index.js"
12
+ },
13
+ "./fake-openai": {
14
+ "types": "./dist/test/fake-openai.d.ts",
15
+ "default": "./dist/test/fake-openai.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist/src"
20
+ ],
21
+ "scripts": {
22
+ "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.json",
23
+ "test": "node --test \"dist/test/**/*.test.js\""
24
+ },
25
+ "dependencies": {
26
+ "@agentorg/interfaces": "0.0.0",
27
+ "@agentorg/sdk": "0.0.0",
28
+ "zod": "^4.6.7"
29
+ },
30
+ "devDependencies": {
31
+ "@agentorg/hub": "0.0.0"
32
+ }
33
+ }
package/dist/src/api.d.ts CHANGED
@@ -1,31 +1,9 @@
1
- /** A view the person has open: an Actor's `view` module. */
2
- export interface View {
3
- actor: string;
4
- module: string;
5
- }
6
- /** The person's own side: its slots, and `layout`, its own export, which the gateway serves. */
1
+ /** The person's own side: the slots the gateway serves `/api` from. */
7
2
  export interface PersonSide {
8
3
  call(slot: string, method: string, input: unknown): Promise<unknown>;
9
4
  /** Events until the returned stop; `end` when the subscription can't go on. */
10
5
  subscribe(slot: string, event: string, send: (payload: unknown) => void, end: () => void): () => void;
11
6
  }
12
- /**
13
- * Calls run as a view's Actor through its `module.slot` bindings: the Hub's in-process view path
14
- * (`hub.viewCall` and `hub.viewSubscribe`), validated and audited like any call. The Hub refuses a
15
- * module that isn't one of the Actor's views, so any view of the Actor's package is open to the
16
- * person, and nothing else is.
17
- */
18
- export interface ViewCalls {
19
- call(view: View, slot: string, method: string, input: unknown): Promise<unknown>;
20
- subscribe(view: View, slot: string, event: string, send: (payload: unknown) => void, end: () => void): () => void;
21
- /**
22
- * The view's slots: `onSlot` hears each bound one at once, then every change (bound again when
23
- * it's usable again). Returns how to stop. Refused for a module that isn't a view.
24
- */
25
- slots(view: View, onSlot: (slot: string, bound: boolean) => void): () => void;
26
- /** A line for the view's Actor's log, which the Hub bounds as it does a frame's. */
27
- log(view: View, level: "info" | "warn" | "error", message: string, fields?: Record<string, unknown>): void;
28
- }
29
7
  /** The socket a tab or the CLI holds: text messages in, text messages out. */
30
8
  export interface Socket {
31
9
  send(text: string): void;
@@ -34,21 +12,14 @@ export interface Socket {
34
12
  /**
35
13
  * One `/api` socket: the same calls a channel carries, as JSON.
36
14
  *
37
- * - `{id, op: "call", slot, method, input}`, or with `view: {actor, module}` for a view's slot;
38
- * answered `{id, result}` or `{id, error}`.
39
- * - `{sub, op: "subscribe", slot, event}` (or with `view`): then `{sub, payload}` per event, and
40
- * `{sub, end: true}` once it can't go on. `{sub, op: "unsubscribe"}` stops it.
41
- * - `{sub, op: "slots", view}`: the view's slots. First `{sub, payload: {bound: [slot]}}`, every
42
- * one bound now, then `{sub, payload: {slot, bound}}` per change, `bound` again for a slot that's
43
- * usable again. `{sub, op: "unsubscribe"}` stops it, as a subscription.
44
- * - `{op: "log", view, level, message, fields?}`: a line for the view's Actor's log, unanswered.
15
+ * - `{id, op: "call", slot, method, input}`: answered `{id, result}` or `{id, error}`.
16
+ * - `{sub, op: "subscribe", slot, event}`: then `{sub, payload}` per event, and `{sub, end: true}`
17
+ * once it can't go on. `{sub, op: "unsubscribe"}` stops it.
45
18
  *
46
- * A view's calls run as its Actor, for one of its views only, which the Hub checks. Everything else
47
- * runs as the person: through its own slots, and `layout`, its own export.
19
+ * Everything runs as the person, through their slots.
48
20
  */
49
21
  export declare function serveSocket(socket: Socket, deps: {
50
22
  person: PersonSide;
51
- views: ViewCalls;
52
23
  }): {
53
24
  receive(text: string): void;
54
25
  closed(): void;
package/dist/src/api.js CHANGED
@@ -18,17 +18,11 @@ function errorBody(error) {
18
18
  /**
19
19
  * One `/api` socket: the same calls a channel carries, as JSON.
20
20
  *
21
- * - `{id, op: "call", slot, method, input}`, or with `view: {actor, module}` for a view's slot;
22
- * answered `{id, result}` or `{id, error}`.
23
- * - `{sub, op: "subscribe", slot, event}` (or with `view`): then `{sub, payload}` per event, and
24
- * `{sub, end: true}` once it can't go on. `{sub, op: "unsubscribe"}` stops it.
25
- * - `{sub, op: "slots", view}`: the view's slots. First `{sub, payload: {bound: [slot]}}`, every
26
- * one bound now, then `{sub, payload: {slot, bound}}` per change, `bound` again for a slot that's
27
- * usable again. `{sub, op: "unsubscribe"}` stops it, as a subscription.
28
- * - `{op: "log", view, level, message, fields?}`: a line for the view's Actor's log, unanswered.
21
+ * - `{id, op: "call", slot, method, input}`: answered `{id, result}` or `{id, error}`.
22
+ * - `{sub, op: "subscribe", slot, event}`: then `{sub, payload}` per event, and `{sub, end: true}`
23
+ * once it can't go on. `{sub, op: "unsubscribe"}` stops it.
29
24
  *
30
- * A view's calls run as its Actor, for one of its views only, which the Hub checks. Everything else
31
- * runs as the person: through its own slots, and `layout`, its own export.
25
+ * Everything runs as the person, through their slots.
32
26
  */
33
27
  export function serveSocket(socket, deps) {
34
28
  const subscriptions = new Map();
@@ -41,13 +35,6 @@ export function serveSocket(socket, deps) {
41
35
  // The socket is gone; its close stops the rest.
42
36
  }
43
37
  };
44
- const viewOf = (value) => {
45
- if (value === undefined)
46
- return undefined;
47
- if (!isRecord(value) || !isName(value.actor) || !isName(value.module))
48
- throw new Refused("invalid", "A view is {actor, module}.");
49
- return { actor: value.actor, module: value.module };
50
- };
51
38
  const receive = (text) => {
52
39
  let message;
53
40
  try {
@@ -72,10 +59,7 @@ export function serveSocket(socket, deps) {
72
59
  void (async () => {
73
60
  if (!isName(slot) || !isName(method))
74
61
  throw new Refused("invalid", "A call names its slot and method.");
75
- const view = viewOf(message.view);
76
- return view
77
- ? deps.views.call(view, slot, method, input)
78
- : deps.person.call(slot, method, input);
62
+ return deps.person.call(slot, method, input);
79
63
  })()
80
64
  .then((result) => send({ id, result }), (error) => send({ id, error: errorBody(error) }))
81
65
  .finally(() => calls--);
@@ -99,14 +83,11 @@ export function serveSocket(socket, deps) {
99
83
  throw new Refused("invalid", "A subscription names its slot and event.");
100
84
  if (subscriptions.size >= MAX_SUBSCRIPTIONS)
101
85
  throw new Refused("quota", `A socket holds ${MAX_SUBSCRIPTIONS} subscriptions at most.`);
102
- const view = viewOf(message.view);
103
86
  const forward = (payload) => {
104
87
  if (!ended)
105
88
  send({ sub, payload });
106
89
  };
107
- const stop = view
108
- ? deps.views.subscribe(view, slot, event, forward, end)
109
- : deps.person.subscribe(slot, event, forward, end);
90
+ const stop = deps.person.subscribe(slot, event, forward, end);
110
91
  if (ended)
111
92
  stop();
112
93
  else
@@ -118,49 +99,6 @@ export function serveSocket(socket, deps) {
118
99
  }
119
100
  return;
120
101
  }
121
- case "slots": {
122
- const { sub } = message;
123
- if (!isId(sub) || subscriptions.has(sub))
124
- return;
125
- try {
126
- if (subscriptions.size >= MAX_SUBSCRIPTIONS)
127
- throw new Refused("quota", `A socket holds ${MAX_SUBSCRIPTIONS} subscriptions at most.`);
128
- const view = viewOf(message.view);
129
- if (!view)
130
- throw new Refused("invalid", "A slots watch names its view.");
131
- // The Hub tells the bound slots at once, within the call: gathered, they're one snapshot.
132
- let snapshot = [];
133
- const stop = deps.views.slots(view, (slot, bound) => {
134
- if (snapshot && bound)
135
- snapshot.push(slot);
136
- else if (!snapshot)
137
- send({ sub, payload: { slot, bound } });
138
- });
139
- send({ sub, payload: { bound: snapshot } });
140
- snapshot = undefined;
141
- subscriptions.set(sub, stop);
142
- }
143
- catch (error) {
144
- send({ sub, end: true, error: errorBody(error) });
145
- }
146
- return;
147
- }
148
- case "log": {
149
- const { level, message: text, fields } = message;
150
- if (level !== "info" && level !== "warn" && level !== "error")
151
- return;
152
- if (typeof text !== "string")
153
- return;
154
- try {
155
- const view = viewOf(message.view);
156
- if (view)
157
- deps.views.log(view, level, text, isRecord(fields) ? fields : undefined);
158
- }
159
- catch {
160
- // Not one of an Actor's views: nothing is logged, and nothing answers.
161
- }
162
- return;
163
- }
164
102
  case "unsubscribe": {
165
103
  const stop = isId(message.sub) ? subscriptions.get(message.sub) : undefined;
166
104
  if (!stop)
package/dist/src/cli.js CHANGED
@@ -10,11 +10,7 @@ const USAGE = `Usage: agentorg [command] [--home DIR]
10
10
 
11
11
  (no command) [--port N] [--no-open] run AgentOrg: the Hub, your gateway and the shell
12
12
  open sign a browser in to the running app
13
- build [package-dir] build a package into a .agentorg archive, in a
14
- checkout of this repository; installed, use
15
- call packages build '{"files": {...}}'
16
13
  hub [--port N] run a bare Hub, without the shell
17
- add <file.agentorg> hub.packages.add
18
14
  actors | bindings | exports | audit each one's list
19
15
  bind <consumer.slot> <provider.export> hub.grants.bind
20
16
  call <slot> <method> [json] call one of the person's slots
@@ -44,9 +40,9 @@ const { values, positionals } = parseArgs({
44
40
  strict: false,
45
41
  });
46
42
  const [command, ...rest] = positionals;
47
- // `build` and `hub` are the SDK's, which reads their options itself, strictly. Everything else is
43
+ // `hub` is the SDK's, which reads its options itself, strictly. Everything else is
48
44
  // this command's: an option it doesn't know is a mistake, never a reason to start the app.
49
- if (command !== "build" && command !== "hub") {
45
+ if (command !== "hub") {
50
46
  const unknown = Object.keys(values).find((name) => !["home", "port", "no-open", "help", "version"].includes(name));
51
47
  if (unknown !== undefined)
52
48
  usage(`Unknown option ${unknown.length === 1 ? "-" : "--"}${unknown}. An argument that starts with - goes after --.`);
@@ -202,7 +198,7 @@ else if (command === "open") {
202
198
  process.exit(1);
203
199
  }
204
200
  }
205
- else if (command === "build" || command === "hub")
201
+ else if (command === "hub")
206
202
  await import("@agentorg/sdk/cli");
207
203
  else {
208
204
  let call;