@eum-agent/human-interaction 1.0.1 → 1.2.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/dist/index.js +131 -78
- package/package.json +9 -5
- package/ui/ui.js +3912 -18
package/dist/index.js
CHANGED
|
@@ -1,51 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
2
3
|
import { fileURLToPath } from "node:url";
|
|
3
4
|
import { z } from "zod";
|
|
4
|
-
//#region src/tool-names.ts
|
|
5
|
-
/** Canonical names shared by tool registration and browser presentation. */
|
|
6
|
-
const TOOL_NAMES = ["user_ask", "user_set_participating"];
|
|
7
|
-
//#endregion
|
|
8
|
-
//#region src/guidance.ts
|
|
9
|
-
const GUIDANCE = {
|
|
10
|
-
title: "Working with the person",
|
|
11
|
-
entries: [{
|
|
12
|
-
tools: ["user_set_participating"],
|
|
13
|
-
text: "`user_set_participating` with `participating: true` brings the user into a session you started on your own, such as a scheduled task. Routine background work does not bring them in; do it when they should be told something or have to do something, and write the reason they will read. With `participating: false` it returns such a session to background work once the user is no longer needed."
|
|
14
|
-
}, {
|
|
15
|
-
tools: ["user_ask"],
|
|
16
|
-
text: "`user_ask` asks the person a question with the options you see, when only they can decide."
|
|
17
|
-
}]
|
|
18
|
-
};
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/output-schema.ts
|
|
21
|
-
/** Validates complete owner business results and bounded recoverable delivery failures. */
|
|
22
|
-
/** Adds the standard failure envelope and bounded text refusals to an owner's result schema. */
|
|
23
|
-
function toolOutputSchema(body, input, name) {
|
|
24
|
-
return z.union([
|
|
25
|
-
body,
|
|
26
|
-
z.strictObject({
|
|
27
|
-
ok: z.literal(false),
|
|
28
|
-
error: z.strictObject({
|
|
29
|
-
code: z.string().min(1).max(128),
|
|
30
|
-
message: z.string().refine((value) => [...value].length <= 256).meta({ maxLength: 256 }),
|
|
31
|
-
retryable: z.boolean(),
|
|
32
|
-
field: z.string().max(256).optional(),
|
|
33
|
-
maximum: z.number().finite().optional()
|
|
34
|
-
}),
|
|
35
|
-
next: z.strictObject({
|
|
36
|
-
tool: z.literal(name),
|
|
37
|
-
arguments: input
|
|
38
|
-
}).nullable(),
|
|
39
|
-
execution: z.strictObject({ state: z.enum([
|
|
40
|
-
"not-started",
|
|
41
|
-
"committed",
|
|
42
|
-
"unknown"
|
|
43
|
-
]) }).optional()
|
|
44
|
-
}),
|
|
45
|
-
z.string().max(1024)
|
|
46
|
-
]);
|
|
47
|
-
}
|
|
48
|
-
//#endregion
|
|
49
5
|
//#region src/ask-user/ask.ts
|
|
50
6
|
const Option = z.object({
|
|
51
7
|
label: z.string().min(1).max(128).describe("The choice as the person sees it."),
|
|
@@ -54,20 +10,19 @@ const Option = z.object({
|
|
|
54
10
|
const Question = z.object({
|
|
55
11
|
id: z.string().min(1).max(128).describe("Stable id, echoed in the answer."),
|
|
56
12
|
question: z.string().min(1).max(8e3).describe("The complete question, with any context needed to answer it."),
|
|
57
|
-
header: z.string().max(128).optional().describe("A short
|
|
13
|
+
header: z.string().max(128).optional().describe("A short label for question navigation."),
|
|
58
14
|
options: z.array(Option).min(1).max(8).optional().describe("Suggested answers, in display order; put the recommended one first. Omit for a free-text question."),
|
|
59
15
|
allow_free_text: z.boolean().optional().describe("Whether the person may type an answer instead of picking an option. Default true."),
|
|
60
16
|
allow_multiple: z.boolean().optional().describe("Whether the person may pick several options. Default false.")
|
|
61
17
|
});
|
|
62
|
-
const AskInput = z.object({ questions: z.array(Question).min(1).max(8).describe("One to eight questions
|
|
63
|
-
/** Option ids are the labels: a person picks by label, and the model reads
|
|
64
|
-
* the label back — no second vocabulary to keep in step. */
|
|
18
|
+
const AskInput = z.object({ questions: z.array(Question).min(1).max(8).describe("One to eight questions in a single request.") }).refine((value) => new TextEncoder().encode(JSON.stringify(value)).byteLength <= 8388608, "Question input exceeds 8 MiB");
|
|
65
19
|
function toAsk(input) {
|
|
66
20
|
return {
|
|
67
21
|
kind: "question",
|
|
68
22
|
questions: input.questions.map((q) => ({
|
|
69
23
|
id: q.id,
|
|
70
|
-
|
|
24
|
+
...q.header === void 0 ? {} : { header: q.header },
|
|
25
|
+
text: q.question,
|
|
71
26
|
options: (q.options ?? []).map((o) => ({
|
|
72
27
|
id: o.label,
|
|
73
28
|
label: o.label,
|
|
@@ -78,28 +33,58 @@ function toAsk(input) {
|
|
|
78
33
|
}))
|
|
79
34
|
};
|
|
80
35
|
}
|
|
36
|
+
const AnswerDataSchema = z.strictObject({
|
|
37
|
+
ok: z.literal(true),
|
|
38
|
+
committed: z.literal(true),
|
|
39
|
+
requestId: z.string(),
|
|
40
|
+
answeredQuestionIds: z.array(z.string()).max(8)
|
|
41
|
+
});
|
|
81
42
|
function askUserQuestionTool() {
|
|
82
43
|
return {
|
|
83
44
|
name: "user_ask",
|
|
84
45
|
preload: true,
|
|
85
46
|
description: "Ask the person one or more questions and wait for their answer. Use it when you need missing information, a preference, a constraint or a clarification before continuing. The turn pauses until they answer; the answer comes back as a short committed receipt. The framework supplies a bounded answer preview and restoration reference. Keep questions concise and self-contained. Set allow_multiple to let the person pick several options. Read the answer preview or its original source for their selections. The person can always type a free-text answer unless allow_free_text is false; do not add an \"Other\" option.",
|
|
86
47
|
inputSchema: AskInput,
|
|
87
|
-
outputSchema:
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
48
|
+
outputSchema: AnswerDataSchema,
|
|
49
|
+
formatModel: async (data) => ({
|
|
50
|
+
formatVersion: 1,
|
|
51
|
+
rendererVersion: 1,
|
|
52
|
+
nodes: [{
|
|
53
|
+
id: "answer",
|
|
54
|
+
kind: "literal",
|
|
55
|
+
text: `Human request ${data.requestId} was answered and committed. Answered questions: ${data.answeredQuestionIds.join(", ")}. Read the original answer from its bound human source.`
|
|
56
|
+
}]
|
|
57
|
+
}),
|
|
93
58
|
deliveryKind: "receipt",
|
|
94
59
|
historySource: "human-answer",
|
|
95
60
|
async execute(input, ctx) {
|
|
96
61
|
if (ctx.answer === void 0) return { awaitHuman: toAsk(input) };
|
|
97
|
-
return {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
62
|
+
return {
|
|
63
|
+
data: {
|
|
64
|
+
ok: true,
|
|
65
|
+
committed: true,
|
|
66
|
+
requestId: ctx.answer.requestId,
|
|
67
|
+
answeredQuestionIds: ctx.answer.answers.map((answer) => answer.questionId)
|
|
68
|
+
},
|
|
69
|
+
execution: { state: "committed" },
|
|
70
|
+
isError: false,
|
|
71
|
+
presentation: {
|
|
72
|
+
type: "human-answer",
|
|
73
|
+
schemaVersion: 1,
|
|
74
|
+
summary: `Answered ${ctx.answer.answers.length} questions`,
|
|
75
|
+
state: "ready",
|
|
76
|
+
fields: {
|
|
77
|
+
requestId: ctx.answer.requestId,
|
|
78
|
+
answered: ctx.answer.answers.length
|
|
79
|
+
},
|
|
80
|
+
actions: [{
|
|
81
|
+
kind: "openSource",
|
|
82
|
+
available: true,
|
|
83
|
+
authorityRef: ctx.answer.requestId
|
|
84
|
+
}],
|
|
85
|
+
bodyRefs: []
|
|
86
|
+
}
|
|
87
|
+
};
|
|
103
88
|
}
|
|
104
89
|
};
|
|
105
90
|
}
|
|
@@ -109,6 +94,18 @@ function registerAskUserTools(_ctx, register) {
|
|
|
109
94
|
register(askUserQuestionTool());
|
|
110
95
|
}
|
|
111
96
|
//#endregion
|
|
97
|
+
//#region src/guidance.ts
|
|
98
|
+
const GUIDANCE = {
|
|
99
|
+
title: "Working with the person",
|
|
100
|
+
entries: [{
|
|
101
|
+
tools: ["user_set_participating"],
|
|
102
|
+
text: "`user_set_participating` with `participating: true` brings the user into a session you started on your own, such as a scheduled task. Routine background work does not bring them in; do it when they should be told something or have to do something, and write the reason they will read. With `participating: false` it returns such a session to background work once the user is no longer needed."
|
|
103
|
+
}, {
|
|
104
|
+
tools: ["user_ask"],
|
|
105
|
+
text: "`user_ask` asks the person a question with the options you see, when only they can decide."
|
|
106
|
+
}]
|
|
107
|
+
};
|
|
108
|
+
//#endregion
|
|
112
109
|
//#region src/participants/index.ts
|
|
113
110
|
const PARTICIPANTS_SERVICE = "session-participants";
|
|
114
111
|
const Reason = z.string().trim().max(500).describe("Reason shown to the person; maximum 500 UTF-16 code units after trimming.");
|
|
@@ -133,10 +130,53 @@ function dismissText(result) {
|
|
|
133
130
|
case "refused": return "the user created this session and cannot be removed";
|
|
134
131
|
}
|
|
135
132
|
}
|
|
136
|
-
|
|
133
|
+
const ParticipantDataSchema = z.strictObject({
|
|
134
|
+
sessionId: z.string(),
|
|
135
|
+
requestedParticipation: z.boolean(),
|
|
136
|
+
participating: z.boolean().nullable(),
|
|
137
|
+
changed: z.boolean().nullable(),
|
|
138
|
+
outcome: z.enum([
|
|
139
|
+
"changed",
|
|
140
|
+
"unchanged",
|
|
141
|
+
"refused",
|
|
142
|
+
"unknown"
|
|
143
|
+
]),
|
|
144
|
+
message: z.string().max(1024)
|
|
145
|
+
});
|
|
146
|
+
function participantDocument(data) {
|
|
147
|
+
return {
|
|
148
|
+
formatVersion: 1,
|
|
149
|
+
rendererVersion: 1,
|
|
150
|
+
nodes: [{
|
|
151
|
+
id: "membership",
|
|
152
|
+
kind: "literal",
|
|
153
|
+
text: `${data.message} (session ${data.sessionId})`
|
|
154
|
+
}]
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function participantResult(data, isError = false) {
|
|
137
158
|
return {
|
|
138
|
-
|
|
139
|
-
isError:
|
|
159
|
+
data,
|
|
160
|
+
execution: { state: isError ? "unknown" : "committed" },
|
|
161
|
+
isError,
|
|
162
|
+
...isError ? { error: {
|
|
163
|
+
code: "participant_change_failed",
|
|
164
|
+
retryable: false
|
|
165
|
+
} } : {},
|
|
166
|
+
presentation: {
|
|
167
|
+
type: "user-participating",
|
|
168
|
+
schemaVersion: 1,
|
|
169
|
+
summary: data.message,
|
|
170
|
+
state: isError ? "unknown" : "ready",
|
|
171
|
+
fields: {
|
|
172
|
+
sessionId: data.sessionId,
|
|
173
|
+
participating: data.participating,
|
|
174
|
+
changed: data.changed,
|
|
175
|
+
outcome: data.outcome
|
|
176
|
+
},
|
|
177
|
+
actions: [],
|
|
178
|
+
bodyRefs: []
|
|
179
|
+
}
|
|
140
180
|
};
|
|
141
181
|
}
|
|
142
182
|
function participatingTool(participants) {
|
|
@@ -145,15 +185,30 @@ function participatingTool(participants) {
|
|
|
145
185
|
preload: false,
|
|
146
186
|
description: "Bring the user into this session with `participating: true`, so it appears among their conversations and they are told why; `reason` then says what you need from them or what they should know, in one or two sentences they will read. With `participating: false`, let the user go once they are no longer needed; the session returns to background work and `reason` is optional.",
|
|
147
187
|
inputSchema: ParticipatingInput,
|
|
148
|
-
outputSchema:
|
|
188
|
+
outputSchema: ParticipantDataSchema,
|
|
189
|
+
formatModel: async (data) => participantDocument(data),
|
|
149
190
|
deliveryKind: "receipt",
|
|
150
191
|
async execute(input, ctx) {
|
|
151
192
|
try {
|
|
152
|
-
if (input.participating) return { content: inviteText(await participants.invite(ctx, input.reason ?? "")) };
|
|
153
193
|
const reason = input.reason === "" ? void 0 : input.reason;
|
|
154
|
-
|
|
194
|
+
const result = input.participating ? await participants.invite(ctx, input.reason) : await participants.dismiss(ctx, reason);
|
|
195
|
+
return participantResult({
|
|
196
|
+
sessionId: ctx.sessionId,
|
|
197
|
+
requestedParticipation: input.participating,
|
|
198
|
+
participating: result.kind === "refused" ? true : input.participating,
|
|
199
|
+
changed: result.kind === "changed",
|
|
200
|
+
outcome: result.kind,
|
|
201
|
+
message: input.participating ? inviteText(result) : dismissText(result)
|
|
202
|
+
});
|
|
155
203
|
} catch (error) {
|
|
156
|
-
return
|
|
204
|
+
return participantResult({
|
|
205
|
+
sessionId: ctx.sessionId,
|
|
206
|
+
requestedParticipation: input.participating,
|
|
207
|
+
participating: null,
|
|
208
|
+
changed: null,
|
|
209
|
+
outcome: "unknown",
|
|
210
|
+
message: [...error instanceof Error ? error.message : String(error)].slice(0, 256).join("")
|
|
211
|
+
}, true);
|
|
157
212
|
}
|
|
158
213
|
}
|
|
159
214
|
};
|
|
@@ -162,15 +217,10 @@ function registerParticipantTools(ctx, register) {
|
|
|
162
217
|
register(participatingTool(ctx.services.get(PARTICIPANTS_SERVICE)));
|
|
163
218
|
}
|
|
164
219
|
//#endregion
|
|
220
|
+
//#region src/tool-names.ts
|
|
221
|
+
const TOOL_NAMES = ["user_ask", "user_set_participating"];
|
|
222
|
+
//#endregion
|
|
165
223
|
//#region src/index.ts
|
|
166
|
-
/**
|
|
167
|
-
* This plugin's slice of `config.json`.
|
|
168
|
-
*
|
|
169
|
-
* `disabled` turns one tool off while the rest of the package stays on —
|
|
170
|
-
* `enabled: false` on the declaration turns off every one of them. Strict and
|
|
171
|
-
* enumerated: a misspelled tool name is a load failure naming the field, not
|
|
172
|
-
* a line that quietly does nothing.
|
|
173
|
-
*/
|
|
174
224
|
const HitlConfig = z.strictObject({ disabled: z.array(z.enum(TOOL_NAMES)).default([]) }).default({ disabled: [] });
|
|
175
225
|
const hitlPlugin = {
|
|
176
226
|
name: "@eum-agent/human-interaction",
|
|
@@ -178,7 +228,10 @@ const hitlPlugin = {
|
|
|
178
228
|
configSchema: HitlConfig,
|
|
179
229
|
apply(ctx, config) {
|
|
180
230
|
const module = fileURLToPath(new URL("../ui/ui.js", import.meta.url));
|
|
181
|
-
if (existsSync(module)) ctx.client.
|
|
231
|
+
if (existsSync(module)) ctx.client.registerPackageFiles({
|
|
232
|
+
root: dirname(module),
|
|
233
|
+
entry: "ui.js"
|
|
234
|
+
});
|
|
182
235
|
const disabled = new Set(config.disabled);
|
|
183
236
|
const register = (tool) => disabled.has(tool.name) ? void 0 : ctx.tools.register(tool);
|
|
184
237
|
registerAskUserTools(ctx, register);
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"en-US": "Human Interaction",
|
|
5
5
|
"zh-CN": "人工交互"
|
|
6
6
|
},
|
|
7
|
-
"version": "1.0
|
|
7
|
+
"version": "1.2.0",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"description": "Requests for human input and human participation in agent sessions",
|
|
10
10
|
"license": "MIT",
|
|
@@ -23,8 +23,12 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@eum-agent/container": "1.0.0",
|
|
25
25
|
"@eum-agent/kernel": "1.0.0",
|
|
26
|
-
"@eum-agent/plugin": "1.0
|
|
27
|
-
"vite": "^8.2.2"
|
|
26
|
+
"@eum-agent/plugin": "1.2.0",
|
|
27
|
+
"vite": "^8.2.2",
|
|
28
|
+
"react": "^19.2.0",
|
|
29
|
+
"react-dom": "^19.2.0",
|
|
30
|
+
"@types/react": "^19.2.18",
|
|
31
|
+
"@types/react-dom": "^19.2.5"
|
|
28
32
|
},
|
|
29
33
|
"yaoguang": {
|
|
30
34
|
"description": {
|
|
@@ -34,12 +38,12 @@
|
|
|
34
38
|
"dependencies": {},
|
|
35
39
|
"kind": "extension",
|
|
36
40
|
"foundationDependencies": {
|
|
37
|
-
"@eum-agent/infra-local": "1.0
|
|
41
|
+
"@eum-agent/infra-local": "1.2.0"
|
|
38
42
|
}
|
|
39
43
|
},
|
|
40
44
|
"scripts": {
|
|
41
45
|
"build": "tsdown && vite build",
|
|
42
46
|
"test": "vitest run",
|
|
43
|
-
"typecheck": "tsc --noEmit"
|
|
47
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.ui.json"
|
|
44
48
|
}
|
|
45
49
|
}
|