toolnexus 0.2.0 → 0.3.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/a2a.d.ts +2 -0
- package/dist/client.d.ts +32 -0
- package/dist/client.js +28 -0
- package/dist/serve.d.ts +3 -2
- package/dist/serve.js +7 -4
- package/dist/skill.js +20 -12
- package/dist/toolkit.d.ts +3 -1
- package/dist/toolkit.js +6 -2
- package/package.json +26 -8
- package/src/a2a.ts +2 -0
- package/src/client.ts +45 -1
- package/src/serve.ts +11 -6
- package/src/skill.ts +19 -12
- package/src/toolkit.ts +7 -2
package/dist/a2a.d.ts
CHANGED
package/dist/client.d.ts
CHANGED
|
@@ -21,6 +21,25 @@ export interface ClientOptions {
|
|
|
21
21
|
retryBaseMs?: number;
|
|
22
22
|
/** Whole-run deadline in ms; aborts the run (and its in-flight request) when exceeded. */
|
|
23
23
|
timeoutMs?: number;
|
|
24
|
+
/** Conversation provider for `ask(prompt, { id })`. Default: in-memory (process lifetime).
|
|
25
|
+
* Supply a file/db store to persist conversations across processes. */
|
|
26
|
+
store?: ConversationStore;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Where `ask()` conversations are remembered — two methods. Ship the in-memory
|
|
30
|
+
* default; implement this for a file/db/redis provider to persist across processes.
|
|
31
|
+
*/
|
|
32
|
+
export interface ConversationStore {
|
|
33
|
+
/** Return the stored transcript for `id`, or undefined if none. */
|
|
34
|
+
get(id: string): Promise<any[] | undefined>;
|
|
35
|
+
/** Persist the (updated) transcript for `id`. */
|
|
36
|
+
save(id: string, messages: any[]): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/** Default conversation provider — keeps transcripts in memory for the client's lifetime. */
|
|
39
|
+
export declare class InMemoryConversationStore implements ConversationStore {
|
|
40
|
+
private readonly map;
|
|
41
|
+
get(id: string): Promise<any[] | undefined>;
|
|
42
|
+
save(id: string, messages: any[]): Promise<void>;
|
|
24
43
|
}
|
|
25
44
|
/**
|
|
26
45
|
* Lifecycle hooks around the agent loop. Each may observe, and (where noted)
|
|
@@ -124,12 +143,25 @@ export type StreamEvent = {
|
|
|
124
143
|
};
|
|
125
144
|
export declare class Client {
|
|
126
145
|
private readonly opts;
|
|
146
|
+
/** Conversation provider for `ask()` — from opts.store, else in-memory. */
|
|
147
|
+
private readonly store;
|
|
127
148
|
constructor(opts: ClientOptions);
|
|
128
149
|
run(prompt: string, ctx: {
|
|
129
150
|
toolkit: Toolkit;
|
|
130
151
|
signal?: AbortSignal;
|
|
131
152
|
history?: any[];
|
|
132
153
|
}): Promise<RunResult>;
|
|
154
|
+
/**
|
|
155
|
+
* Stateful ask. With an `id`, the client's `store` remembers the conversation:
|
|
156
|
+
* it loads that id's transcript, runs, saves the updated transcript, and returns
|
|
157
|
+
* the answer — so the next `ask` with the same `id` continues it. Without an
|
|
158
|
+
* `id` it is a stateless one-shot (identical to `run`).
|
|
159
|
+
*/
|
|
160
|
+
ask(prompt: string, ctx: {
|
|
161
|
+
toolkit: Toolkit;
|
|
162
|
+
id?: string;
|
|
163
|
+
signal?: AbortSignal;
|
|
164
|
+
}): Promise<RunResult>;
|
|
133
165
|
/** A stateful multi-turn conversation that retains history across sends. */
|
|
134
166
|
conversation(ctx: {
|
|
135
167
|
toolkit: Toolkit;
|
package/dist/client.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/** Default conversation provider — keeps transcripts in memory for the client's lifetime. */
|
|
2
|
+
export class InMemoryConversationStore {
|
|
3
|
+
map = new Map();
|
|
4
|
+
async get(id) {
|
|
5
|
+
const m = this.map.get(id);
|
|
6
|
+
return m ? [...m] : undefined;
|
|
7
|
+
}
|
|
8
|
+
async save(id, messages) {
|
|
9
|
+
this.map.set(id, [...messages]);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
1
12
|
const RETRYABLE = new Set([429, 500, 502, 503, 504]);
|
|
2
13
|
function delay(ms, signal) {
|
|
3
14
|
return new Promise((resolve, reject) => {
|
|
@@ -33,14 +44,31 @@ function resolveKey(opts) {
|
|
|
33
44
|
}
|
|
34
45
|
export class Client {
|
|
35
46
|
opts;
|
|
47
|
+
/** Conversation provider for `ask()` — from opts.store, else in-memory. */
|
|
48
|
+
store;
|
|
36
49
|
constructor(opts) {
|
|
37
50
|
this.opts = opts;
|
|
51
|
+
this.store = opts.store ?? new InMemoryConversationStore();
|
|
38
52
|
}
|
|
39
53
|
async run(prompt, ctx) {
|
|
40
54
|
return this.opts.style === "anthropic"
|
|
41
55
|
? this.runAnthropic(prompt, ctx.toolkit, ctx.signal, ctx.history)
|
|
42
56
|
: this.runOpenAI(prompt, ctx.toolkit, ctx.signal, ctx.history);
|
|
43
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Stateful ask. With an `id`, the client's `store` remembers the conversation:
|
|
60
|
+
* it loads that id's transcript, runs, saves the updated transcript, and returns
|
|
61
|
+
* the answer — so the next `ask` with the same `id` continues it. Without an
|
|
62
|
+
* `id` it is a stateless one-shot (identical to `run`).
|
|
63
|
+
*/
|
|
64
|
+
async ask(prompt, ctx) {
|
|
65
|
+
if (!ctx.id)
|
|
66
|
+
return this.run(prompt, { toolkit: ctx.toolkit, signal: ctx.signal });
|
|
67
|
+
const history = (await this.store.get(ctx.id)) ?? [];
|
|
68
|
+
const result = await this.run(prompt, { toolkit: ctx.toolkit, signal: ctx.signal, history });
|
|
69
|
+
await this.store.save(ctx.id, result.messages);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
44
72
|
/** A stateful multi-turn conversation that retains history across sends. */
|
|
45
73
|
conversation(ctx) {
|
|
46
74
|
return new Conversation(this, ctx.toolkit, ctx.signal);
|
package/dist/serve.d.ts
CHANGED
|
@@ -67,8 +67,9 @@ export interface StartServerOptions {
|
|
|
67
67
|
a2a?: A2AConfig;
|
|
68
68
|
/** The toolkit's skills, for the card. */
|
|
69
69
|
skills: SkillInfo[];
|
|
70
|
-
/** Runs one served Task through the client loop.
|
|
71
|
-
|
|
70
|
+
/** Runs one served Task through the client loop. `contextId` (from the A2A
|
|
71
|
+
* message) keys the conversation so served turns are remembered via the store. */
|
|
72
|
+
runTask: (text: string, contextId?: string) => Promise<RunResult>;
|
|
72
73
|
onTask?: OnTask;
|
|
73
74
|
}
|
|
74
75
|
/**
|
package/dist/serve.js
CHANGED
|
@@ -149,11 +149,12 @@ export async function startA2AServer(opts) {
|
|
|
149
149
|
}
|
|
150
150
|
if (rpc?.method === "SendMessage") {
|
|
151
151
|
const text = messageText(rpc.params);
|
|
152
|
+
const contextId = typeof rpc.params?.message?.contextId === "string" ? rpc.params.message.contextId : undefined;
|
|
152
153
|
const id = randomUUID();
|
|
153
|
-
const submitted = { id, status: { state: "submitted" } };
|
|
154
|
+
const submitted = { id, contextId, status: { state: "submitted" } };
|
|
154
155
|
// Persist submitted, kick fulfilment async, return the id immediately.
|
|
155
156
|
store.save(submitted).then(() => {
|
|
156
|
-
void fulfil(id, text, store, runTask, onTask);
|
|
157
|
+
void fulfil(id, text, store, runTask, onTask, contextId);
|
|
157
158
|
rpcOk(rpc.id, submitted);
|
|
158
159
|
});
|
|
159
160
|
return;
|
|
@@ -189,13 +190,15 @@ export async function startA2AServer(opts) {
|
|
|
189
190
|
* Background fulfilment: submitted → working → (completed | failed). Never
|
|
190
191
|
* throws — a fulfilment error becomes a `failed` Task, keeping the server alive.
|
|
191
192
|
*/
|
|
192
|
-
async function fulfil(id, text, store, runTask, onTask) {
|
|
193
|
+
async function fulfil(id, text, store, runTask, onTask, contextId) {
|
|
193
194
|
let task;
|
|
194
195
|
let result;
|
|
195
196
|
let state;
|
|
196
197
|
try {
|
|
197
198
|
await store.save({ id, status: { state: "working" } });
|
|
198
|
-
|
|
199
|
+
// contextId groups a peer's turns into one conversation — thread it so the
|
|
200
|
+
// client's ConversationStore remembers across tasks in the same context.
|
|
201
|
+
result = await runTask(text, contextId);
|
|
199
202
|
const artifact = {
|
|
200
203
|
artifactId: randomUUID(),
|
|
201
204
|
parts: [{ kind: "text", text: result.text }],
|
package/dist/skill.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { parse as parseYaml } from "yaml";
|
|
10
11
|
export const SKILL_TOOL_DESCRIPTION = `Load a specialized skill when the task at hand matches one of the skills listed in the system prompt.
|
|
11
12
|
|
|
12
13
|
Use this tool to inject the skill's instructions and resources into current conversation. The output may contain detailed workflow guidance as well as references to scripts, files, etc in the same directory as the skill.
|
|
@@ -18,24 +19,31 @@ The skill name must match one of the skills listed in your system prompt.`;
|
|
|
18
19
|
*/
|
|
19
20
|
export const SKILLS_PROMPT_PREAMBLE = "Skills provide specialized instructions and workflows for specific tasks.\n" +
|
|
20
21
|
"Use the skill tool to load a skill when a task matches its description.";
|
|
21
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Parse YAML frontmatter (between the leading `---` fences) with a real YAML
|
|
24
|
+
* parser, so folded (`>`)/literal (`|`) block scalars, quoting, and multi-line
|
|
25
|
+
* values all resolve correctly. Scalar values are coerced to strings.
|
|
26
|
+
*/
|
|
22
27
|
function parseFrontmatter(text) {
|
|
23
28
|
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text);
|
|
24
29
|
if (!match)
|
|
25
30
|
return { data: {}, content: text };
|
|
26
31
|
const data = {};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
let parsed;
|
|
33
|
+
try {
|
|
34
|
+
parsed = parseYaml(match[1]);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
parsed = null;
|
|
38
|
+
}
|
|
39
|
+
if (parsed && typeof parsed === "object") {
|
|
40
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
41
|
+
// Trim so block-scalar trailing newlines (chomping differs subtly between
|
|
42
|
+
// YAML libs) don't leak — keeps the five ports byte-identical.
|
|
43
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
44
|
+
data[key] = String(value).trim();
|
|
45
|
+
}
|
|
36
46
|
}
|
|
37
|
-
if (key)
|
|
38
|
-
data[key] = value;
|
|
39
47
|
}
|
|
40
48
|
return { data, content: match[2] };
|
|
41
49
|
}
|
package/dist/toolkit.d.ts
CHANGED
|
@@ -37,7 +37,9 @@ export declare class Toolkit {
|
|
|
37
37
|
* Serve this toolkit as an agent over HTTP. When the `a2a` profile is present
|
|
38
38
|
* (inline, or a top-level `a2a` config block the toolkit was built from), it
|
|
39
39
|
* mounts the A2A Agent Card (`/.well-known/agent-card.json`, built from skills)
|
|
40
|
-
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by
|
|
40
|
+
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by the client.
|
|
41
|
+
* A message's A2A `contextId` keys the conversation via `client.ask`, so a
|
|
42
|
+
* peer's turns are remembered through the client's ConversationStore.
|
|
41
43
|
* When `a2a` is absent, no A2A routes are mounted. Returns a stoppable handle.
|
|
42
44
|
*/
|
|
43
45
|
serve(addr: string, opts: {
|
package/dist/toolkit.js
CHANGED
|
@@ -92,7 +92,9 @@ export class Toolkit {
|
|
|
92
92
|
* Serve this toolkit as an agent over HTTP. When the `a2a` profile is present
|
|
93
93
|
* (inline, or a top-level `a2a` config block the toolkit was built from), it
|
|
94
94
|
* mounts the A2A Agent Card (`/.well-known/agent-card.json`, built from skills)
|
|
95
|
-
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by
|
|
95
|
+
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by the client.
|
|
96
|
+
* A message's A2A `contextId` keys the conversation via `client.ask`, so a
|
|
97
|
+
* peer's turns are remembered through the client's ConversationStore.
|
|
96
98
|
* When `a2a` is absent, no A2A routes are mounted. Returns a stoppable handle.
|
|
97
99
|
*/
|
|
98
100
|
serve(addr, opts) {
|
|
@@ -102,7 +104,9 @@ export class Toolkit {
|
|
|
102
104
|
addr,
|
|
103
105
|
a2a,
|
|
104
106
|
skills,
|
|
105
|
-
runTask: (text) =>
|
|
107
|
+
runTask: (text, contextId) => contextId
|
|
108
|
+
? opts.client.ask(text, { toolkit: this, id: contextId })
|
|
109
|
+
: opts.client.run(text, { toolkit: this }),
|
|
106
110
|
onTask: opts.onTask,
|
|
107
111
|
});
|
|
108
112
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toolnexus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Dynamic MCP servers + agent skills as uniform tools for any LLM (opencode-style).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,22 +11,40 @@
|
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
"files": [
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
15
18
|
"scripts": {
|
|
16
19
|
"build": "tsc -p tsconfig.json",
|
|
17
20
|
"test": "npm run build && node --experimental-strip-types --test test/unit.test.ts",
|
|
18
21
|
"example": "node --experimental-strip-types examples/basic.ts"
|
|
19
22
|
},
|
|
20
|
-
"keywords": [
|
|
23
|
+
"keywords": [
|
|
24
|
+
"mcp",
|
|
25
|
+
"llm",
|
|
26
|
+
"agent",
|
|
27
|
+
"skills",
|
|
28
|
+
"tools",
|
|
29
|
+
"openai",
|
|
30
|
+
"anthropic",
|
|
31
|
+
"gemini"
|
|
32
|
+
],
|
|
21
33
|
"license": "MIT",
|
|
22
|
-
"repository": {
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/muthuishere/toolnexus.git"
|
|
37
|
+
},
|
|
23
38
|
"homepage": "https://github.com/muthuishere/toolnexus#readme",
|
|
24
|
-
"bugs": {
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/muthuishere/toolnexus/issues"
|
|
41
|
+
},
|
|
25
42
|
"dependencies": {
|
|
26
|
-
"@modelcontextprotocol/sdk": "^1.12.0"
|
|
43
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
44
|
+
"yaml": "^2.9.0"
|
|
27
45
|
},
|
|
28
46
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
47
|
+
"@types/node": "^22.0.0",
|
|
48
|
+
"typescript": "^5.6.0"
|
|
31
49
|
}
|
|
32
50
|
}
|
package/src/a2a.ts
CHANGED
package/src/client.ts
CHANGED
|
@@ -23,6 +23,32 @@ export interface ClientOptions {
|
|
|
23
23
|
retryBaseMs?: number
|
|
24
24
|
/** Whole-run deadline in ms; aborts the run (and its in-flight request) when exceeded. */
|
|
25
25
|
timeoutMs?: number
|
|
26
|
+
/** Conversation provider for `ask(prompt, { id })`. Default: in-memory (process lifetime).
|
|
27
|
+
* Supply a file/db store to persist conversations across processes. */
|
|
28
|
+
store?: ConversationStore
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Where `ask()` conversations are remembered — two methods. Ship the in-memory
|
|
33
|
+
* default; implement this for a file/db/redis provider to persist across processes.
|
|
34
|
+
*/
|
|
35
|
+
export interface ConversationStore {
|
|
36
|
+
/** Return the stored transcript for `id`, or undefined if none. */
|
|
37
|
+
get(id: string): Promise<any[] | undefined>
|
|
38
|
+
/** Persist the (updated) transcript for `id`. */
|
|
39
|
+
save(id: string, messages: any[]): Promise<void>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Default conversation provider — keeps transcripts in memory for the client's lifetime. */
|
|
43
|
+
export class InMemoryConversationStore implements ConversationStore {
|
|
44
|
+
private readonly map = new Map<string, any[]>()
|
|
45
|
+
async get(id: string): Promise<any[] | undefined> {
|
|
46
|
+
const m = this.map.get(id)
|
|
47
|
+
return m ? [...m] : undefined
|
|
48
|
+
}
|
|
49
|
+
async save(id: string, messages: any[]): Promise<void> {
|
|
50
|
+
this.map.set(id, [...messages])
|
|
51
|
+
}
|
|
26
52
|
}
|
|
27
53
|
|
|
28
54
|
const RETRYABLE = new Set([429, 500, 502, 503, 504])
|
|
@@ -114,7 +140,11 @@ function resolveKey(opts: ClientOptions): string {
|
|
|
114
140
|
}
|
|
115
141
|
|
|
116
142
|
export class Client {
|
|
117
|
-
|
|
143
|
+
/** Conversation provider for `ask()` — from opts.store, else in-memory. */
|
|
144
|
+
private readonly store: ConversationStore
|
|
145
|
+
constructor(private readonly opts: ClientOptions) {
|
|
146
|
+
this.store = opts.store ?? new InMemoryConversationStore()
|
|
147
|
+
}
|
|
118
148
|
|
|
119
149
|
async run(prompt: string, ctx: { toolkit: Toolkit; signal?: AbortSignal; history?: any[] }): Promise<RunResult> {
|
|
120
150
|
return this.opts.style === "anthropic"
|
|
@@ -122,6 +152,20 @@ export class Client {
|
|
|
122
152
|
: this.runOpenAI(prompt, ctx.toolkit, ctx.signal, ctx.history)
|
|
123
153
|
}
|
|
124
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Stateful ask. With an `id`, the client's `store` remembers the conversation:
|
|
157
|
+
* it loads that id's transcript, runs, saves the updated transcript, and returns
|
|
158
|
+
* the answer — so the next `ask` with the same `id` continues it. Without an
|
|
159
|
+
* `id` it is a stateless one-shot (identical to `run`).
|
|
160
|
+
*/
|
|
161
|
+
async ask(prompt: string, ctx: { toolkit: Toolkit; id?: string; signal?: AbortSignal }): Promise<RunResult> {
|
|
162
|
+
if (!ctx.id) return this.run(prompt, { toolkit: ctx.toolkit, signal: ctx.signal })
|
|
163
|
+
const history = (await this.store.get(ctx.id)) ?? []
|
|
164
|
+
const result = await this.run(prompt, { toolkit: ctx.toolkit, signal: ctx.signal, history })
|
|
165
|
+
await this.store.save(ctx.id, result.messages)
|
|
166
|
+
return result
|
|
167
|
+
}
|
|
168
|
+
|
|
125
169
|
/** A stateful multi-turn conversation that retains history across sends. */
|
|
126
170
|
conversation(ctx: { toolkit: Toolkit; signal?: AbortSignal }): Conversation {
|
|
127
171
|
return new Conversation(this, ctx.toolkit, ctx.signal)
|
package/src/serve.ts
CHANGED
|
@@ -158,8 +158,9 @@ export interface StartServerOptions {
|
|
|
158
158
|
a2a?: A2AConfig
|
|
159
159
|
/** The toolkit's skills, for the card. */
|
|
160
160
|
skills: SkillInfo[]
|
|
161
|
-
/** Runs one served Task through the client loop.
|
|
162
|
-
|
|
161
|
+
/** Runs one served Task through the client loop. `contextId` (from the A2A
|
|
162
|
+
* message) keys the conversation so served turns are remembered via the store. */
|
|
163
|
+
runTask: (text: string, contextId?: string) => Promise<RunResult>
|
|
163
164
|
onTask?: OnTask
|
|
164
165
|
}
|
|
165
166
|
|
|
@@ -222,11 +223,12 @@ export async function startA2AServer(opts: StartServerOptions): Promise<ServeHan
|
|
|
222
223
|
}
|
|
223
224
|
if (rpc?.method === "SendMessage") {
|
|
224
225
|
const text = messageText(rpc.params)
|
|
226
|
+
const contextId = typeof rpc.params?.message?.contextId === "string" ? rpc.params.message.contextId : undefined
|
|
225
227
|
const id = randomUUID()
|
|
226
|
-
const submitted: A2ATask = { id, status: { state: "submitted" } }
|
|
228
|
+
const submitted: A2ATask = { id, contextId, status: { state: "submitted" } }
|
|
227
229
|
// Persist submitted, kick fulfilment async, return the id immediately.
|
|
228
230
|
store.save(submitted).then(() => {
|
|
229
|
-
void fulfil(id, text, store, runTask, onTask)
|
|
231
|
+
void fulfil(id, text, store, runTask, onTask, contextId)
|
|
230
232
|
rpcOk(rpc.id, submitted)
|
|
231
233
|
})
|
|
232
234
|
return
|
|
@@ -269,15 +271,18 @@ async function fulfil(
|
|
|
269
271
|
id: string,
|
|
270
272
|
text: string,
|
|
271
273
|
store: TaskStore,
|
|
272
|
-
runTask: (text: string) => Promise<RunResult>,
|
|
274
|
+
runTask: (text: string, contextId?: string) => Promise<RunResult>,
|
|
273
275
|
onTask?: OnTask,
|
|
276
|
+
contextId?: string,
|
|
274
277
|
): Promise<void> {
|
|
275
278
|
let task: A2ATask
|
|
276
279
|
let result: RunResult | undefined
|
|
277
280
|
let state: A2ATaskState
|
|
278
281
|
try {
|
|
279
282
|
await store.save({ id, status: { state: "working" } })
|
|
280
|
-
|
|
283
|
+
// contextId groups a peer's turns into one conversation — thread it so the
|
|
284
|
+
// client's ConversationStore remembers across tasks in the same context.
|
|
285
|
+
result = await runTask(text, contextId)
|
|
281
286
|
const artifact: A2AArtifact = {
|
|
282
287
|
artifactId: randomUUID(),
|
|
283
288
|
parts: [{ kind: "text", text: result.text }],
|
package/src/skill.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { readFileSync, readdirSync, realpathSync, statSync } from "node:fs"
|
|
8
8
|
import path from "node:path"
|
|
9
9
|
import { pathToFileURL } from "node:url"
|
|
10
|
+
import { parse as parseYaml } from "yaml"
|
|
10
11
|
import type { Tool, ToolResult } from "./types.js"
|
|
11
12
|
|
|
12
13
|
export const SKILL_TOOL_DESCRIPTION = `Load a specialized skill when the task at hand matches one of the skills listed in the system prompt.
|
|
@@ -30,23 +31,29 @@ export interface SkillInfo {
|
|
|
30
31
|
content: string // body after frontmatter
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Parse YAML frontmatter (between the leading `---` fences) with a real YAML
|
|
36
|
+
* parser, so folded (`>`)/literal (`|`) block scalars, quoting, and multi-line
|
|
37
|
+
* values all resolve correctly. Scalar values are coerced to strings.
|
|
38
|
+
*/
|
|
34
39
|
function parseFrontmatter(text: string): { data: Record<string, string>; content: string } {
|
|
35
40
|
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text)
|
|
36
41
|
if (!match) return { data: {}, content: text }
|
|
37
42
|
const data: Record<string, string> = {}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
let parsed: unknown
|
|
44
|
+
try {
|
|
45
|
+
parsed = parseYaml(match[1])
|
|
46
|
+
} catch {
|
|
47
|
+
parsed = null
|
|
48
|
+
}
|
|
49
|
+
if (parsed && typeof parsed === "object") {
|
|
50
|
+
for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
51
|
+
// Trim so block-scalar trailing newlines (chomping differs subtly between
|
|
52
|
+
// YAML libs) don't leak — keeps the five ports byte-identical.
|
|
53
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
54
|
+
data[key] = String(value).trim()
|
|
55
|
+
}
|
|
48
56
|
}
|
|
49
|
-
if (key) data[key] = value
|
|
50
57
|
}
|
|
51
58
|
return { data, content: match[2] }
|
|
52
59
|
}
|
package/src/toolkit.ts
CHANGED
|
@@ -124,7 +124,9 @@ export class Toolkit {
|
|
|
124
124
|
* Serve this toolkit as an agent over HTTP. When the `a2a` profile is present
|
|
125
125
|
* (inline, or a top-level `a2a` config block the toolkit was built from), it
|
|
126
126
|
* mounts the A2A Agent Card (`/.well-known/agent-card.json`, built from skills)
|
|
127
|
-
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by
|
|
127
|
+
* and a JSON-RPC endpoint (`SendMessage` + `GetTask`) fulfilled by the client.
|
|
128
|
+
* A message's A2A `contextId` keys the conversation via `client.ask`, so a
|
|
129
|
+
* peer's turns are remembered through the client's ConversationStore.
|
|
128
130
|
* When `a2a` is absent, no A2A routes are mounted. Returns a stoppable handle.
|
|
129
131
|
*/
|
|
130
132
|
serve(addr: string, opts: { client: Client; a2a?: A2AConfig; onTask?: OnTask }): Promise<ServeHandle> {
|
|
@@ -134,7 +136,10 @@ export class Toolkit {
|
|
|
134
136
|
addr,
|
|
135
137
|
a2a,
|
|
136
138
|
skills,
|
|
137
|
-
runTask: (text) =>
|
|
139
|
+
runTask: (text, contextId) =>
|
|
140
|
+
contextId
|
|
141
|
+
? opts.client.ask(text, { toolkit: this, id: contextId })
|
|
142
|
+
: opts.client.run(text, { toolkit: this }),
|
|
138
143
|
onTask: opts.onTask,
|
|
139
144
|
})
|
|
140
145
|
}
|