@opengeni/sdk 3.1.0 → 3.1.2
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.
- package/README.md +1037 -71
- package/dist/accounts.d.ts +5 -1
- package/dist/accounts.js +13 -1
- package/dist/accounts.js.map +1 -1
- package/dist/adapters/express.d.ts +31 -0
- package/dist/adapters/express.js +87 -0
- package/dist/adapters/express.js.map +1 -0
- package/dist/adapters/hono.d.ts +16 -0
- package/dist/adapters/hono.js +8 -0
- package/dist/adapters/hono.js.map +1 -0
- package/dist/adapters/next.d.ts +29 -0
- package/dist/adapters/next.js +22 -0
- package/dist/adapters/next.js.map +1 -0
- package/dist/allowance-refusal.d.ts +2 -0
- package/dist/allowance-refusal.js +50 -0
- package/dist/allowance-refusal.js.map +1 -0
- package/dist/app-links.d.ts +42 -0
- package/dist/app-links.js +66 -0
- package/dist/app-links.js.map +1 -0
- package/dist/artifact-catalog.d.ts +40 -0
- package/dist/artifact-client.d.ts +20 -8
- package/dist/artifacts.d.ts +1 -1
- package/dist/artifacts.js +8 -6
- package/dist/automations.js +8 -2
- package/dist/automations.js.map +1 -1
- package/dist/azure-live-transport.d.ts +32 -0
- package/dist/browser.d.ts +7 -2
- package/dist/browser.js +27 -5
- package/dist/chat/fold.d.ts +75 -0
- package/dist/chat/handler.d.ts +41 -0
- package/dist/chat/http.d.ts +67 -0
- package/dist/chat/ids.d.ts +34 -0
- package/dist/chat/index.d.ts +9 -0
- package/dist/chat/index.js +1703 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/chat/openai.d.ts +30 -0
- package/dist/chat/opengeni.d.ts +129 -0
- package/dist/chat/types.d.ts +207 -0
- package/dist/chat/vercel.d.ts +49 -0
- package/dist/chats.d.ts +8 -0
- package/dist/chunk-4AHR4KGA.js +301 -0
- package/dist/chunk-4AHR4KGA.js.map +1 -0
- package/dist/chunk-6HZGAIUE.js +1665 -0
- package/dist/chunk-6HZGAIUE.js.map +1 -0
- package/dist/chunk-7EWT3PV6.js +368 -0
- package/dist/chunk-7EWT3PV6.js.map +1 -0
- package/dist/{chunk-VYCTL62C.js → chunk-GZQSWAKD.js} +1 -1
- package/dist/chunk-GZQSWAKD.js.map +1 -0
- package/dist/chunk-JXXCTW5W.js +1392 -0
- package/dist/chunk-JXXCTW5W.js.map +1 -0
- package/dist/{chunk-YTAWBDO2.js → chunk-KADZ5PGG.js} +28 -2
- package/dist/chunk-KADZ5PGG.js.map +1 -0
- package/dist/{chunk-YKZME56C.js → chunk-KWADQ227.js} +3 -3
- package/dist/{chunk-YKZME56C.js.map → chunk-KWADQ227.js.map} +1 -1
- package/dist/{chunk-7ZXBPJZP.js → chunk-NEM5N6C6.js} +3902 -1457
- package/dist/chunk-NEM5N6C6.js.map +1 -0
- package/dist/{chunk-GW3EJ2GP.js → chunk-O7KANXC3.js} +86 -13
- package/dist/chunk-O7KANXC3.js.map +1 -0
- package/dist/{chunk-VUQZAB3O.js → chunk-OAKHWFSV.js} +28 -32
- package/dist/chunk-OAKHWFSV.js.map +1 -0
- package/dist/chunk-P7UWKORG.js +561 -0
- package/dist/chunk-P7UWKORG.js.map +1 -0
- package/dist/chunk-QGEIL4TR.js +195 -0
- package/dist/chunk-QGEIL4TR.js.map +1 -0
- package/dist/chunk-RJHCBZF7.js +270 -0
- package/dist/chunk-RJHCBZF7.js.map +1 -0
- package/dist/chunk-S3D75WSQ.js +340 -0
- package/dist/chunk-S3D75WSQ.js.map +1 -0
- package/dist/chunk-UM4ZXXJR.js +9 -0
- package/dist/chunk-UM4ZXXJR.js.map +1 -0
- package/dist/chunk-VPVMLM7Q.js +15 -0
- package/dist/chunk-VPVMLM7Q.js.map +1 -0
- package/dist/{chunk-C2H7HBNP.js → chunk-WVCJRSGJ.js} +119 -14
- package/dist/chunk-WVCJRSGJ.js.map +1 -0
- package/dist/chunk-XA7HVIHI.js +126 -0
- package/dist/chunk-XA7HVIHI.js.map +1 -0
- package/dist/{chunk-TX3EIENU.js → chunk-YDVPI7WT.js} +73 -2
- package/dist/chunk-YDVPI7WT.js.map +1 -0
- package/dist/chunk-YKIHNLWD.js +153 -0
- package/dist/chunk-YKIHNLWD.js.map +1 -0
- package/dist/chunk-ZDUBPV33.js +277 -0
- package/dist/chunk-ZDUBPV33.js.map +1 -0
- package/dist/client.d.ts +1065 -124
- package/dist/codex-realtime-controller.d.ts +13 -0
- package/dist/codex-realtime-controller.js +4 -2
- package/dist/codex-realtime-v3-wire.d.ts +3 -2
- package/dist/codex-realtime.d.ts +5 -1
- package/dist/company-brain.d.ts +1 -11
- package/dist/company-profile.d.ts +13 -0
- package/dist/core.d.ts +7 -2
- package/dist/core.js +25 -6
- package/dist/deprecation.d.ts +47 -0
- package/dist/document-authority-client.d.ts +24 -2
- package/dist/document-authority.js +7 -5
- package/dist/editable-artifacts/index.d.ts +1 -1
- package/dist/editable-artifacts/types.d.ts +12 -2
- package/dist/editable-artifacts.js +19 -3
- package/dist/editable-artifacts.js.map +1 -1
- package/dist/embedding-client.d.ts +129 -0
- package/dist/errors.d.ts +76 -1
- package/dist/feedback.d.ts +23 -0
- package/dist/gateway-realtime-transport.js +1 -1
- package/dist/github-repositories.d.ts +10 -0
- package/dist/github-repositories.js +36 -0
- package/dist/github-repositories.js.map +1 -0
- package/dist/index.d.ts +64 -11
- package/dist/index.js +566 -141
- package/dist/index.js.map +1 -1
- package/dist/insights-usage.d.ts +33 -0
- package/dist/insights-usage.js +7 -0
- package/dist/insights-usage.js.map +1 -0
- package/dist/interaction.d.ts +103 -71
- package/dist/interaction.js +4 -2
- package/dist/knowledge.d.ts +7 -0
- package/dist/knowledge.js +20 -0
- package/dist/knowledge.js.map +1 -0
- package/dist/known-tools.d.ts +4 -0
- package/dist/message-links.d.ts +68 -0
- package/dist/model-context.d.ts +68 -0
- package/dist/model-display.d.ts +2 -0
- package/dist/model-display.js +17 -0
- package/dist/model-display.js.map +1 -0
- package/dist/model-picker-order.d.ts +1 -0
- package/dist/model-picker-order.js +10 -0
- package/dist/model-picker-order.js.map +1 -0
- package/dist/organization-integration-policy.d.ts +26 -0
- package/dist/organization-integration-policy.js +26 -0
- package/dist/organization-integration-policy.js.map +1 -0
- package/dist/pr-review-client.d.ts +1 -1
- package/dist/pr-review.d.ts +1 -1
- package/dist/pr-review.js.map +1 -1
- package/dist/preference-registry.d.ts +1 -1
- package/dist/prepared-mcp-setup.d.ts +5 -0
- package/dist/proxy.d.ts +13 -11
- package/dist/realtime.d.ts +4 -2
- package/dist/realtime.js +253 -7
- package/dist/realtime.js.map +1 -1
- package/dist/session-history-import.d.ts +22 -0
- package/dist/session-history-import.js +36 -0
- package/dist/session-history-import.js.map +1 -0
- package/dist/session-list-entries.d.ts +1 -0
- package/dist/session-list-entries.js +7 -0
- package/dist/session-list-entries.js.map +1 -0
- package/dist/session-message-search.d.ts +53 -0
- package/dist/session-proxy-client.d.ts +28 -0
- package/dist/session-proxy.d.ts +334 -0
- package/dist/session-proxy.js +24 -0
- package/dist/session-proxy.js.map +1 -0
- package/dist/session-titles.d.ts +2 -0
- package/dist/session-usage-limits.d.ts +10 -0
- package/dist/session-usage-limits.js +43 -0
- package/dist/session-usage-limits.js.map +1 -0
- package/dist/shared-catalog-loader.d.ts +34 -0
- package/dist/site-bridge-host.d.ts +112 -0
- package/dist/site-bridge-host.js +285 -0
- package/dist/site-bridge-host.js.map +1 -0
- package/dist/site-browser-entry.d.ts +1 -0
- package/dist/site-browser-runtime.gen.d.ts +1 -0
- package/dist/site-data.d.ts +178 -0
- package/dist/site-document.d.ts +5 -0
- package/dist/site-document.js +20 -0
- package/dist/site-document.js.map +1 -0
- package/dist/site-document.test.d.ts +1 -0
- package/dist/site-http.d.ts +17 -0
- package/dist/site-sql.d.ts +31 -0
- package/dist/site-tool-bridge.d.ts +57 -0
- package/dist/site.d.ts +182 -0
- package/dist/site.js +62 -0
- package/dist/site.js.map +1 -0
- package/dist/skills.d.ts +75 -0
- package/dist/sse.d.ts +1 -1
- package/dist/stream.d.ts +2 -0
- package/dist/tenant-workspaces.d.ts +47 -0
- package/dist/tenant-workspaces.js +8 -0
- package/dist/tenant-workspaces.js.map +1 -0
- package/dist/tool-auth.d.ts +87 -0
- package/dist/tool-auth.js +23 -0
- package/dist/tool-auth.js.map +1 -0
- package/dist/tool-display-metadata.d.ts +3 -0
- package/dist/tools-37DCTCNZ.js +12 -0
- package/dist/tools-37DCTCNZ.js.map +1 -0
- package/dist/tools.d.ts +81 -0
- package/dist/transcription.d.ts +1 -1
- package/dist/types.d.ts +2848 -573
- package/dist/usage-allowances.d.ts +36 -0
- package/dist/usage-allowances.js +80 -0
- package/dist/usage-allowances.js.map +1 -0
- package/dist/vault-types.d.ts +236 -0
- package/dist/vaults-api.d.ts +57 -0
- package/dist/workspace-artifacts.d.ts +47 -5
- package/dist/workspace-instruction-policies.d.ts +2 -2
- package/dist/workspace-integrations.d.ts +74 -0
- package/dist/workspace-integrations.js +65 -0
- package/dist/workspace-integrations.js.map +1 -0
- package/dist/workspace-state.d.ts +8 -71
- package/package.json +99 -5
- package/src/accounts.ts +18 -0
- package/src/adapters/express.ts +136 -0
- package/src/adapters/hono.ts +18 -0
- package/src/adapters/next.ts +42 -0
- package/src/allowance-refusal.ts +5 -0
- package/src/app-links.ts +117 -0
- package/src/artifact-catalog.ts +51 -0
- package/src/artifact-client.ts +40 -49
- package/src/artifacts.ts +1 -0
- package/src/automations-client.ts +8 -2
- package/src/azure-live-transport.ts +286 -0
- package/src/browser.ts +37 -0
- package/src/chat/fold.ts +431 -0
- package/src/chat/handler.ts +240 -0
- package/src/chat/http.ts +257 -0
- package/src/chat/ids.ts +85 -0
- package/src/chat/index.ts +88 -0
- package/src/chat/openai.ts +439 -0
- package/src/chat/opengeni.ts +737 -0
- package/src/chat/types.ts +219 -0
- package/src/chat/vercel.ts +204 -0
- package/src/chats.ts +16 -0
- package/src/client.ts +5822 -1847
- package/src/codex-realtime-controller.ts +163 -8
- package/src/codex-realtime-v3-wire.ts +4 -3
- package/src/codex-realtime-v3.ts +12 -3
- package/src/codex-realtime.ts +11 -3
- package/src/company-brain.ts +0 -40
- package/src/company-profile.ts +13 -0
- package/src/core.ts +20 -0
- package/src/deprecation.ts +149 -0
- package/src/document-authority-client.ts +104 -1
- package/src/editable-artifacts/controller.ts +36 -2
- package/src/editable-artifacts/index.ts +1 -0
- package/src/editable-artifacts/types.ts +29 -16
- package/src/embedding-client.ts +519 -0
- package/src/errors.ts +303 -4
- package/src/feedback.ts +20 -0
- package/src/gateway-realtime-transport.ts +3 -3
- package/src/github-repositories.ts +60 -0
- package/src/index.ts +611 -64
- package/src/insights-usage.ts +61 -0
- package/src/interaction.ts +274 -93
- package/src/knowledge.ts +72 -0
- package/src/known-tools.ts +279 -0
- package/src/message-links.ts +178 -0
- package/src/model-context.ts +113 -0
- package/src/model-display.ts +11 -0
- package/src/model-picker-order.ts +6 -0
- package/src/organization-integration-policy.ts +54 -0
- package/src/pr-review-client.ts +1 -1
- package/src/pr-review.ts +1 -1
- package/src/preference-registry.ts +2 -0
- package/src/prepared-mcp-setup.ts +123 -0
- package/src/proxy.ts +23 -12
- package/src/realtime.ts +16 -6
- package/src/session-history-import.ts +84 -0
- package/src/session-list-entries.ts +1 -0
- package/src/session-message-search.ts +47 -0
- package/src/session-proxy-client.ts +68 -0
- package/src/session-proxy.ts +2098 -0
- package/src/session-titles.ts +10 -0
- package/src/session-usage-limits.ts +71 -0
- package/src/shared-catalog-loader.ts +169 -0
- package/src/site-bridge-host.ts +422 -0
- package/src/site-browser-entry.ts +6 -0
- package/src/site-browser-runtime.gen.ts +2 -0
- package/src/site-data.ts +372 -0
- package/src/site-document.test.ts +64 -0
- package/src/site-document.ts +18 -0
- package/src/site-http.ts +171 -0
- package/src/site-sql.ts +153 -0
- package/src/site-tool-bridge.ts +401 -0
- package/src/site.ts +867 -0
- package/src/skills.ts +69 -0
- package/src/sse.ts +9 -3
- package/src/stream.ts +33 -10
- package/src/tenant-workspaces.ts +148 -0
- package/src/tool-auth.ts +301 -0
- package/src/tool-display-metadata.ts +27 -0
- package/src/tools.ts +452 -0
- package/src/transcription.ts +3 -0
- package/src/types.ts +3216 -581
- package/src/usage-allowances.ts +189 -0
- package/src/vault-types.ts +293 -0
- package/src/vaults-api.ts +288 -0
- package/src/workspace-artifacts.ts +55 -6
- package/src/workspace-instruction-policies.ts +2 -1
- package/src/workspace-integrations.ts +402 -0
- package/src/workspace-state.ts +8 -82
- package/dist/chunk-7ZXBPJZP.js.map +0 -1
- package/dist/chunk-C2H7HBNP.js.map +0 -1
- package/dist/chunk-C4DJPZRU.js +0 -119
- package/dist/chunk-C4DJPZRU.js.map +0 -1
- package/dist/chunk-GW3EJ2GP.js.map +0 -1
- package/dist/chunk-TX3EIENU.js.map +0 -1
- package/dist/chunk-VUQZAB3O.js.map +0 -1
- package/dist/chunk-VYCTL62C.js.map +0 -1
- package/dist/chunk-YTAWBDO2.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
|
+
}
|