@mushinjs/plugin-agent 0.0.0-stage → 0.2.0-next.20261112165327

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Setobox
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,274 @@
1
+ import { SYSTEM_PROMPT, SceneInfo, TurnContext, contextNote, skillBlock } from "./prompt.js";
2
+ import { AgentSettings, CustomRecommendation, SCENE_KEYS, SceneProfile, TokenUsage, ToolPolicy, defaultPolicy, formatSkillMarkdown, parseSkillMarkdown, policyInput, profileInput, today } from "./settings.js";
3
+ import { AgentSkill, BUILTIN_SKILLS, SkillDefinition, SkillSource, skillFields, skillInput } from "./skills.js";
4
+ import { Context, Service, z } from "@mushinjs/core";
5
+ import { LlmMessage, ToolDefinition } from "@mushinjs/plugin-providers";
6
+ declare module '@mushinjs/core' {
7
+ interface Context {
8
+ agent: Agent;
9
+ }
10
+ interface Events {
11
+ 'agent/event'(event: AgentEvent): void;
12
+ /** Skills, page profiles or tool policies changed. */
13
+ 'agent/changed'(): void;
14
+ }
15
+ }
16
+ export type AgentEvent = {
17
+ session: string;
18
+ } & ({
19
+ type: 'start';
20
+ } | {
21
+ type: 'delta';
22
+ text: string;
23
+ } | {
24
+ type: 'tool-call';
25
+ id: string;
26
+ action: string;
27
+ input: unknown;
28
+ risk: string;
29
+ } | {
30
+ type: 'confirm';
31
+ id: string;
32
+ action: string;
33
+ input: unknown;
34
+ } | {
35
+ type: 'tool-result';
36
+ id: string;
37
+ ok: boolean;
38
+ result?: unknown;
39
+ error?: string;
40
+ } | {
41
+ type: 'done';
42
+ text: string;
43
+ usage?: {
44
+ inputTokens: number;
45
+ outputTokens: number;
46
+ };
47
+ } |
48
+ /** `code` is `max_steps` when the turn ran out of tool rounds, so the dashboard can offer to go on. */
49
+ {
50
+ type: 'error';
51
+ error: string;
52
+ code?: 'max_steps';
53
+ });
54
+ /**
55
+ * A stored message; `display` holds the user's own words when the content carries a context note,
56
+ * and `skills` the names of the skills picked for it.
57
+ */
58
+ export type AgentMessage = LlmMessage & {
59
+ display?: string;
60
+ skills?: string[];
61
+ /** A chat quoted into the message, as the dashboard shows it: who said what, and where. */
62
+ quote?: QuotedChat;
63
+ at: number;
64
+ };
65
+ /** Messages of a bot's chat quoted into an Agent message, around one picked message. */
66
+ export interface QuotedChat {
67
+ bot: string;
68
+ botName: string;
69
+ channel: {
70
+ type: 'group' | 'private';
71
+ id: string;
72
+ name?: string;
73
+ };
74
+ /** The picked message's record id; a sample for flows.test and flows.preview. */
75
+ focus: string;
76
+ lines: {
77
+ id: string;
78
+ direction: 'in' | 'out';
79
+ user: string;
80
+ userId: string;
81
+ text: string;
82
+ at: number;
83
+ }[];
84
+ }
85
+ /** Where a conversation was started, for the history list. */
86
+ export interface SessionScene {
87
+ key: string;
88
+ label?: string;
89
+ page?: string;
90
+ }
91
+ export interface AgentSession {
92
+ id: string;
93
+ title: string;
94
+ createdAt: number;
95
+ updatedAt: number;
96
+ /** Tool list frozen at creation, so every request in the session sends identical tools. */
97
+ tools: ToolDefinition[];
98
+ messages: AgentMessage[];
99
+ /** The page the conversation was started on. */
100
+ scene?: SessionScene;
101
+ /** What it touched, for search: `scene:<key>`, `skill:<id>`, `command:<name>`, `resource:<kind>:<id>`. */
102
+ tags?: string[];
103
+ }
104
+ export interface SendOptions {
105
+ /** Dashboard connection id; client actions such as ui.navigate are routed back to it. */
106
+ client?: string;
107
+ page?: string;
108
+ /** The page as the dashboard describes it; its hint and state go into the context note. */
109
+ scene?: SceneInfo;
110
+ /** Ids of skills whose instructions go into this message. */
111
+ skills?: string[];
112
+ /** A message of a bot's chat to quote, with a few messages around it. */
113
+ quote?: {
114
+ bot: string;
115
+ channel: string;
116
+ id: string;
117
+ before?: number;
118
+ after?: number;
119
+ };
120
+ }
121
+ /** Commands the dashboard runs itself, without asking the model. */
122
+ export declare const BUILTIN_COMMANDS: {
123
+ name: string;
124
+ description: string;
125
+ 'zh-CN': string;
126
+ }[];
127
+ export declare const Config: z.ZodObject<{
128
+ provider: z.ZodOptional<z.ZodString>;
129
+ model: z.ZodOptional<z.ZodString>;
130
+ maxSteps: z.ZodDefault<z.ZodNumber>;
131
+ confirmTimeout: z.ZodDefault<z.ZodNumber>;
132
+ dailyTokens: z.ZodDefault<z.ZodNumber>;
133
+ }, z.core.$strip>;
134
+ export type Config = z.infer<typeof Config>;
135
+ /** Runs conversations in which the LLM operates Mushin.js through the action registry. */
136
+ export declare class Agent extends Service {
137
+ config: Config;
138
+ static inject: string[];
139
+ static Config: z.ZodObject<{
140
+ provider: z.ZodOptional<z.ZodString>;
141
+ model: z.ZodOptional<z.ZodString>;
142
+ maxSteps: z.ZodDefault<z.ZodNumber>;
143
+ confirmTimeout: z.ZodDefault<z.ZodNumber>;
144
+ dailyTokens: z.ZodDefault<z.ZodNumber>;
145
+ }, z.core.$strip>;
146
+ private home;
147
+ private running;
148
+ private pluginSkills;
149
+ private settingsCache?;
150
+ constructor(ctx: Context, config: Config);
151
+ [Service.init](): Generator<() => void, void, unknown>;
152
+ /** Tools offered to the model: every action except the Agent's own and those turned off. */
153
+ tools(): Promise<ToolDefinition[]>;
154
+ settings(): Promise<AgentSettings>;
155
+ private updateSettings;
156
+ private policyOf;
157
+ /** Every action with its policy, for the tool policy table. */
158
+ policies(): Promise<{
159
+ name: string;
160
+ description: string;
161
+ plugin: string;
162
+ risk: import("@mushinjs/core").Risk;
163
+ client: boolean;
164
+ policy: ToolPolicy;
165
+ custom: boolean;
166
+ }[]>;
167
+ /** Sets the policy of some actions; `null` goes back to the default for the action's risk. */
168
+ setPolicy(changes: Record<string, ToolPolicy | null>): Promise<{
169
+ name: string;
170
+ description: string;
171
+ plugin: string;
172
+ risk: import("@mushinjs/core").Risk;
173
+ client: boolean;
174
+ policy: ToolPolicy;
175
+ custom: boolean;
176
+ }[]>;
177
+ profiles(): Promise<Record<string, SceneProfile>>;
178
+ setProfile(scene: string, profile: SceneProfile | null): Promise<SceneProfile>;
179
+ /** Tokens used today and the daily budget. */
180
+ usage(): Promise<{
181
+ budget: number;
182
+ day: string;
183
+ input: number;
184
+ output: number;
185
+ }>;
186
+ private addUsage;
187
+ private emit;
188
+ private allSessions;
189
+ private summary;
190
+ sessions(): Promise<{
191
+ id: string;
192
+ title: string;
193
+ createdAt: number;
194
+ updatedAt: number;
195
+ scene: SessionScene | undefined;
196
+ tags: string[];
197
+ running: boolean;
198
+ }[]>;
199
+ /**
200
+ * Conversations matching every tag (`scene:flowEditor`, `skill:<id>`, `status:running`, …) and
201
+ * containing the query in the title or in what the user or the Agent wrote.
202
+ */
203
+ search(options?: {
204
+ query?: string;
205
+ tags?: string[];
206
+ limit?: number;
207
+ }): Promise<{
208
+ snippet?: string | undefined;
209
+ id: string;
210
+ title: string;
211
+ createdAt: number;
212
+ updatedAt: number;
213
+ scene: SessionScene | undefined;
214
+ tags: string[];
215
+ running: boolean;
216
+ }[]>;
217
+ /** Adds a skill from a plugin; removed when the plugin unloads. */
218
+ defineSkill(definition: SkillDefinition): import("cordis").Disposable<Promise<void>>;
219
+ /** Built-in, plugin and user skills; user skills win on the same id. */
220
+ skills(): Promise<AgentSkill[]>;
221
+ skill(id: string): Promise<AgentSkill>;
222
+ /** Saves a user skill. With the id of a built-in or plugin skill it replaces that one until deleted. */
223
+ saveSkill(input: SkillDefinition & {
224
+ enabled?: boolean;
225
+ }): Promise<AgentSkill>;
226
+ createSkill(input: Omit<SkillDefinition, 'id'> & {
227
+ id?: string;
228
+ }): Promise<AgentSkill>;
229
+ updateSkill(id: string, patch: Partial<Omit<SkillDefinition, 'id'>>): Promise<AgentSkill>;
230
+ /** Deletes a user skill; a built-in or plugin skill it replaced comes back. */
231
+ deleteSkill(id: string): Promise<{
232
+ deleted: string;
233
+ restored: boolean;
234
+ }>;
235
+ setSkillEnabled(id: string, enabled: boolean): Promise<AgentSkill>;
236
+ /** Agent commands and the commands of every bot, for the `/` picker. */
237
+ commands(): {
238
+ builtin: {
239
+ name: string;
240
+ description: string;
241
+ 'zh-CN': string;
242
+ }[];
243
+ bots: any;
244
+ };
245
+ session(id: string): Promise<AgentSession>;
246
+ createSession(): Promise<AgentSession>;
247
+ deleteSession(id: string): Promise<{
248
+ deleted: string;
249
+ }>;
250
+ private save;
251
+ private turnContext;
252
+ /** A bot's chat around one message, for quoting into an Agent message. */
253
+ private quoteChat;
254
+ /** Starts a turn and returns once it finishes. Progress streams as `agent/event` events. */
255
+ send(sessionId: string | undefined, text: string, options?: SendOptions): Promise<{
256
+ session: string;
257
+ }>;
258
+ private loop;
259
+ private runTool;
260
+ private waitForApproval;
261
+ confirm(sessionId: string, callId: string, approved: boolean): {
262
+ ok: boolean;
263
+ };
264
+ cancel(sessionId: string): {
265
+ ok: boolean;
266
+ };
267
+ status(): {
268
+ ready: boolean;
269
+ providers: number;
270
+ running: string[];
271
+ };
272
+ private defineActions;
273
+ }
274
+ export { Agent as default, AgentSettings, AgentSkill, BUILTIN_SKILLS, CustomRecommendation, SCENE_KEYS, SYSTEM_PROMPT, SceneInfo, SceneProfile, SkillDefinition, SkillSource, TokenUsage, ToolPolicy, TurnContext, contextNote, defaultPolicy, formatSkillMarkdown, parseSkillMarkdown, policyInput, profileInput, skillBlock, skillFields, skillInput, today };