@pal-code/core 0.0.10 → 0.0.11

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pal-code/core",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Core runtime for pal — agent loop, providers, tools, sessions",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -13,7 +13,7 @@
13
13
  */
14
14
 
15
15
  import {createOpenRouterProvider} from '../provider/openrouter.js';
16
- import {PROVIDERS, providerForModel} from '../provider/registry.js';
16
+ import {PROVIDERS, providerForModel, withSystemPromptAppend} from '../provider/registry.js';
17
17
  import type {ProviderDef} from '../provider/registry.js';
18
18
  import type {ProviderKeys} from '../provider/auth.js';
19
19
  import type {Provider} from '../provider/types.js';
@@ -261,6 +261,10 @@ export async function* runWithFallback(
261
261
  apiKey: keys[modelDef.id] ?? (modelDef.keyOptional ? 'public' : ''),
262
262
  baseUrl: modelDef.baseUrl,
263
263
  keys: keys as Record<string, string>,
264
+ // Same custom headers as the primary request, so child turns match it.
265
+ headers: modelDef.headers,
266
+ // Provider's extra system-prompt block, appended the same way.
267
+ systemPromptAppend: modelDef.systemPromptAppend,
264
268
  // Carried through to `executeTool` so a read-only turn's `delegate` can
265
269
  // refuse to spawn anything that would mutate files.
266
270
  readOnly: readOnly ?? false,
@@ -286,7 +290,7 @@ export async function* runWithFallback(
286
290
  tools,
287
291
  provider,
288
292
  model: bareModel,
289
- systemPrompt,
293
+ systemPrompt: withSystemPromptAppend(systemPrompt, modelDef),
290
294
  maxRounds: maxToolRounds,
291
295
  contextLimit,
292
296
  reasoningEffort,
package/src/agent/run.ts CHANGED
@@ -58,6 +58,10 @@ export interface ChildProviderConfig {
58
58
  baseUrl: string;
59
59
  /** Provider id → API key, for resolving role-specific models. */
60
60
  keys?: Record<string, string>;
61
+ /** Custom headers from the provider def, so child requests look identical to the parent's. */
62
+ headers?: Record<string, string>;
63
+ /** Provider's `systemPromptAppend` block, appended to the child's system prompt if set. */
64
+ systemPromptAppend?: string;
61
65
  /**
62
66
  * Set when the parent turn is read-only. `delegate` refuses to spawn any
63
67
  * child whose toolset would mutate state.
package/src/config.ts CHANGED
@@ -27,6 +27,10 @@ export interface ToolConfig {
27
27
  baseUrl: string;
28
28
  /** Provider id → key, so `delegate` can resolve a role's own model. */
29
29
  keys?: Record<string, string>;
30
+ /** Custom provider headers, forwarded to the child's provider. */
31
+ headers?: Record<string, string>;
32
+ /** Provider's `systemPromptAppend` block, appended to the child's system prompt if set. */
33
+ systemPromptAppend?: string;
30
34
  /** Set when the parent turn is read-only; blocks spawning mutating children. */
31
35
  readOnly?: boolean;
32
36
  /** Tool allowlist enforced by `executeTool` for the current turn. */
@@ -7,11 +7,33 @@ import type {Provider, ProviderConfig, ProviderModel, StreamEvent, Usage} from '
7
7
  import {randomBytes} from 'crypto';
8
8
  import {log} from '../logger.js';
9
9
 
10
- /** Expand `__random[:PREFIX]:N__` tokens in header values.
10
+ const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
11
+
12
+ /** `len` base62 chars from crypto random bytes.
13
+ * 248 = 62 × 4, so every accepted byte maps uniformly onto the alphabet —
14
+ * bytes ≥ 248 are rejected rather than taken modulo, keeping the distribution
15
+ * exactly uniform. */
16
+ function randomBase62(len: number): string {
17
+ let out = '';
18
+ while (out.length < len) {
19
+ for (const b of randomBytes(len - out.length)) {
20
+ if (b < 248) out += BASE62[b % 62];
21
+ if (out.length === len) break;
22
+ }
23
+ }
24
+ return out;
25
+ }
26
+
27
+ /** Expand header-value tokens.
28
+ * `__sesid__` → `ses_` + 12 lowercase hex + 14 base62
11
29
  * `__random:32__` → 32-char random hex
12
30
  * `__random:ses_:32__` → `ses_` + 32-char random hex
31
+ * Anything else is returned unchanged.
13
32
  */
14
- function expandRandomValue(val: string): string {
33
+ export function expandRandomValue(val: string): string {
34
+ if (val === '__sesid__') {
35
+ return `ses_${randomBytes(6).toString('hex')}${randomBase62(14)}`;
36
+ }
15
37
  const m = val.match(/^__random(?::([^:]+))?:(\d+)__$/);
16
38
  if (!m) return val;
17
39
  const prefix = m[1] ?? '';
@@ -12,7 +12,8 @@
12
12
  * Custom providers can be added via `~/.pal/providers.json` — an array of
13
13
  * ProviderDef objects. Use the `headers` field to inject custom HTTP headers
14
14
  * (e.g. User-Agent overrides). Header values containing `__random:N__` are
15
- * expanded to N-character random hex strings at provider init time.
15
+ * expanded to N-character random hex strings at request time; `__sesid__`
16
+ * expands to a fresh session-shaped id (`ses_` + 12 hex + 14 base62).
16
17
  */
17
18
 
18
19
  export interface ProviderDef {
@@ -38,6 +39,12 @@ export interface ProviderDef {
38
39
  freeModelPattern?: string;
39
40
  /** Custom HTTP headers sent with every request (e.g. User-Agent overrides). */
40
41
  headers?: Record<string, string>;
42
+ /**
43
+ * Text appended after the model's own system instructions when this provider
44
+ * serves the turn — carried in `messages[0].content`. Absent → the system
45
+ * prompt is untouched.
46
+ */
47
+ systemPromptAppend?: string;
41
48
  /** Short hint shown in the provider picker UI. */
42
49
  hint?: string;
43
50
  /** Provider id alias (e.g. "opencode" → resolves to this provider's id). */
@@ -220,3 +227,11 @@ export function providerForModel(
220
227
  const fallback = configured ?? getProvider('gemini')!;
221
228
  return {provider: fallback, model: modelSpec};
222
229
  }
230
+
231
+ /**
232
+ * Append a provider's `systemPromptAppend` block to a system prompt.
233
+ * No field → the prompt comes back byte-for-byte unchanged.
234
+ */
235
+ export function withSystemPromptAppend(base: string, def: {systemPromptAppend?: string}): string {
236
+ return def.systemPromptAppend ? `${base}\n\n${def.systemPromptAppend}` : base;
237
+ }
@@ -919,6 +919,8 @@ export async function executeTool(
919
919
  apiKey: string;
920
920
  baseUrl: string;
921
921
  keys?: Record<string, string>;
922
+ headers?: Record<string, string>;
923
+ systemPromptAppend?: string;
922
924
  readOnly?: boolean;
923
925
  allowedTools?: string[];
924
926
  },
@@ -1165,6 +1167,7 @@ async function executeChild(plan: ChildPlan, config: ToolConfig): Promise<string
1165
1167
  id: 'delegate',
1166
1168
  baseUrl: config.provider.baseUrl,
1167
1169
  apiKey: config.provider.apiKey,
1170
+ headers: config.provider.headers,
1168
1171
  });
1169
1172
  const providerCfg = config.provider;
1170
1173
  let childModel = providerCfg.model;
@@ -1200,6 +1203,7 @@ async function executeChild(plan: ChildPlan, config: ToolConfig): Promise<string
1200
1203
 
1201
1204
  // Dynamic import avoids the executor → run → executor import cycle.
1202
1205
  const {runAgent} = await import('../agent/run.js');
1206
+ const {withSystemPromptAppend} = await import('../provider/registry.js');
1203
1207
 
1204
1208
  await provider.init();
1205
1209
 
@@ -1213,7 +1217,7 @@ async function executeChild(plan: ChildPlan, config: ToolConfig): Promise<string
1213
1217
  tools: childTools,
1214
1218
  provider,
1215
1219
  model: childModel,
1216
- systemPrompt,
1220
+ systemPrompt: withSystemPromptAppend(systemPrompt, config.provider),
1217
1221
  maxRounds,
1218
1222
  reasoningEffort: reasoning,
1219
1223
  providerConfig: providerCfg,