@sophonsai/agent-embed-core 0.0.0-stage → 0.1.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.
- package/README.md +8 -2
- package/dist/index.d.mts +299 -0
- package/dist/index.mjs +127 -0
- package/package.json +32 -3
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sophonsai/agent-embed-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@sophonsai/agent-embed-core` 是 Agent Embed 的底层通信协议包,负责定义通信消息结构、协议版本、公共数据形状和运行时校验。
|
|
4
|
+
|
|
5
|
+
Core 不包含面板挂载、生命周期管理或主题 preset 等产品配置。主题预设由 `@sophonsai/agent-embed-sdk` 定义并对外导出。
|
|
6
|
+
|
|
7
|
+
该包是 SDK 和框架适配包的底层依赖。业务项目通常应安装 SDK 或 React/Vue 适配包,不需要直接依赖 Core。
|
|
8
|
+
|
|
9
|
+
URL 模式的 init config 为 `{ mode: "url", appearance, threadId? }`,不携带 Agent ID 或用户凭证。Agent 与身份由 iframe URL 中的 应用接入凭证 在 WebAPI 端解析。
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
declare const AGENT_EMBED_QUERY_PARAMS: {
|
|
3
|
+
readonly agentId: "agent_id";
|
|
4
|
+
readonly threadId: "thread_id";
|
|
5
|
+
readonly token: "token";
|
|
6
|
+
readonly theme: "theme";
|
|
7
|
+
readonly fontSize: "font_size";
|
|
8
|
+
readonly headerHistoryButton: "header_history_button";
|
|
9
|
+
readonly headerNewConversationButton: "header_new_conversation_button";
|
|
10
|
+
readonly headerThemeSettingButton: "header_theme_setting_button";
|
|
11
|
+
};
|
|
12
|
+
interface AgentEmbedTheme {
|
|
13
|
+
preset: string;
|
|
14
|
+
}
|
|
15
|
+
interface AgentEmbedHeaderAppearance {
|
|
16
|
+
historyButton?: boolean;
|
|
17
|
+
newConversationButton?: boolean;
|
|
18
|
+
themeSettingButton?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface AgentAppearance {
|
|
21
|
+
theme?: AgentEmbedTheme;
|
|
22
|
+
fontSize?: number;
|
|
23
|
+
header?: AgentEmbedHeaderAppearance;
|
|
24
|
+
}
|
|
25
|
+
interface AgentSessionContext {
|
|
26
|
+
threadId: string;
|
|
27
|
+
turnId: string;
|
|
28
|
+
}
|
|
29
|
+
interface AgentTextPayload {
|
|
30
|
+
version: 1;
|
|
31
|
+
range_unit: 'utf16';
|
|
32
|
+
references: Array<{
|
|
33
|
+
id: string;
|
|
34
|
+
type: 'resource';
|
|
35
|
+
provider: string;
|
|
36
|
+
resource_id: string;
|
|
37
|
+
label: string;
|
|
38
|
+
value: string;
|
|
39
|
+
range: {
|
|
40
|
+
start: number;
|
|
41
|
+
end: number;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
interface AgentSessionRunEventContext {
|
|
46
|
+
runId: string;
|
|
47
|
+
threadId: string | null;
|
|
48
|
+
objectType: 'model' | 'agent' | 'knowledge_base' | 'database' | 'file';
|
|
49
|
+
objectId: string;
|
|
50
|
+
step: number;
|
|
51
|
+
parentToolCallId: string | null;
|
|
52
|
+
}
|
|
53
|
+
type AgentSessionEventContext = AgentSessionRunEventContext & AgentSessionContext;
|
|
54
|
+
interface AgentToolCallStartedEvent {
|
|
55
|
+
toolCallId: string;
|
|
56
|
+
toolName: string;
|
|
57
|
+
toolNameEn?: string;
|
|
58
|
+
actionDescription?: string;
|
|
59
|
+
arguments: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
interface AgentToolCallSucceededEvent extends AgentToolCallStartedEvent {
|
|
62
|
+
result: string;
|
|
63
|
+
}
|
|
64
|
+
interface AgentToolCallProgressUpdatedEvent extends AgentToolCallStartedEvent {
|
|
65
|
+
sequence: number;
|
|
66
|
+
progress: {
|
|
67
|
+
type: string;
|
|
68
|
+
phase: string;
|
|
69
|
+
data: Record<string, unknown>;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
interface AgentToolCallFailedEvent extends AgentToolCallStartedEvent {
|
|
73
|
+
error: string;
|
|
74
|
+
}
|
|
75
|
+
interface AgentToolCallBackgroundedEvent {
|
|
76
|
+
toolCallId: string;
|
|
77
|
+
toolName: string;
|
|
78
|
+
toolNameEn?: string;
|
|
79
|
+
content?: string;
|
|
80
|
+
}
|
|
81
|
+
interface AgentToolCallCancelledEvent {
|
|
82
|
+
toolCallId: string;
|
|
83
|
+
toolName: string;
|
|
84
|
+
toolNameEn?: string;
|
|
85
|
+
reason?: 'user_terminated' | 'session_disconnected' | 'background_cancelled' | 'unknown';
|
|
86
|
+
}
|
|
87
|
+
type AgentToolCall = AgentToolCallStartedEvent & ({
|
|
88
|
+
kind: 'tool';
|
|
89
|
+
executor: 'server';
|
|
90
|
+
} | {
|
|
91
|
+
kind: 'action';
|
|
92
|
+
executor: 'client';
|
|
93
|
+
});
|
|
94
|
+
interface AgentToolCallCreatedEvent {
|
|
95
|
+
toolCall: AgentToolCall;
|
|
96
|
+
dependsOn?: {
|
|
97
|
+
toolCallId: string;
|
|
98
|
+
reason: string;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
interface AgentToolApprovalRequestedEvent {
|
|
102
|
+
toolCallIds: string[];
|
|
103
|
+
}
|
|
104
|
+
interface AgentClientActionRequestEvent {
|
|
105
|
+
actionId: string;
|
|
106
|
+
actionName: string;
|
|
107
|
+
actionNameEn?: string;
|
|
108
|
+
arguments: Record<string, unknown>;
|
|
109
|
+
actionDescription?: string;
|
|
110
|
+
}
|
|
111
|
+
interface AgentUserFeedbackConsumedEvent {
|
|
112
|
+
content: string;
|
|
113
|
+
payload?: AgentTextPayload | null;
|
|
114
|
+
}
|
|
115
|
+
interface AgentUserSteerConsumedEvent {
|
|
116
|
+
steerId: string;
|
|
117
|
+
content: string;
|
|
118
|
+
payload?: AgentTextPayload;
|
|
119
|
+
}
|
|
120
|
+
interface AgentUserSteerCancelledEvent {
|
|
121
|
+
steerId: string;
|
|
122
|
+
}
|
|
123
|
+
interface AgentUserInputSubmittedEvent {
|
|
124
|
+
content: string;
|
|
125
|
+
payload?: AgentTextPayload | null;
|
|
126
|
+
}
|
|
127
|
+
type AgentSessionTurnStartedEvent = Record<string, never>;
|
|
128
|
+
type AgentSessionTurnSucceededEvent = Record<string, never>;
|
|
129
|
+
type AgentSessionRunStartedEvent = Record<string, never>;
|
|
130
|
+
type AgentSessionRunIdledEvent = Record<string, never>;
|
|
131
|
+
type AgentSessionRunEndedEvent = Record<string, never>;
|
|
132
|
+
interface AgentSessionTurnFailedEvent {
|
|
133
|
+
code: number;
|
|
134
|
+
error: string;
|
|
135
|
+
}
|
|
136
|
+
interface AgentSessionTurnCancelledEvent {
|
|
137
|
+
reason: 'user_graceful_termination' | 'user_forced_termination' | 'hitl_user_termination' | 'hitl_timeout' | 'session_orphaned' | 'runtime_lost' | 'unknown';
|
|
138
|
+
}
|
|
139
|
+
interface AgentSystemMessageConsumedEvent {
|
|
140
|
+
messageId: string;
|
|
141
|
+
messageType: string;
|
|
142
|
+
content: string;
|
|
143
|
+
modelContent: string;
|
|
144
|
+
data?: unknown;
|
|
145
|
+
}
|
|
146
|
+
interface AgentSessionSuccess {
|
|
147
|
+
status: 'success';
|
|
148
|
+
context: AgentSessionContext | null;
|
|
149
|
+
}
|
|
150
|
+
interface AgentSessionFailure {
|
|
151
|
+
status: 'failure';
|
|
152
|
+
context: AgentSessionContext | null;
|
|
153
|
+
error: unknown;
|
|
154
|
+
}
|
|
155
|
+
interface AgentSessionCancelled {
|
|
156
|
+
status: 'cancelled';
|
|
157
|
+
context: AgentSessionContext | null;
|
|
158
|
+
}
|
|
159
|
+
type AgentSessionSettlement = AgentSessionSuccess | AgentSessionFailure | AgentSessionCancelled;
|
|
160
|
+
type AgentServerEvent = Record<string, unknown>;
|
|
161
|
+
interface AgentEvents {
|
|
162
|
+
onConcurrentSend?: () => void;
|
|
163
|
+
onEvent?: (event: AgentServerEvent) => void;
|
|
164
|
+
onSessionContextReady?: (context: AgentSessionContext) => void;
|
|
165
|
+
onToolCallCreated?: (event: AgentToolCallCreatedEvent, context: AgentSessionEventContext) => void;
|
|
166
|
+
onToolApprovalRequested?: (event: AgentToolApprovalRequestedEvent, context: AgentSessionEventContext) => void;
|
|
167
|
+
onToolCallStarted?: (event: AgentToolCallStartedEvent, context: AgentSessionEventContext) => void;
|
|
168
|
+
onToolCallProgressUpdated?: (event: AgentToolCallProgressUpdatedEvent, context: AgentSessionEventContext) => void;
|
|
169
|
+
onToolCallBackgrounded?: (event: AgentToolCallBackgroundedEvent, context: AgentSessionEventContext) => void;
|
|
170
|
+
onToolCallSucceeded?: (event: AgentToolCallSucceededEvent, context: AgentSessionEventContext) => void;
|
|
171
|
+
onToolCallFailed?: (event: AgentToolCallFailedEvent, context: AgentSessionEventContext) => void;
|
|
172
|
+
onToolCallCancelled?: (event: AgentToolCallCancelledEvent, context: AgentSessionEventContext) => void;
|
|
173
|
+
onClientActionRequest?: (event: AgentClientActionRequestEvent, context: AgentSessionEventContext) => void;
|
|
174
|
+
onUserFeedbackConsumed?: (event: AgentUserFeedbackConsumedEvent, context: AgentSessionEventContext) => void;
|
|
175
|
+
onUserSteerConsumed?: (event: AgentUserSteerConsumedEvent, context: AgentSessionEventContext) => void;
|
|
176
|
+
onUserSteerCancelled?: (event: AgentUserSteerCancelledEvent, context: AgentSessionRunEventContext) => void;
|
|
177
|
+
onUserInputSubmitted?: (event: AgentUserInputSubmittedEvent, context: AgentSessionEventContext) => void;
|
|
178
|
+
onSessionTurnStarted?: (event: AgentSessionTurnStartedEvent, context: AgentSessionEventContext) => void;
|
|
179
|
+
onSessionTurnSucceeded?: (event: AgentSessionTurnSucceededEvent, context: AgentSessionEventContext) => void;
|
|
180
|
+
onSessionTurnFailed?: (event: AgentSessionTurnFailedEvent, context: AgentSessionEventContext) => void;
|
|
181
|
+
onSessionTurnCancelled?: (event: AgentSessionTurnCancelledEvent, context: AgentSessionEventContext) => void;
|
|
182
|
+
onSessionRunStarted?: (event: AgentSessionRunStartedEvent, context: AgentSessionRunEventContext) => void;
|
|
183
|
+
onSessionRunIdled?: (event: AgentSessionRunIdledEvent, context: AgentSessionRunEventContext) => void;
|
|
184
|
+
onSessionRunEnded?: (event: AgentSessionRunEndedEvent, context: AgentSessionRunEventContext) => void;
|
|
185
|
+
onSystemMessageConsumed?: (event: AgentSystemMessageConsumedEvent, context: AgentSessionEventContext) => void;
|
|
186
|
+
onSuccess?: (result: AgentSessionSuccess) => void;
|
|
187
|
+
onError?: (result: AgentSessionFailure) => void;
|
|
188
|
+
onCancelled?: (result: AgentSessionCancelled) => void;
|
|
189
|
+
onSettled?: (result: AgentSessionSettlement) => void;
|
|
190
|
+
}
|
|
191
|
+
declare const AGENT_EVENT_NAMES: readonly ["onConcurrentSend", "onEvent", "onSessionContextReady", "onToolCallCreated", "onToolApprovalRequested", "onToolCallStarted", "onToolCallProgressUpdated", "onToolCallBackgrounded", "onToolCallSucceeded", "onToolCallFailed", "onToolCallCancelled", "onClientActionRequest", "onUserFeedbackConsumed", "onUserSteerConsumed", "onUserSteerCancelled", "onUserInputSubmitted", "onSessionTurnStarted", "onSessionTurnSucceeded", "onSessionTurnFailed", "onSessionTurnCancelled", "onSessionRunStarted", "onSessionRunIdled", "onSessionRunEnded", "onSystemMessageConsumed", "onSuccess", "onError", "onCancelled", "onSettled"];
|
|
192
|
+
interface AgentEmbedPlatformUserAuth {
|
|
193
|
+
type: 'platform.user.credential';
|
|
194
|
+
credential: string;
|
|
195
|
+
}
|
|
196
|
+
interface AgentEmbedExtensionUserAuth {
|
|
197
|
+
type: 'extension.user.identity';
|
|
198
|
+
appId: string;
|
|
199
|
+
externalUser: {
|
|
200
|
+
id: string;
|
|
201
|
+
name?: string;
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
type AgentEmbedAuth = AgentEmbedPlatformUserAuth | AgentEmbedExtensionUserAuth;
|
|
205
|
+
interface AgentEmbedSendMessageInput {
|
|
206
|
+
text: string;
|
|
207
|
+
}
|
|
208
|
+
interface AgentEmbedInputQuery extends AgentEmbedSendMessageInput {
|
|
209
|
+
context?: string;
|
|
210
|
+
}
|
|
211
|
+
interface AgentEmbedInstance {
|
|
212
|
+
sendMessage: (input: AgentEmbedSendMessageInput) => void;
|
|
213
|
+
addInputQuery: (input: AgentEmbedInputQuery) => void;
|
|
214
|
+
abortResponse: () => void;
|
|
215
|
+
openHistoryPanel: () => void;
|
|
216
|
+
openThemeSettingsPanel: () => void;
|
|
217
|
+
newConversation: () => void;
|
|
218
|
+
focusInput: () => void;
|
|
219
|
+
scrollToBottom: () => void;
|
|
220
|
+
}
|
|
221
|
+
interface AgentEmbedInitError {
|
|
222
|
+
code: 'invalid_token' | 'forbidden' | 'agent_not_found' | 'request_failed' | 'frame_load_failed';
|
|
223
|
+
message: string;
|
|
224
|
+
}
|
|
225
|
+
interface AgentEmbedInitConfig {
|
|
226
|
+
agentId: string;
|
|
227
|
+
auth: AgentEmbedAuth;
|
|
228
|
+
/** @deprecated Bridge compatibility for token-mode clients and frames. */
|
|
229
|
+
token?: string;
|
|
230
|
+
threadId?: string;
|
|
231
|
+
appearance: AgentAppearance;
|
|
232
|
+
}
|
|
233
|
+
interface AgentEmbedLegacyInitConfig {
|
|
234
|
+
agentId: string;
|
|
235
|
+
token: string;
|
|
236
|
+
auth?: never;
|
|
237
|
+
threadId?: string;
|
|
238
|
+
appearance: AgentAppearance;
|
|
239
|
+
}
|
|
240
|
+
/** URL mode config carries appearance and session context, never platform credentials. */
|
|
241
|
+
type AgentRuntimeValue = string | number | boolean | null | AgentRuntimeValue[] | AgentRuntimeVariables;
|
|
242
|
+
interface AgentRuntimeVariables {
|
|
243
|
+
[key: string]: AgentRuntimeValue;
|
|
244
|
+
}
|
|
245
|
+
interface AgentEmbedUrlInitConfig {
|
|
246
|
+
mode: 'url';
|
|
247
|
+
runtime_variables?: AgentRuntimeVariables;
|
|
248
|
+
threadId?: string;
|
|
249
|
+
appearance: AgentAppearance;
|
|
250
|
+
}
|
|
251
|
+
type AgentEmbedBridgeConfig = AgentEmbedInitConfig | AgentEmbedUrlInitConfig;
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/protocol.d.ts
|
|
254
|
+
declare const AGENT_EMBED_BRIDGE = "sophonsai/agent-component-bridge";
|
|
255
|
+
declare const AGENT_EMBED_BRIDGE_VERSION = 1;
|
|
256
|
+
type AgentEmbedAction = 'sendMessage' | 'addInputQuery' | 'abortResponse' | 'openHistoryPanel' | 'openThemeSettingsPanel' | 'newConversation' | 'focusInput' | 'scrollToBottom';
|
|
257
|
+
type AgentEmbedCallback = keyof AgentEvents | 'onThemeChange' | 'onFontSizeChange' | 'onInitError';
|
|
258
|
+
interface AgentEmbedInitMessage {
|
|
259
|
+
bridge: typeof AGENT_EMBED_BRIDGE;
|
|
260
|
+
version: typeof AGENT_EMBED_BRIDGE_VERSION;
|
|
261
|
+
type: 'init';
|
|
262
|
+
instanceId: string;
|
|
263
|
+
config: AgentEmbedInitConfig | AgentEmbedLegacyInitConfig | AgentEmbedUrlInitConfig;
|
|
264
|
+
}
|
|
265
|
+
interface AgentEmbedCommandMessage {
|
|
266
|
+
bridge: typeof AGENT_EMBED_BRIDGE;
|
|
267
|
+
version: typeof AGENT_EMBED_BRIDGE_VERSION;
|
|
268
|
+
type: 'command';
|
|
269
|
+
instanceId: string;
|
|
270
|
+
action: AgentEmbedAction;
|
|
271
|
+
args: unknown[];
|
|
272
|
+
}
|
|
273
|
+
interface AgentEmbedAppearanceUpdate {
|
|
274
|
+
theme?: AgentEmbedTheme | null;
|
|
275
|
+
fontSize?: number | null;
|
|
276
|
+
header?: AgentEmbedHeaderAppearance | null;
|
|
277
|
+
}
|
|
278
|
+
interface AgentEmbedUpdateAppearanceMessage {
|
|
279
|
+
bridge: typeof AGENT_EMBED_BRIDGE;
|
|
280
|
+
version: typeof AGENT_EMBED_BRIDGE_VERSION;
|
|
281
|
+
type: 'update-appearance';
|
|
282
|
+
instanceId: string;
|
|
283
|
+
appearance: AgentEmbedAppearanceUpdate;
|
|
284
|
+
}
|
|
285
|
+
type AgentEmbedHostMessage = AgentEmbedInitMessage | AgentEmbedCommandMessage | AgentEmbedUpdateAppearanceMessage;
|
|
286
|
+
interface AgentEmbedFrameMessage {
|
|
287
|
+
bridge: typeof AGENT_EMBED_BRIDGE;
|
|
288
|
+
version: typeof AGENT_EMBED_BRIDGE_VERSION;
|
|
289
|
+
type: 'hello' | 'ready' | 'callback';
|
|
290
|
+
instanceId?: string;
|
|
291
|
+
callback?: AgentEmbedCallback;
|
|
292
|
+
args?: unknown[];
|
|
293
|
+
}
|
|
294
|
+
/** Runtime variables cross the iframe boundary as a JSON object. */
|
|
295
|
+
declare const isAgentRuntimeVariables: (value: unknown) => value is AgentRuntimeVariables;
|
|
296
|
+
declare const isAgentEmbedHostMessage: (value: unknown) => value is AgentEmbedHostMessage;
|
|
297
|
+
declare const isAgentEmbedFrameMessage: (value: unknown) => value is AgentEmbedFrameMessage;
|
|
298
|
+
//#endregion
|
|
299
|
+
export { AGENT_EMBED_BRIDGE, AGENT_EMBED_BRIDGE_VERSION, AGENT_EMBED_QUERY_PARAMS, AGENT_EVENT_NAMES, AgentAppearance, AgentClientActionRequestEvent, AgentEmbedAction, AgentEmbedAppearanceUpdate, AgentEmbedAuth, AgentEmbedBridgeConfig, AgentEmbedCallback, AgentEmbedCommandMessage, AgentEmbedExtensionUserAuth, AgentEmbedFrameMessage, AgentEmbedHeaderAppearance, AgentEmbedHostMessage, AgentEmbedInitConfig, AgentEmbedInitError, AgentEmbedInitMessage, AgentEmbedInputQuery, AgentEmbedInstance, AgentEmbedLegacyInitConfig, AgentEmbedPlatformUserAuth, AgentEmbedSendMessageInput, AgentEmbedTheme, AgentEmbedUpdateAppearanceMessage, AgentEmbedUrlInitConfig, AgentEvents, AgentRuntimeValue, AgentRuntimeVariables, AgentServerEvent, AgentSessionCancelled, AgentSessionContext, AgentSessionEventContext, AgentSessionFailure, AgentSessionRunEndedEvent, AgentSessionRunEventContext, AgentSessionRunIdledEvent, AgentSessionRunStartedEvent, AgentSessionSettlement, AgentSessionSuccess, AgentSessionTurnCancelledEvent, AgentSessionTurnFailedEvent, AgentSessionTurnStartedEvent, AgentSessionTurnSucceededEvent, AgentSystemMessageConsumedEvent, AgentTextPayload, AgentToolApprovalRequestedEvent, AgentToolCall, AgentToolCallBackgroundedEvent, AgentToolCallCancelledEvent, AgentToolCallCreatedEvent, AgentToolCallFailedEvent, AgentToolCallProgressUpdatedEvent, AgentToolCallStartedEvent, AgentToolCallSucceededEvent, AgentUserFeedbackConsumedEvent, AgentUserInputSubmittedEvent, AgentUserSteerCancelledEvent, AgentUserSteerConsumedEvent, isAgentEmbedFrameMessage, isAgentEmbedHostMessage, isAgentRuntimeVariables };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//#region src/protocol.ts
|
|
2
|
+
const AGENT_EMBED_BRIDGE = "sophonsai/agent-component-bridge";
|
|
3
|
+
const AGENT_EMBED_BRIDGE_VERSION = 1;
|
|
4
|
+
/** Runtime variables cross the iframe boundary as a JSON object. */
|
|
5
|
+
const isAgentRuntimeVariables = (value) => {
|
|
6
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
7
|
+
const isValue = (item) => {
|
|
8
|
+
if (item === null || typeof item === "string" || typeof item === "boolean") return true;
|
|
9
|
+
if (typeof item === "number") return Number.isFinite(item);
|
|
10
|
+
if (typeof item !== "object" || ancestors.has(item)) return false;
|
|
11
|
+
if (!Array.isArray(item) && Object.prototype.toString.call(item) !== "[object Object]") return false;
|
|
12
|
+
ancestors.add(item);
|
|
13
|
+
const valid = Object.values(item).every(isValue);
|
|
14
|
+
ancestors.delete(item);
|
|
15
|
+
return valid;
|
|
16
|
+
};
|
|
17
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value) && isValue(value));
|
|
18
|
+
};
|
|
19
|
+
const isAgentEmbedHostMessage = (value) => {
|
|
20
|
+
if (!value || typeof value !== "object") return false;
|
|
21
|
+
const message = value;
|
|
22
|
+
if (message.bridge !== "sophonsai/agent-component-bridge" || message.version !== 1 || typeof message.instanceId !== "string") return false;
|
|
23
|
+
if (message.type === "init") {
|
|
24
|
+
const config = message.config;
|
|
25
|
+
if (config?.mode === "url") return Boolean(config.appearance && typeof config.appearance === "object" && (config.threadId === void 0 || typeof config.threadId === "string") && (config.runtime_variables === void 0 || isAgentRuntimeVariables(config.runtime_variables)));
|
|
26
|
+
if (!config || typeof config.agentId !== "string") return false;
|
|
27
|
+
if (!config.auth) return typeof config.token === "string";
|
|
28
|
+
if (typeof config.auth !== "object") return false;
|
|
29
|
+
const auth = config.auth;
|
|
30
|
+
if (auth.type === "platform.user.credential") return typeof auth.credential === "string";
|
|
31
|
+
if (auth.type !== "extension.user.identity") return false;
|
|
32
|
+
const externalUser = auth.externalUser;
|
|
33
|
+
return Boolean(typeof auth.appId === "string" && externalUser && typeof externalUser === "object" && typeof externalUser.id === "string" && (externalUser.name === void 0 || typeof externalUser.name === "string"));
|
|
34
|
+
}
|
|
35
|
+
if (message.type === "update-appearance") {
|
|
36
|
+
const appearance = message.appearance;
|
|
37
|
+
if (!appearance || typeof appearance !== "object") return false;
|
|
38
|
+
const hasTheme = Object.prototype.hasOwnProperty.call(appearance, "theme");
|
|
39
|
+
const hasFontSize = Object.prototype.hasOwnProperty.call(appearance, "fontSize");
|
|
40
|
+
const hasHeader = Object.prototype.hasOwnProperty.call(appearance, "header");
|
|
41
|
+
if (!hasTheme && !hasFontSize && !hasHeader) return false;
|
|
42
|
+
if (Object.keys(appearance).some((key) => key !== "theme" && key !== "fontSize" && key !== "header")) return false;
|
|
43
|
+
if (hasTheme && appearance.theme !== null && (!appearance.theme || typeof appearance.theme.preset !== "string")) return false;
|
|
44
|
+
if (hasFontSize && appearance.fontSize !== null && (typeof appearance.fontSize !== "number" || !Number.isFinite(appearance.fontSize))) return false;
|
|
45
|
+
if (hasHeader && appearance.header !== null) {
|
|
46
|
+
if (!appearance.header || typeof appearance.header !== "object") return false;
|
|
47
|
+
const headerKeys = [
|
|
48
|
+
"historyButton",
|
|
49
|
+
"newConversationButton",
|
|
50
|
+
"themeSettingButton"
|
|
51
|
+
];
|
|
52
|
+
if (Object.keys(appearance.header).some((key) => !headerKeys.includes(key))) return false;
|
|
53
|
+
if (Object.values(appearance.header).some((value) => value !== void 0 && typeof value !== "boolean")) return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return message.type === "command" && [
|
|
58
|
+
"sendMessage",
|
|
59
|
+
"addInputQuery",
|
|
60
|
+
"abortResponse",
|
|
61
|
+
"openHistoryPanel",
|
|
62
|
+
"openThemeSettingsPanel",
|
|
63
|
+
"newConversation",
|
|
64
|
+
"focusInput",
|
|
65
|
+
"scrollToBottom"
|
|
66
|
+
].includes(message.action ?? "") && Array.isArray(message.args);
|
|
67
|
+
};
|
|
68
|
+
const isInitError = (value) => {
|
|
69
|
+
if (!value || typeof value !== "object") return false;
|
|
70
|
+
const error = value;
|
|
71
|
+
return typeof error.message === "string" && typeof error.code === "string" && [
|
|
72
|
+
"invalid_token",
|
|
73
|
+
"forbidden",
|
|
74
|
+
"agent_not_found",
|
|
75
|
+
"request_failed",
|
|
76
|
+
"frame_load_failed"
|
|
77
|
+
].includes(error.code);
|
|
78
|
+
};
|
|
79
|
+
const isAgentEmbedFrameMessage = (value) => {
|
|
80
|
+
if (!value || typeof value !== "object") return false;
|
|
81
|
+
const message = value;
|
|
82
|
+
return message.bridge === "sophonsai/agent-component-bridge" && message.version === 1 && (message.type === "hello" || message.type === "ready" || message.type === "callback") && (message.type !== "callback" || message.callback !== "onInitError" || isInitError(message.args?.[0]));
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/types.ts
|
|
86
|
+
const AGENT_EMBED_QUERY_PARAMS = {
|
|
87
|
+
agentId: "agent_id",
|
|
88
|
+
threadId: "thread_id",
|
|
89
|
+
token: "token",
|
|
90
|
+
theme: "theme",
|
|
91
|
+
fontSize: "font_size",
|
|
92
|
+
headerHistoryButton: "header_history_button",
|
|
93
|
+
headerNewConversationButton: "header_new_conversation_button",
|
|
94
|
+
headerThemeSettingButton: "header_theme_setting_button"
|
|
95
|
+
};
|
|
96
|
+
const AGENT_EVENT_NAMES = [
|
|
97
|
+
"onConcurrentSend",
|
|
98
|
+
"onEvent",
|
|
99
|
+
"onSessionContextReady",
|
|
100
|
+
"onToolCallCreated",
|
|
101
|
+
"onToolApprovalRequested",
|
|
102
|
+
"onToolCallStarted",
|
|
103
|
+
"onToolCallProgressUpdated",
|
|
104
|
+
"onToolCallBackgrounded",
|
|
105
|
+
"onToolCallSucceeded",
|
|
106
|
+
"onToolCallFailed",
|
|
107
|
+
"onToolCallCancelled",
|
|
108
|
+
"onClientActionRequest",
|
|
109
|
+
"onUserFeedbackConsumed",
|
|
110
|
+
"onUserSteerConsumed",
|
|
111
|
+
"onUserSteerCancelled",
|
|
112
|
+
"onUserInputSubmitted",
|
|
113
|
+
"onSessionTurnStarted",
|
|
114
|
+
"onSessionTurnSucceeded",
|
|
115
|
+
"onSessionTurnFailed",
|
|
116
|
+
"onSessionTurnCancelled",
|
|
117
|
+
"onSessionRunStarted",
|
|
118
|
+
"onSessionRunIdled",
|
|
119
|
+
"onSessionRunEnded",
|
|
120
|
+
"onSystemMessageConsumed",
|
|
121
|
+
"onSuccess",
|
|
122
|
+
"onError",
|
|
123
|
+
"onCancelled",
|
|
124
|
+
"onSettled"
|
|
125
|
+
];
|
|
126
|
+
//#endregion
|
|
127
|
+
export { AGENT_EMBED_BRIDGE, AGENT_EMBED_BRIDGE_VERSION, AGENT_EMBED_QUERY_PARAMS, AGENT_EVENT_NAMES, isAgentEmbedFrameMessage, isAgentEmbedHostMessage, isAgentRuntimeVariables };
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sophonsai/agent-embed-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-independent types and protocol primitives for Sophon Agent Embed.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"sophonsai",
|
|
8
|
+
"agent",
|
|
9
|
+
"agent-embed",
|
|
10
|
+
"protocol"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"registry": "https://registry.npmjs.org/",
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"import": "./dist/index.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsdown": "^0.22.14"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsdown src/index.ts --format esm --dts",
|
|
32
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
33
|
+
"test": "vitest run --config vitest.config.ts"
|
|
34
|
+
}
|
|
6
35
|
}
|