@opengeni/sdk 3.3.2-canary.1 → 3.4.0

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 (295) hide show
  1. package/README.md +1007 -72
  2. package/dist/adapters/express.d.ts +31 -0
  3. package/dist/adapters/express.js +87 -0
  4. package/dist/adapters/express.js.map +1 -0
  5. package/dist/adapters/hono.d.ts +16 -0
  6. package/dist/adapters/hono.js +8 -0
  7. package/dist/adapters/hono.js.map +1 -0
  8. package/dist/adapters/next.d.ts +29 -0
  9. package/dist/adapters/next.js +22 -0
  10. package/dist/adapters/next.js.map +1 -0
  11. package/dist/allowance-refusal.d.ts +2 -0
  12. package/dist/allowance-refusal.js +50 -0
  13. package/dist/allowance-refusal.js.map +1 -0
  14. package/dist/app-links.d.ts +42 -0
  15. package/dist/app-links.js +66 -0
  16. package/dist/app-links.js.map +1 -0
  17. package/dist/artifact-catalog.d.ts +40 -0
  18. package/dist/artifact-client.d.ts +20 -8
  19. package/dist/artifacts.d.ts +1 -1
  20. package/dist/artifacts.js +8 -6
  21. package/dist/automations.js +8 -2
  22. package/dist/automations.js.map +1 -1
  23. package/dist/azure-live-transport.d.ts +32 -0
  24. package/dist/browser.d.ts +6 -2
  25. package/dist/browser.js +25 -5
  26. package/dist/chat/fold.d.ts +75 -0
  27. package/dist/chat/handler.d.ts +41 -0
  28. package/dist/chat/http.d.ts +67 -0
  29. package/dist/chat/ids.d.ts +34 -0
  30. package/dist/chat/index.d.ts +9 -0
  31. package/dist/chat/index.js +1369 -0
  32. package/dist/chat/index.js.map +1 -0
  33. package/dist/chat/openai.d.ts +30 -0
  34. package/dist/chat/opengeni.d.ts +129 -0
  35. package/dist/chat/types.d.ts +207 -0
  36. package/dist/chat/vercel.d.ts +49 -0
  37. package/dist/chats.d.ts +8 -0
  38. package/dist/chunk-4AHR4KGA.js +301 -0
  39. package/dist/chunk-4AHR4KGA.js.map +1 -0
  40. package/dist/chunk-6GYGMLX4.js +332 -0
  41. package/dist/chunk-6GYGMLX4.js.map +1 -0
  42. package/dist/chunk-7EWT3PV6.js +368 -0
  43. package/dist/chunk-7EWT3PV6.js.map +1 -0
  44. package/dist/chunk-FTWVVPR6.js +1665 -0
  45. package/dist/chunk-FTWVVPR6.js.map +1 -0
  46. package/dist/{chunk-VYCTL62C.js → chunk-GZQSWAKD.js} +1 -1
  47. package/dist/chunk-GZQSWAKD.js.map +1 -0
  48. package/dist/chunk-KGEABAID.js +71 -0
  49. package/dist/chunk-KGEABAID.js.map +1 -0
  50. package/dist/{chunk-YKZME56C.js → chunk-KWADQ227.js} +3 -3
  51. package/dist/{chunk-YKZME56C.js.map → chunk-KWADQ227.js.map} +1 -1
  52. package/dist/{chunk-E5BNEG2D.js → chunk-NCU5OAN7.js} +27 -2
  53. package/dist/chunk-NCU5OAN7.js.map +1 -0
  54. package/dist/{chunk-BESZFWMU.js → chunk-OI5YLASH.js} +73 -2
  55. package/dist/chunk-OI5YLASH.js.map +1 -0
  56. package/dist/chunk-OV3POE62.js +359 -0
  57. package/dist/chunk-OV3POE62.js.map +1 -0
  58. package/dist/chunk-P7UWKORG.js +561 -0
  59. package/dist/chunk-P7UWKORG.js.map +1 -0
  60. package/dist/chunk-QGEIL4TR.js +195 -0
  61. package/dist/chunk-QGEIL4TR.js.map +1 -0
  62. package/dist/{chunk-GY6RQPCG.js → chunk-S2NLS3ES.js} +3598 -1278
  63. package/dist/chunk-S2NLS3ES.js.map +1 -0
  64. package/dist/chunk-UM4ZXXJR.js +9 -0
  65. package/dist/chunk-UM4ZXXJR.js.map +1 -0
  66. package/dist/{chunk-TFCLHRZK.js → chunk-UME3X7CP.js} +28 -32
  67. package/dist/chunk-UME3X7CP.js.map +1 -0
  68. package/dist/chunk-VT4YM4XQ.js +210 -0
  69. package/dist/chunk-VT4YM4XQ.js.map +1 -0
  70. package/dist/chunk-WSYYXONG.js +1602 -0
  71. package/dist/chunk-WSYYXONG.js.map +1 -0
  72. package/dist/{chunk-6NEPJ7NC.js → chunk-WVCJRSGJ.js} +119 -14
  73. package/dist/chunk-WVCJRSGJ.js.map +1 -0
  74. package/dist/chunk-X5ITQVXE.js +15 -0
  75. package/dist/chunk-X5ITQVXE.js.map +1 -0
  76. package/dist/chunk-XA7HVIHI.js +126 -0
  77. package/dist/chunk-XA7HVIHI.js.map +1 -0
  78. package/dist/{chunk-QTBAMHEF.js → chunk-YJCHB7R3.js} +60 -12
  79. package/dist/chunk-YJCHB7R3.js.map +1 -0
  80. package/dist/chunk-YKIHNLWD.js +153 -0
  81. package/dist/chunk-YKIHNLWD.js.map +1 -0
  82. package/dist/chunk-ZDUBPV33.js +277 -0
  83. package/dist/chunk-ZDUBPV33.js.map +1 -0
  84. package/dist/client.d.ts +1056 -118
  85. package/dist/codex-realtime-controller.d.ts +13 -0
  86. package/dist/codex-realtime-controller.js +4 -2
  87. package/dist/codex-realtime-v3-wire.d.ts +3 -2
  88. package/dist/codex-realtime.d.ts +5 -1
  89. package/dist/company-brain.d.ts +1 -11
  90. package/dist/core.d.ts +6 -2
  91. package/dist/core.js +23 -6
  92. package/dist/deprecation.d.ts +47 -0
  93. package/dist/document-authority-client.d.ts +24 -2
  94. package/dist/document-authority.js +7 -5
  95. package/dist/editable-artifacts/index.d.ts +1 -1
  96. package/dist/editable-artifacts/types.d.ts +12 -2
  97. package/dist/editable-artifacts.js +19 -3
  98. package/dist/editable-artifacts.js.map +1 -1
  99. package/dist/embedding-client.d.ts +129 -0
  100. package/dist/errors.d.ts +64 -1
  101. package/dist/external-vault-receiver.d.ts +203 -0
  102. package/dist/external-vault-receiver.js +564 -0
  103. package/dist/external-vault-receiver.js.map +1 -0
  104. package/dist/feedback.d.ts +23 -0
  105. package/dist/gateway-realtime-transport.js +1 -1
  106. package/dist/github-repositories.d.ts +10 -0
  107. package/dist/github-repositories.js +36 -0
  108. package/dist/github-repositories.js.map +1 -0
  109. package/dist/index.d.ts +61 -11
  110. package/dist/index.js +555 -338
  111. package/dist/index.js.map +1 -1
  112. package/dist/insights-usage.d.ts +33 -0
  113. package/dist/insights-usage.js +7 -0
  114. package/dist/insights-usage.js.map +1 -0
  115. package/dist/interaction.d.ts +165 -71
  116. package/dist/interaction.js +2 -2
  117. package/dist/knowledge.d.ts +7 -0
  118. package/dist/knowledge.js +20 -0
  119. package/dist/knowledge.js.map +1 -0
  120. package/dist/known-tools.d.ts +4 -0
  121. package/dist/message-links.d.ts +68 -0
  122. package/dist/model-context.d.ts +68 -0
  123. package/dist/model-display.d.ts +2 -0
  124. package/dist/model-display.js +17 -0
  125. package/dist/model-display.js.map +1 -0
  126. package/dist/organization-integration-policy.d.ts +26 -0
  127. package/dist/organization-integration-policy.js +26 -0
  128. package/dist/organization-integration-policy.js.map +1 -0
  129. package/dist/pr-review-client.d.ts +1 -1
  130. package/dist/pr-review.d.ts +1 -1
  131. package/dist/pr-review.js.map +1 -1
  132. package/dist/preference-registry.d.ts +1 -1
  133. package/dist/prepared-mcp-setup.d.ts +5 -0
  134. package/dist/proxy.d.ts +13 -11
  135. package/dist/realtime.d.ts +1 -1
  136. package/dist/realtime.js +253 -7
  137. package/dist/realtime.js.map +1 -1
  138. package/dist/session-history-import.d.ts +22 -0
  139. package/dist/session-history-import.js +36 -0
  140. package/dist/session-history-import.js.map +1 -0
  141. package/dist/session-list-entries.d.ts +1 -0
  142. package/dist/session-list-entries.js +7 -0
  143. package/dist/session-list-entries.js.map +1 -0
  144. package/dist/session-message-search.d.ts +53 -0
  145. package/dist/session-proxy-client.d.ts +28 -0
  146. package/dist/session-proxy.d.ts +334 -0
  147. package/dist/session-proxy.js +24 -0
  148. package/dist/session-proxy.js.map +1 -0
  149. package/dist/session-titles.d.ts +2 -28
  150. package/dist/session-usage-limits.d.ts +10 -0
  151. package/dist/session-usage-limits.js +43 -0
  152. package/dist/session-usage-limits.js.map +1 -0
  153. package/dist/shared-catalog-loader.d.ts +34 -0
  154. package/dist/site-ai.d.ts +35 -0
  155. package/dist/site-bridge-host.d.ts +112 -0
  156. package/dist/site-bridge-host.js +286 -0
  157. package/dist/site-bridge-host.js.map +1 -0
  158. package/dist/site-browser-entry.d.ts +1 -0
  159. package/dist/site-browser-runtime.gen.d.ts +1 -0
  160. package/dist/site-data.d.ts +178 -0
  161. package/dist/site-document.d.ts +5 -0
  162. package/dist/site-document.js +20 -0
  163. package/dist/site-document.js.map +1 -0
  164. package/dist/site-document.test.d.ts +1 -0
  165. package/dist/site-http.d.ts +17 -0
  166. package/dist/site-sql.d.ts +31 -0
  167. package/dist/site-tool-bridge.d.ts +57 -0
  168. package/dist/site.d.ts +186 -0
  169. package/dist/site.js +63 -0
  170. package/dist/site.js.map +1 -0
  171. package/dist/skills.d.ts +75 -0
  172. package/dist/sse.d.ts +1 -1
  173. package/dist/stream.d.ts +2 -0
  174. package/dist/tenant-workspaces.d.ts +47 -0
  175. package/dist/tenant-workspaces.js +8 -0
  176. package/dist/tenant-workspaces.js.map +1 -0
  177. package/dist/tool-auth.d.ts +87 -0
  178. package/dist/tool-auth.js +23 -0
  179. package/dist/tool-auth.js.map +1 -0
  180. package/dist/tool-display-metadata.d.ts +3 -0
  181. package/dist/tools-37DCTCNZ.js +12 -0
  182. package/dist/tools-37DCTCNZ.js.map +1 -0
  183. package/dist/tools.d.ts +81 -0
  184. package/dist/transcription.d.ts +1 -1
  185. package/dist/types.d.ts +2785 -592
  186. package/dist/usage-allowances.d.ts +36 -0
  187. package/dist/usage-allowances.js +80 -0
  188. package/dist/usage-allowances.js.map +1 -0
  189. package/dist/vault-types.d.ts +324 -0
  190. package/dist/vaults-api.d.ts +65 -0
  191. package/dist/workspace-artifacts.d.ts +47 -5
  192. package/dist/workspace-instruction-policies.d.ts +2 -2
  193. package/dist/workspace-integrations.d.ts +74 -0
  194. package/dist/workspace-integrations.js +67 -0
  195. package/dist/workspace-integrations.js.map +1 -0
  196. package/dist/workspace-state.d.ts +8 -71
  197. package/package.json +99 -5
  198. package/src/adapters/express.ts +136 -0
  199. package/src/adapters/hono.ts +18 -0
  200. package/src/adapters/next.ts +42 -0
  201. package/src/allowance-refusal.ts +5 -0
  202. package/src/app-links.ts +117 -0
  203. package/src/artifact-catalog.ts +51 -0
  204. package/src/artifact-client.ts +40 -49
  205. package/src/artifacts.ts +1 -0
  206. package/src/automations-client.ts +8 -2
  207. package/src/azure-live-transport.ts +286 -0
  208. package/src/browser.ts +35 -0
  209. package/src/chat/fold.ts +431 -0
  210. package/src/chat/handler.ts +240 -0
  211. package/src/chat/http.ts +257 -0
  212. package/src/chat/ids.ts +85 -0
  213. package/src/chat/index.ts +88 -0
  214. package/src/chat/openai.ts +439 -0
  215. package/src/chat/opengeni.ts +737 -0
  216. package/src/chat/types.ts +219 -0
  217. package/src/chat/vercel.ts +204 -0
  218. package/src/chats.ts +16 -0
  219. package/src/client.ts +5595 -1847
  220. package/src/codex-realtime-controller.ts +163 -8
  221. package/src/codex-realtime-v3-wire.ts +4 -3
  222. package/src/codex-realtime-v3.ts +12 -3
  223. package/src/codex-realtime.ts +11 -3
  224. package/src/company-brain.ts +0 -40
  225. package/src/core.ts +18 -0
  226. package/src/deprecation.ts +149 -0
  227. package/src/document-authority-client.ts +104 -1
  228. package/src/editable-artifacts/controller.ts +36 -2
  229. package/src/editable-artifacts/index.ts +1 -0
  230. package/src/editable-artifacts/types.ts +29 -16
  231. package/src/embedding-client.ts +519 -0
  232. package/src/errors.ts +281 -5
  233. package/src/external-vault-receiver.ts +914 -0
  234. package/src/feedback.ts +20 -0
  235. package/src/gateway-realtime-transport.ts +3 -3
  236. package/src/github-repositories.ts +60 -0
  237. package/src/index.ts +592 -65
  238. package/src/insights-usage.ts +64 -0
  239. package/src/interaction.ts +315 -93
  240. package/src/knowledge.ts +72 -0
  241. package/src/known-tools.ts +279 -0
  242. package/src/message-links.ts +178 -0
  243. package/src/model-context.ts +113 -0
  244. package/src/model-display.ts +11 -0
  245. package/src/organization-integration-policy.ts +54 -0
  246. package/src/pr-review-client.ts +1 -1
  247. package/src/pr-review.ts +1 -1
  248. package/src/preference-registry.ts +2 -0
  249. package/src/prepared-mcp-setup.ts +123 -0
  250. package/src/proxy.ts +23 -12
  251. package/src/realtime.ts +15 -6
  252. package/src/session-history-import.ts +84 -0
  253. package/src/session-list-entries.ts +1 -0
  254. package/src/session-message-search.ts +47 -0
  255. package/src/session-proxy-client.ts +68 -0
  256. package/src/session-proxy.ts +2098 -0
  257. package/src/session-titles.ts +8 -82
  258. package/src/session-usage-limits.ts +71 -0
  259. package/src/shared-catalog-loader.ts +169 -0
  260. package/src/site-ai.ts +284 -0
  261. package/src/site-bridge-host.ts +422 -0
  262. package/src/site-browser-entry.ts +6 -0
  263. package/src/site-browser-runtime.gen.ts +2 -0
  264. package/src/site-data.ts +372 -0
  265. package/src/site-document.test.ts +64 -0
  266. package/src/site-document.ts +18 -0
  267. package/src/site-http.ts +171 -0
  268. package/src/site-sql.ts +153 -0
  269. package/src/site-tool-bridge.ts +401 -0
  270. package/src/site.ts +878 -0
  271. package/src/skills.ts +69 -0
  272. package/src/sse.ts +9 -3
  273. package/src/stream.ts +33 -10
  274. package/src/tenant-workspaces.ts +148 -0
  275. package/src/tool-auth.ts +301 -0
  276. package/src/tool-display-metadata.ts +27 -0
  277. package/src/tools.ts +452 -0
  278. package/src/transcription.ts +3 -0
  279. package/src/types.ts +3142 -594
  280. package/src/usage-allowances.ts +189 -0
  281. package/src/vault-types.ts +379 -0
  282. package/src/vaults-api.ts +339 -0
  283. package/src/workspace-artifacts.ts +55 -6
  284. package/src/workspace-instruction-policies.ts +2 -1
  285. package/src/workspace-integrations.ts +402 -0
  286. package/src/workspace-state.ts +8 -82
  287. package/dist/chunk-6NEPJ7NC.js.map +0 -1
  288. package/dist/chunk-BESZFWMU.js.map +0 -1
  289. package/dist/chunk-E5BNEG2D.js.map +0 -1
  290. package/dist/chunk-GY6RQPCG.js.map +0 -1
  291. package/dist/chunk-OO5LPTO7.js +0 -132
  292. package/dist/chunk-OO5LPTO7.js.map +0 -1
  293. package/dist/chunk-QTBAMHEF.js.map +0 -1
  294. package/dist/chunk-TFCLHRZK.js.map +0 -1
  295. package/dist/chunk-VYCTL62C.js.map +0 -1
@@ -0,0 +1,219 @@
1
+ import type { FetchLike } from "../client";
2
+ import type { Chats } from "../chats";
3
+ import type { WorkspaceIdResolverOptions } from "../tenant-workspaces";
4
+ import type {
5
+ CreateSessionRequest,
6
+ AgentConfigRequest,
7
+ HumanInputAnswer,
8
+ LatencyMode,
9
+ ReasoningEffort,
10
+ SessionEvent,
11
+ Session,
12
+ SessionSkill,
13
+ ToolRef,
14
+ } from "../types";
15
+
16
+ /** Which other sessions the agent may reach from this one. */
17
+ export type ChatAgentAccess = "session" | "user" | "workspace";
18
+
19
+ /**
20
+ * Compatibility option for personal or workspace Knowledge. `false` initializes
21
+ * Knowledge authoring to Off; authorized retrieval remains available. Omitted
22
+ * follows `chats`: private with a user, or legacy reach-based defaults without one.
23
+ * Task notes cover temporary session-tree data.
24
+ */
25
+ export type ChatMemory = "user" | "workspace" | false;
26
+
27
+ export type OpenGeniOptions = {
28
+ /** Organization API key. Keep it on the server. */
29
+ apiKey: string;
30
+ /**
31
+ * The organization (account) id that owns every workspace. Optional: when
32
+ * omitted it is read once from the API key.
33
+ */
34
+ organizationId?: string | undefined;
35
+ /** Defaults to `https://app.opengeni.ai`. */
36
+ baseUrl?: string | undefined;
37
+ /**
38
+ * The product identity used as the external-source label for tenant
39
+ * workspaces and end users. Defaults to `"app"`.
40
+ */
41
+ source?: string | undefined;
42
+ fetch?: FetchLike | undefined;
43
+ /** Display name for a tenant workspace created on first use. Defaults to the tenant id. */
44
+ workspaceName?: ((tenant: string) => string) | undefined;
45
+ /**
46
+ * Per-user workspaces only (`{ user }` alone, or `chats: "isolated"`):
47
+ * permissions for the one owner the SDK adds explicitly. Tenant workspaces
48
+ * rely on Opengeni, which adds a user on their first request. Both default
49
+ * to workspace read, session create/read/control (including sending
50
+ * messages), file upload/read, and per-session MCP attachment; no admin
51
+ * permissions. Never updates an existing membership: use
52
+ * `client.updateExternalWorkspaceMember` for that.
53
+ */
54
+ memberPermissions?: WorkspaceIdResolverOptions["memberPermissions"];
55
+ };
56
+
57
+ /**
58
+ * Which workspace, in your own ids: `tenant` (one workspace per tenant),
59
+ * neither (the `user`'s own workspace), or an explicit `workspaceId`.
60
+ */
61
+ export type ChatTarget =
62
+ | { tenant: string; workspaceId?: undefined }
63
+ | { workspaceId: string; tenant?: undefined }
64
+ | { user: string; tenant?: undefined; workspaceId?: undefined };
65
+
66
+ /**
67
+ * A workspace in your own ids, for `og.workspaceId(...)`: `{ tenant }` (one
68
+ * workspace per tenant), `{ user }` (one workspace per user), or
69
+ * `{ workspaceId }`. Precedence: workspaceId, then tenant, then user.
70
+ */
71
+ export type WorkspaceTarget = {
72
+ tenant?: string | undefined;
73
+ user?: string | undefined;
74
+ workspaceId?: string | undefined;
75
+ };
76
+
77
+ export type ChatOptions = ChatTarget & {
78
+ /**
79
+ * Host-authenticated external user; resolved through server-side asUser().
80
+ * Without a tenant or workspaceId, the user gets one workspace of their own.
81
+ */
82
+ user?: string | undefined;
83
+ /** Stable conversation id; the session id is derived from it deterministically. */
84
+ conversation: string;
85
+ /** Prefer the actual Opengeni session ID for shared or existing conversations.
86
+ * The acting user is independent of the conversation's identity. */
87
+ sessionId?: string | undefined;
88
+ /**
89
+ * Private (default with a user) uses personal Knowledge and session-only reach;
90
+ * shared uses workspace Knowledge/reach, isolated adds a workspace per tenant/user.
91
+ * Without a user, omission keeps workspace visibility, session reach and Knowledge off.
92
+ */
93
+ chats?: Chats | undefined;
94
+ /** Agent identity, capabilities and instructions; implicit markdown falls back on older servers. */
95
+ agent?: AgentConfigRequest | undefined;
96
+ /** Explicit agent reach overrides the `chats` default. */
97
+ agentAccess?: ChatAgentAccess | undefined;
98
+ memory?: ChatMemory | undefined;
99
+ model?: string | undefined;
100
+ instructions?: string | undefined;
101
+ skills?: SessionSkill[] | undefined;
102
+ tools?: ToolRef[] | undefined;
103
+ /**
104
+ * Raw create-request passthrough (for example `sandboxBackend: "none"` for a
105
+ * pure chat). Explicit values win over facade-derived ones; the message and
106
+ * identity/idempotency fields always come from the facade.
107
+ */
108
+ create?: Partial<CreateSessionRequest> | undefined;
109
+ };
110
+
111
+ /** A turn stopped to wait for a human decision. */
112
+ export type ChatPending = {
113
+ kind: "approval" | "human_input";
114
+ /** The id to pass back through `chat.respond`. */
115
+ requestId: string;
116
+ /** Tool name for an approval; null for structured human input. */
117
+ name: string | null;
118
+ /** The producer's exact payload (approval entry or human-input request). */
119
+ payload: unknown;
120
+ };
121
+
122
+ export type ChatReplyStatus = "completed" | "pending" | "cancelled";
123
+
124
+ export type ChatReply = {
125
+ text: string;
126
+ sessionId: string;
127
+ workspaceId: string;
128
+ turnId: string | null;
129
+ /** `pending` waits on `chat.respond`; `cancelled` carries the text produced before the cancel. */
130
+ status: ChatReplyStatus;
131
+ /** Set when the turn is waiting on `chat.respond` instead of finished. */
132
+ pending: ChatPending | null;
133
+ /** A non-destructive completion without a model-authored final answer. */
134
+ emptyFinalReply?: true;
135
+ /** Display this informational notice when emptyFinalReply is present. */
136
+ notice?: string;
137
+ /** Every event consumed while folding this turn. */
138
+ events: SessionEvent[];
139
+ toString(): string;
140
+ };
141
+
142
+ export type ChatToolStatus = "started" | "completed" | "failed";
143
+
144
+ export type ChatChunk =
145
+ | { type: "text"; text: string }
146
+ | {
147
+ type: "tool";
148
+ name: string;
149
+ status: ChatToolStatus;
150
+ callId?: string | undefined;
151
+ input?: unknown;
152
+ }
153
+ | { type: "pending"; pending: ChatPending }
154
+ | { type: "done"; reply: ChatReply };
155
+
156
+ export type ChatMessage = {
157
+ role: "user" | "assistant";
158
+ text: string;
159
+ sequence: number;
160
+ };
161
+
162
+ /** Durable text and unresolved decisions restored when a chat is reopened. */
163
+ export type ChatSnapshot = {
164
+ messages: ChatMessage[];
165
+ pending: ChatPending[];
166
+ status: Session["status"] | null;
167
+ };
168
+
169
+ export type ChatRespondInput =
170
+ | { requestId: string; decision: "approve" | "reject"; message?: string | undefined }
171
+ | { requestId: string; answers: HumanInputAnswer[] }
172
+ | { requestId: string; skip: true };
173
+
174
+ /** One earlier message from the product's own history, imported as context on the first send. */
175
+ export type ChatImportedMessage = { role: "user" | "assistant" | "system"; text: string };
176
+
177
+ export type ChatSendOptions = {
178
+ signal?: AbortSignal | undefined;
179
+ /** Supersede the current inference instead of queueing behind it. */
180
+ steer?: boolean | undefined;
181
+ /**
182
+ * Earlier messages the product already holds, oldest first. Used only when
183
+ * this send creates the session: they become the first message's
184
+ * `modelContext` (skipped when `create.modelContext` was supplied), trimmed
185
+ * from the oldest end to 30,000 characters. Never resent on later turns:
186
+ * after the first message Opengeni owns the history.
187
+ */
188
+ importedHistory?: ChatImportedMessage[] | undefined;
189
+ /** Model for this message's turn; omitted keeps the session's current policy. */
190
+ model?: string | undefined;
191
+ reasoningEffort?: ReasoningEffort | undefined;
192
+ latencyMode?: LatencyMode | undefined;
193
+ /**
194
+ * Model-visible application context for this one message (for example the
195
+ * page or record the user is looking at). Not shown in the transcript and not
196
+ * secret. On the creating message it is combined with imported history.
197
+ */
198
+ modelContext?: string | undefined;
199
+ };
200
+
201
+ export type ChatSessionListOptions = ChatTarget & {
202
+ user?: string | undefined;
203
+ /** Match the chat's mode; isolated lists that user's separate workspace. */
204
+ chats?: Chats | undefined;
205
+ limit?: number | undefined;
206
+ };
207
+
208
+ /** A turn ended without a usable reply, or the facade was misused. */
209
+ export class OpenGeniChatError extends Error {
210
+ readonly code: string;
211
+ readonly event: SessionEvent | null;
212
+
213
+ constructor(code: string, message: string, event: SessionEvent | null = null) {
214
+ super(message);
215
+ this.name = "OpenGeniChatError";
216
+ this.code = code;
217
+ this.event = event;
218
+ }
219
+ }
@@ -0,0 +1,204 @@
1
+ import { asRecord } from "./fold";
2
+ import {
3
+ chatErrorSummary,
4
+ clientConversation,
5
+ errorResponse,
6
+ importedHistoryBefore,
7
+ lastUserMessageText,
8
+ openResolvedChat,
9
+ readJsonObject,
10
+ resolveChatRequest,
11
+ sseByteStream,
12
+ sseHeaders,
13
+ sseLine,
14
+ type ChatResolve,
15
+ } from "./http";
16
+ import type { Opengeni } from "./opengeni";
17
+ import type { ChatChunk, ChatPending } from "./types";
18
+
19
+ /**
20
+ * Vercel AI SDK UI message stream (protocol v1, as consumed by `useChat` with
21
+ * the default transport in AI SDK 5, 6, and 7; approval requests need 6+).
22
+ * Parts are `data: <json>` SSE
23
+ * lines terminated by `data: [DONE]`, under the
24
+ * `x-vercel-ai-ui-message-stream: v1` header.
25
+ */
26
+
27
+ export const UI_MESSAGE_STREAM_HEADER = "x-vercel-ai-ui-message-stream";
28
+ export const UI_MESSAGE_STREAM_VERSION = "v1";
29
+
30
+ export type UIMessageStreamOptions = {
31
+ /** Assistant message id announced in the `start` part. Defaults to a random UUID. */
32
+ messageId?: string | undefined;
33
+ /**
34
+ * Emit Opengeni's own tool activity as tool parts. Off by default: those
35
+ * tools are not in the app's typed tool set. When on, parts are marked
36
+ * `dynamic` and `providerExecuted`, so `useChat` renders them as
37
+ * `dynamic-tool` parts and never tries to execute them. Approval requests
38
+ * are always emitted (as dynamic tool parts) because answering them is part
39
+ * of the conversation.
40
+ */
41
+ toolParts?: boolean | undefined;
42
+ /**
43
+ * Emit the `start` / `start-step` / `finish-step` / `finish` framing. Set to
44
+ * false when writing into an existing AI SDK stream that owns its framing
45
+ * (for example `createUIMessageStream({ execute: ({ writer }) => ... })`).
46
+ * Defaults to true.
47
+ */
48
+ framing?: boolean | undefined;
49
+ };
50
+
51
+ /** Protocol parts for one reply, in order. */
52
+ export async function* uiMessageStreamParts(
53
+ chunks: AsyncIterable<ChatChunk>,
54
+ options: UIMessageStreamOptions = {},
55
+ ): AsyncGenerator<Record<string, unknown>, void, void> {
56
+ const messageId = options.messageId ?? crypto.randomUUID();
57
+ const textId = `text_${messageId}`;
58
+ let textOpen = false;
59
+ const closeText = (): Record<string, unknown>[] => {
60
+ if (!textOpen) return [];
61
+ textOpen = false;
62
+ return [{ type: "text-end", id: textId }];
63
+ };
64
+ const framing = options.framing ?? true;
65
+ if (framing) {
66
+ yield { type: "start", messageId };
67
+ yield { type: "start-step" };
68
+ }
69
+ try {
70
+ for await (const chunk of chunks) {
71
+ switch (chunk.type) {
72
+ case "text":
73
+ if (!textOpen) {
74
+ textOpen = true;
75
+ yield { type: "text-start", id: textId };
76
+ }
77
+ yield { type: "text-delta", id: textId, delta: chunk.text };
78
+ break;
79
+ case "tool": {
80
+ if (!options.toolParts) break;
81
+ yield* closeText();
82
+ const toolCallId = chunk.callId ?? `call_${crypto.randomUUID()}`;
83
+ const tool = { toolCallId, dynamic: true, providerExecuted: true };
84
+ if (chunk.status === "started") {
85
+ yield { type: "tool-input-start", ...tool, toolName: chunk.name };
86
+ yield {
87
+ type: "tool-input-available",
88
+ ...tool,
89
+ toolName: chunk.name,
90
+ input: chunk.input ?? {},
91
+ };
92
+ } else if (chunk.status === "failed") {
93
+ yield { type: "tool-output-error", ...tool, errorText: "The tool call failed." };
94
+ } else {
95
+ yield { type: "tool-output-available", ...tool, output: { status: chunk.status } };
96
+ }
97
+ break;
98
+ }
99
+ case "pending":
100
+ yield* closeText();
101
+ yield* pendingParts(chunk.pending);
102
+ break;
103
+ case "done":
104
+ yield* closeText();
105
+ break;
106
+ }
107
+ }
108
+ yield* closeText();
109
+ if (framing) {
110
+ yield { type: "finish-step" };
111
+ yield { type: "finish" };
112
+ }
113
+ } catch (error) {
114
+ yield* closeText();
115
+ yield { type: "error", errorText: chatErrorSummary(error).message };
116
+ }
117
+ }
118
+
119
+ function* pendingParts(pending: ChatPending): Generator<Record<string, unknown>> {
120
+ if (pending.kind === "approval") {
121
+ const raw = asRecord(pending.payload);
122
+ const rawItem = asRecord(raw.rawItem);
123
+ yield {
124
+ type: "tool-input-available",
125
+ toolCallId: pending.requestId,
126
+ toolName: pending.name ?? "tool",
127
+ dynamic: true,
128
+ providerExecuted: true,
129
+ input: raw.arguments ?? rawItem.arguments ?? {},
130
+ };
131
+ yield {
132
+ type: "tool-approval-request",
133
+ toolCallId: pending.requestId,
134
+ approvalId: pending.requestId,
135
+ };
136
+ return;
137
+ }
138
+ yield { type: "data-opengeni-pending", data: pending };
139
+ }
140
+
141
+ export async function* uiMessageStreamBlocks(
142
+ chunks: AsyncIterable<ChatChunk>,
143
+ options: UIMessageStreamOptions = {},
144
+ ): AsyncGenerator<string, void, void> {
145
+ for await (const part of uiMessageStreamParts(chunks, options)) {
146
+ yield sseLine(JSON.stringify(part));
147
+ }
148
+ yield sseLine("[DONE]");
149
+ }
150
+
151
+ export function chatChunksToUIMessageStream(
152
+ chunks: AsyncIterable<ChatChunk>,
153
+ options: UIMessageStreamOptions & { onCancel?: (() => void) | undefined } = {},
154
+ ): ReadableStream<Uint8Array> {
155
+ return sseByteStream(uiMessageStreamBlocks(chunks, options), options.onCancel);
156
+ }
157
+
158
+ export function uiMessageStreamResponse(
159
+ chunks: AsyncIterable<ChatChunk>,
160
+ options: UIMessageStreamOptions = {},
161
+ ): Response {
162
+ return new Response(chatChunksToUIMessageStream(chunks, options), {
163
+ headers: sseHeaders({ [UI_MESSAGE_STREAM_HEADER]: UI_MESSAGE_STREAM_VERSION }),
164
+ });
165
+ }
166
+
167
+ /** Text of the last user `UIMessage` (`parts` of type `text`, joined). */
168
+ export function lastUIMessageText(messages: unknown): string | null {
169
+ return lastUserMessageText(messages, ["text"], "parts");
170
+ }
171
+
172
+ /**
173
+ * `useChat` request handler: body `{ id, messages, trigger, messageId }`.
174
+ * Conversation: the host's `resolve`, else the `x-opengeni-conversation`
175
+ * header, else the chat `id`. Only the last user message is sent; the
176
+ * messages before it are imported once as context when this request creates
177
+ * the session. `regenerate-message` steers instead of queueing.
178
+ */
179
+ export async function handleVercelChatRequest(
180
+ og: Opengeni,
181
+ request: Request,
182
+ resolve: ChatResolve,
183
+ options: Pick<UIMessageStreamOptions, "toolParts"> = {},
184
+ ): Promise<Response> {
185
+ const body = await readJsonObject(request);
186
+ const prompt = lastUIMessageText(body?.messages)?.trim() ?? "";
187
+ if (!prompt) {
188
+ return errorResponse(400, "The last user message has no text.", "message_required");
189
+ }
190
+ const resolved = await resolveChatRequest(request, resolve);
191
+ if (resolved.response) return resolved.response;
192
+ const opened = await openResolvedChat(
193
+ og,
194
+ resolved.resolution,
195
+ clientConversation(request, body?.id),
196
+ );
197
+ if (opened.response) return opened.response;
198
+ const steer = body?.trigger === "regenerate-message";
199
+ const importedHistory = importedHistoryBefore(body?.messages, ["text"], "parts");
200
+ return uiMessageStreamResponse(
201
+ opened.chat.stream(prompt, { signal: request.signal, steer, importedHistory }),
202
+ options.toolParts ? { toolParts: true } : {},
203
+ );
204
+ }
package/src/chats.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { CreateSessionRequest } from "./types";
2
+
3
+ /**
4
+ * Private chats use personal Knowledge and session-only agent reach; shared chats
5
+ * use workspace Knowledge and reach. Isolated adds a separate workspace per user.
6
+ */
7
+ export type Chats = "private" | "shared" | "isolated";
8
+
9
+ /** SDK defaults only: the API still authorizes visibility, agent reach and Knowledge separately. */
10
+ export function chatDefaults(
11
+ chats: Chats,
12
+ ): Required<Pick<CreateSessionRequest, "visibility" | "agentAccess" | "memoryScope">> {
13
+ return chats === "shared"
14
+ ? { visibility: "workspace", agentAccess: "workspace", memoryScope: "workspace" }
15
+ : { visibility: "private", agentAccess: "session", memoryScope: "user" };
16
+ }