@pal-code/core 0.0.13 → 0.0.15
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/package.json +1 -1
- package/src/agent/run.ts +3 -0
- package/src/extensions.ts +4 -0
- package/src/index.ts +2 -0
- package/src/lifecycle.ts +3 -1
- package/src/tools/definitions.ts +51 -3
- package/src/tools/executor.ts +5 -0
package/package.json
CHANGED
package/src/agent/run.ts
CHANGED
|
@@ -656,6 +656,9 @@ async function* agentTurn(opts: RunAgentOptions): AsyncGenerator<AgentRunEvent>
|
|
|
656
656
|
|
|
657
657
|
// ── No tool calls → turn complete ──
|
|
658
658
|
if (toolCalls.length === 0) {
|
|
659
|
+
if (!textContent.trim()) {
|
|
660
|
+
console.warn('pal: empty assistant response (no text and no tool calls)');
|
|
661
|
+
}
|
|
659
662
|
messages.push({role: 'assistant', content: textContent});
|
|
660
663
|
yield {type: 'done', model, rounds: roundsRun, usage: usage()};
|
|
661
664
|
return;
|
package/src/extensions.ts
CHANGED
|
@@ -34,6 +34,7 @@ import {realpathSync} from 'node:fs';
|
|
|
34
34
|
import {homedir} from 'node:os';
|
|
35
35
|
import {isAbsolute, join, relative} from 'node:path';
|
|
36
36
|
import type {ToolDef} from './tools/definitions.js';
|
|
37
|
+
import {clearExtensionTools} from './tools/definitions.js';
|
|
37
38
|
import type {ToolConfig} from './config.js';
|
|
38
39
|
import type {LifecycleHandlers} from './lifecycle.js';
|
|
39
40
|
import {clearRoleProvider} from './agent/roles.js';
|
|
@@ -205,6 +206,9 @@ export async function loadExtensions(
|
|
|
205
206
|
// Clear any previously-registered role provider *before* (re)loading. If the
|
|
206
207
|
// extension that provides it is gone or fails, its roles must not linger.
|
|
207
208
|
clearRoleProvider();
|
|
209
|
+
// Same for extension tools: clear previously-registered ones *before*
|
|
210
|
+
// (re)loading, so a removed extension or MCP server's tools don't linger on /reload.
|
|
211
|
+
clearExtensionTools();
|
|
208
212
|
|
|
209
213
|
for (const pkg of extensionPackages) {
|
|
210
214
|
try {
|
package/src/index.ts
CHANGED
package/src/lifecycle.ts
CHANGED
|
@@ -71,7 +71,9 @@ export type LifecycleHandlers = {
|
|
|
71
71
|
/**
|
|
72
72
|
* Replace the registered lifecycle hooks. Called when extensions load and on
|
|
73
73
|
* `/reload` — replace rather than merge so a removed extension's hooks neither
|
|
74
|
-
* linger nor duplicate (
|
|
74
|
+
* linger nor duplicate. (Unlike `registerExtensionTools`, which merges — tools
|
|
75
|
+
* may register themselves from inside an extension's `setup()` before the load
|
|
76
|
+
* list arrives.)
|
|
75
77
|
*/
|
|
76
78
|
export function registerExtensionLifecycle(hooks: readonly LifecycleHandlers[]): void {
|
|
77
79
|
registry.lifecycleHooks = [...hooks];
|
package/src/tools/definitions.ts
CHANGED
|
@@ -22,17 +22,65 @@ export interface ToolDef {
|
|
|
22
22
|
* so that `delegate` can hand the *full* toolset to child agents. Without this
|
|
23
23
|
* a role like `librarian`, whose tools are all extension-provided, would spawn
|
|
24
24
|
* a child with no usable tools at all.
|
|
25
|
+
*
|
|
26
|
+
* Two registration paths feed this: extensions like @pal-code/mcp register
|
|
27
|
+
* discovered tools from inside their `setup()` (packages/mcp/src/index.ts:58),
|
|
28
|
+
* while the rest arrive via `loadExtensions`' returned list registered by the
|
|
29
|
+
* CLI (packages/cli/src/turn.ts:58).
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Merge the registered extension tools by `function.name` — a later
|
|
33
|
+
* registration of the same name wins (upsert). Called on load and on `/reload`.
|
|
34
|
+
*
|
|
35
|
+
* Merge, not replace: replacing wiped whichever path registered first — e.g.
|
|
36
|
+
* turn.ts's register of the load list erased the 34 MCP tools mcp's `setup()`
|
|
37
|
+
* had already registered ("34 MCP tools wiped to 4"). Removal of reload-stale
|
|
38
|
+
* tools is `clearExtensionTools()`'s job, called at load start.
|
|
39
|
+
* Non-array input still resets the registry (defensive).
|
|
25
40
|
*/
|
|
26
|
-
/** Replace the registered extension tools. Called on load and on `/reload`. */
|
|
27
41
|
export function registerExtensionTools(tools: ToolDef[]): void {
|
|
28
|
-
|
|
42
|
+
if (!Array.isArray(tools)) {
|
|
43
|
+
registry.extensionTools = [];
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// O(n): one Map keyed on function.name; existing entries keep their order.
|
|
47
|
+
const byName = new Map(registry.extensionTools.map((t) => [t.function.name, t]));
|
|
48
|
+
for (const tool of tools) byName.set(tool.function.name, tool);
|
|
49
|
+
registry.extensionTools = [...byName.values()];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Drop every registered extension tool — called before (re)loading extensions. */
|
|
53
|
+
export function clearExtensionTools(): void {
|
|
54
|
+
registry.extensionTools = [];
|
|
29
55
|
}
|
|
30
56
|
|
|
31
57
|
/** The extension tools currently registered (read-only view). */
|
|
32
|
-
export function getExtensionTools():
|
|
58
|
+
export function getExtensionTools(): ToolDef[] {
|
|
33
59
|
return registry.extensionTools;
|
|
34
60
|
}
|
|
35
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Presence check for every `required` argument in the tool's JSON schema.
|
|
64
|
+
* Returns the model-facing error string, or `null` when the call is acceptable.
|
|
65
|
+
*
|
|
66
|
+
* Fail-open by design: an unknown tool is the executor's unknown-tool branch
|
|
67
|
+
* (no schema here to check against), and a schema without `required` has
|
|
68
|
+
* nothing to check. A key present with any value — including `null` — counts
|
|
69
|
+
* as provided; this only catches *absent* keys, e.g. `edit_file` dispatched
|
|
70
|
+
* without `path` (the `ENOENT ... open 'undefined'` failure mode).
|
|
71
|
+
*/
|
|
72
|
+
export function validateRequiredArgs(name: string, args: Record<string, unknown>): string | null {
|
|
73
|
+
const params = getAllTools().find((t) => t.function.name === name)?.function.parameters;
|
|
74
|
+
if (!params || !('required' in params)) return null;
|
|
75
|
+
const required: unknown = params.required;
|
|
76
|
+
if (!Array.isArray(required)) return null;
|
|
77
|
+
const missing = required.filter((key) => typeof key === 'string' && args[key] === undefined);
|
|
78
|
+
if (missing.length === 0) return null;
|
|
79
|
+
return missing.length === 1
|
|
80
|
+
? `missing required argument: ${String(missing[0])}`
|
|
81
|
+
: `missing required argument(s): ${missing.map(String).join(', ')}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
36
84
|
/**
|
|
37
85
|
* Get all tools — core tools plus registered extension tools, plus any
|
|
38
86
|
* explicitly supplied ones (kept for callers that aren't the registry).
|
package/src/tools/executor.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {snapshotFile} from '../snapshots.js';
|
|
|
26
26
|
import {noteEditedFile} from '../diagnostics.js';
|
|
27
27
|
import type {ConversationMessage} from '../agent/run.js';
|
|
28
28
|
import {registry} from '../registry.js';
|
|
29
|
+
import {validateRequiredArgs} from './definitions.js';
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* `rg --glob '!pat'` args for each `.pal.json` ignore pattern, so ripgrep
|
|
@@ -1077,6 +1078,10 @@ export async function executeTool(
|
|
|
1077
1078
|
const args = (
|
|
1078
1079
|
argsStr && argsStr.trim() ? JSON.parse(argsStr.replaceAll('\u0000', '')) : {}
|
|
1079
1080
|
) as Record<string, unknown>;
|
|
1081
|
+
const validationError = validateRequiredArgs(name, args);
|
|
1082
|
+
if (validationError) {
|
|
1083
|
+
return {toolCallId, content: validationError, error: true};
|
|
1084
|
+
}
|
|
1080
1085
|
const config = loadAgentConfig();
|
|
1081
1086
|
if (providerConfigOverride) {
|
|
1082
1087
|
config.provider = providerConfigOverride;
|