@sagmans/dsh-provider-extra 0.8.1 → 0.9.2

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.
@@ -13,17 +13,23 @@
13
13
  async function statusLine(host, choice) {
14
14
  const reference = await host.reference(choice.providerId);
15
15
  if (reference !== undefined) {
16
- const where = reference.source === undefined ? 'not set' : 'set from ' + reference.source;
17
- return ' ' + choice.providerName + ' — API key ' + reference.ref + ' (' + where + ')';
16
+ const where = reference.source === undefined ? 'not set' : `set from ${reference.source}`;
17
+ return ` ${choice.providerName} — API key ${reference.ref} (${where})`;
18
18
  }
19
19
  const stored = await host.stored(choice.providerId);
20
- return ' ' + choice.providerName + ' — ' + (stored === undefined ? 'not signed in' : 'signed in (' + stored + ')');
20
+ return (' ' +
21
+ choice.providerName +
22
+ ' — ' +
23
+ (stored === undefined ? 'not signed in' : `signed in (${stored})`));
21
24
  }
22
25
  /** Answer how each provider authenticates today. */
23
26
  export async function statusOf(host) {
24
27
  const choices = host.choices();
25
28
  if (choices.length === 0) {
26
- return { kind: 'error', text: 'dsh-provider-extra: this composition mounts no provider with an interactive sign-in' };
29
+ return {
30
+ kind: 'error',
31
+ text: 'dsh-provider-extra: this composition mounts no provider with an interactive sign-in',
32
+ };
27
33
  }
28
34
  const seen = new Set();
29
35
  const lines = [];
@@ -3,19 +3,22 @@
3
3
  *
4
4
  * The OpenCode Go gateway rejects requests that carry no session identity
5
5
  * (HTTP 400 MissingSessionID) and uses a stable per-conversation id to optimize
6
- * routing and prompt caching. The shipped dsh pi-ai adapter forwards the
7
- * session id into pi-ai's options, but pi-ai emits session headers only for
8
- * providers whose compat opts in, so this route injects the header itself at
9
- * the provider delegate — the one boundary every dispatch path (direct
10
- * stream() and the runtime's prepareCall()) reaches with the per-request
11
- * options still in hand.
6
+ * routing and prompt caching. pi-ai's own opencode-go provider already stamps
7
+ * the request's session id, but it stays silent for sessionless requests and
8
+ * defers to any configured static header of the same name, which would pin
9
+ * every conversation to one stale identity. This route therefore injects the
10
+ * header itself, so the configured fallback answers sessionless requests and
11
+ * the live id beats a static one; pi-ai then sees the header and adds nothing.
12
+ * The injection sits at the provider delegate — the one boundary every
13
+ * dispatch path (direct stream() and the runtime's prepareCall()) reaches
14
+ * with the per-request options still in hand.
12
15
  *
13
16
  * @module dsh-provider-extra/opencode-go
14
17
  */
18
+ import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
15
19
  import { defaultProviderAuthContext } from '@earendil-works/pi-ai';
16
20
  import { builtinProviders } from '@earendil-works/pi-ai/providers/all';
17
- import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
18
- import { resolveExtraModels, selectWhitelistedModels } from "./extra-models.js";
21
+ import { resolveExtraModels, selectWhitelistedModels } from './extra-models.js';
19
22
  /** pi-ai catalog id of the OpenCode Go provider this route mirrors. */
20
23
  export const OPENCODE_GO_PROVIDER_ID = 'opencode-go';
21
24
  /** Routing header the OpenCode Go gateway requires on every model request. */
@@ -26,11 +29,19 @@ export const DEFAULT_OPENCODE_API_KEY_ENV = 'OPENCODE_API_KEY';
26
29
  export const DEEPSEEK_V41_FLASH_ID = 'deepseek-flash';
27
30
  /** Display name for DeepSeek V4.1 Flash, matching the OpenCode Go model list. */
28
31
  export const DEEPSEEK_V41_FLASH_NAME = 'DeepSeek V4.1 Flash';
29
- /** Default template sibling an extra model inherits its wire behavior from. */
30
- export const DEFAULT_EXTRA_MODEL_TEMPLATE = 'deepseek-v4-flash';
32
+ /**
33
+ * Default template sibling an extra model inherits its wire behavior from. It
34
+ * must be an id every supported pi-ai catalog ships: the 1.3.0 catalog dropped
35
+ * DeepSeek V4 Flash, and a missing template fails the shipped extra above.
36
+ */
37
+ export const DEFAULT_EXTRA_MODEL_TEMPLATE = 'deepseek-v4.1-flash';
31
38
  /** Shipped extras for ids the installed catalog predates while the gateway already serves them. */
32
39
  const DEFAULT_EXTRA_MODELS = [
33
- { id: DEEPSEEK_V41_FLASH_ID, name: DEEPSEEK_V41_FLASH_NAME, template: DEFAULT_EXTRA_MODEL_TEMPLATE },
40
+ {
41
+ id: DEEPSEEK_V41_FLASH_ID,
42
+ name: DEEPSEEK_V41_FLASH_NAME,
43
+ template: DEFAULT_EXTRA_MODEL_TEMPLATE,
44
+ },
34
45
  ];
35
46
  // The resolved profile defaults below mirror dsh-llm-pi-ai's config resolution,
36
47
  // which does not export them; drift is caught by that package's own tests.
@@ -42,16 +53,22 @@ const MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
42
53
  const REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
43
54
  /** Default raw encoded-byte target before inline base64 expansion. */
44
55
  const REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
56
+ /** Default request-level bound on retained image occurrences. */
57
+ const MAX_REQUEST_IMAGES = 50;
58
+ /** Default oldest-image offload step when the occurrence bound overflows. */
59
+ const IMAGE_OFFLOAD_COUNT_QUANTUM = 10;
45
60
  /**
46
61
  * The installed pi-ai catalog's OpenCode Go provider, whose API implementations
47
62
  * and auth this route reuses rather than reconstructing.
48
63
  * @throws Error when the installed pi-ai catalog no longer ships OpenCode Go.
49
64
  */
50
65
  function catalogOpenCodeGo() {
51
- const found = builtinProviders().find(provider => provider.id === OPENCODE_GO_PROVIDER_ID);
66
+ const found = builtinProviders().find((provider) => provider.id === OPENCODE_GO_PROVIDER_ID);
52
67
  if (found === undefined) {
53
- throw new Error('dsh-provider-extra: the installed pi-ai catalog has no provider "' + OPENCODE_GO_PROVIDER_ID + '";'
54
- + ' pin @earendil-works/pi-ai to a catalog that ships it');
68
+ throw new Error('dsh-provider-extra: the installed pi-ai catalog has no provider "' +
69
+ OPENCODE_GO_PROVIDER_ID +
70
+ '";' +
71
+ ' pin @earendil-works/pi-ai to a catalog that ships it');
55
72
  }
56
73
  return found;
57
74
  }
@@ -88,15 +105,15 @@ function withRoutingHeader(options, config) {
88
105
  * @returns the provider to register into the adapter's model collection.
89
106
  */
90
107
  function routeProvider(config, catalog, resolved) {
91
- const models = resolved.map(model => ({
108
+ const models = resolved.map((model) => ({
92
109
  ...model,
93
- ...config.baseURL === undefined ? {} : { baseUrl: config.baseURL },
110
+ ...(config.baseURL === undefined ? {} : { baseUrl: config.baseURL }),
94
111
  provider: config.provider,
95
112
  }));
96
113
  return {
97
114
  id: config.provider,
98
115
  name: config.displayName,
99
- ...config.baseURL === undefined ? {} : { baseUrl: config.baseURL },
116
+ ...(config.baseURL === undefined ? {} : { baseUrl: config.baseURL }),
100
117
  auth: catalog.auth,
101
118
  getModels: () => models,
102
119
  stream: (model, context, options) => catalog.stream(model, context, withRoutingHeader(options, config)),
@@ -119,9 +136,11 @@ export function buildOpenCodeGoProfile(config) {
119
136
  return {
120
137
  provider: config.provider,
121
138
  displayName: config.displayName,
122
- ...config.headers === undefined ? {} : { headers: { ...config.headers } },
139
+ ...(config.headers === undefined ? {} : { headers: { ...config.headers } }),
123
140
  streamIdleTimeoutMs: STREAM_IDLE_TIMEOUT_MS,
124
141
  maxRequestImageBytes: MAX_REQUEST_IMAGE_BYTES,
142
+ maxRequestImages: MAX_REQUEST_IMAGES,
143
+ imageOffloadCountQuantum: IMAGE_OFFLOAD_COUNT_QUANTUM,
125
144
  requestImagePixelBudget: REQUEST_IMAGE_PIXEL_BUDGET,
126
145
  requestImageMaxBytes: REQUEST_IMAGE_MAX_BYTES,
127
146
  retryPolicy: resolveRetryPolicy(undefined, 'dsh-provider-extra: opencode-go'),
@@ -0,0 +1,12 @@
1
+ import { LlmError } from '@deepseek-ai/dsh-llm';
2
+ /**
3
+ * Raise a failure whose code belongs to this plugin, not to the harness list.
4
+ *
5
+ * Harness 0.2.2-rc.5 narrows the constructor to its built-in codes, while the
6
+ * runtime still accepts any non-empty string and older verified releases have no
7
+ * re-raise helper. The plugin's own codes are a documented contract that callers
8
+ * and tests match on, so the type is widened here once instead of renaming them.
9
+ */
10
+ export function pluginLlmError(message, code) {
11
+ return new LlmError(message, code);
12
+ }
@@ -1,7 +1,7 @@
1
- import { mountTierActions } from "./tui-actions.js";
2
- import { mountTierRemote } from "./tier-remote.js";
3
- import { createTierStore } from "./tier-store.js";
4
- import { currentInvocationTier, mountTierInvocation } from "./tier-invocation.js";
1
+ import { currentInvocationTier, mountTierInvocation } from './tier-invocation.js';
2
+ import { mountTierRemote } from './tier-remote.js';
3
+ import { createTierStore } from './tier-store.js';
4
+ import { mountTierActions } from './tui-actions.js';
5
5
  /** Source and protocol must agree before a route can request paid processing. */
6
6
  export const CODEX_TIER_API = 'openai-codex-responses';
7
7
  const CODEX_SOURCE = 'openai-codex';
@@ -12,7 +12,11 @@ const OPENAI_TIER_APIS = ['openai-responses', 'openai-completions'];
12
12
  export const SERVICE_TIER_CHOICES = [
13
13
  { id: AUTO_TIER, name: 'Auto', description: 'Use the account service tier' },
14
14
  { id: 'default', name: 'Standard', description: 'Standard processing' },
15
- { id: 'priority', name: 'Fast', description: 'Priority processing; higher usage cost, subject to account access' },
15
+ {
16
+ id: 'priority',
17
+ name: 'Fast',
18
+ description: 'Priority processing; higher usage cost, subject to account access',
19
+ },
16
20
  ];
17
21
  const SERVICE_NAME = 'providerServiceTiers';
18
22
  const PROFILE_SERVICE = 'profileContext';
@@ -24,15 +28,16 @@ const INVOCATION_TIERS = new Map([
24
28
  const INVALID_TIER = 'unsupported service tier for this model route';
25
29
  const INVALID_PAYLOAD = 'OpenAI service tier requires an object request payload';
26
30
  /** Standalone helpers may use native route ids; mounted owners must supply authoritative sources. */
27
- const routeIdentity = provider => provider;
31
+ const routeIdentity = (provider) => provider;
28
32
  /** OpenAI-compatible gateways must not inherit native account billing policy. */
29
33
  function supportsTiers(source, api) {
30
- return source === CODEX_SOURCE ? api === CODEX_TIER_API
34
+ return source === CODEX_SOURCE
35
+ ? api === CODEX_TIER_API
31
36
  : source === OPENAI_SOURCE && OPENAI_TIER_APIS.includes(api);
32
37
  }
33
38
  /** Reject stale or hand-edited values before they become request parameters. */
34
39
  function validTier(tier) {
35
- return SERVICE_TIER_CHOICES.some(choice => choice.id === tier);
40
+ return SERVICE_TIER_CHOICES.some((choice) => choice.id === tier);
36
41
  }
37
42
  /** Startup validation shares the same canonical values and aliases as model-scoped resolution. */
38
43
  export function isServiceTierOverride(value) {
@@ -40,14 +45,21 @@ export function isServiceTierOverride(value) {
40
45
  }
41
46
  /** Durable writes complete before the selected row can truthfully report success. */
42
47
  export function createTierSelection(read, write, profiles, sourceFor = routeIdentity) {
43
- const choices = (provider, model) => profiles().get(provider)?.piProvider?.getModels().some(entry => entry.id === model && supportsTiers(sourceFor(provider), entry.api))
44
- ? SERVICE_TIER_CHOICES : [];
48
+ const choices = (provider, model) => profiles()
49
+ .get(provider)
50
+ ?.piProvider?.getModels()
51
+ .some((entry) => entry.id === model && supportsTiers(sourceFor(provider), entry.api))
52
+ ? SERVICE_TIER_CHOICES
53
+ : [];
45
54
  return {
46
55
  choices,
47
56
  resolve: (provider, model, override) => {
48
57
  const available = choices(provider, model);
49
- const tier = INVOCATION_TIERS.has(override) ? INVOCATION_TIERS.get(override) : override;
50
- if (available.length === 0 || (tier !== null && !available.some(choice => choice.id === tier)))
58
+ const mapped = INVOCATION_TIERS.get(override);
59
+ // Not `??`: a mapped null is the provider-default choice and must not become the override.
60
+ const tier = mapped === undefined ? override : mapped;
61
+ if (available.length === 0 ||
62
+ (tier !== null && !available.some((choice) => choice.id === tier)))
51
63
  throw new Error(INVALID_TIER);
52
64
  return tier;
53
65
  },
@@ -73,7 +85,8 @@ export function withServiceTiers(profiles, current, sourceFor = routeIdentity) {
73
85
  // Plugin-local request scope carries CLI policy without changing host request or session schemas.
74
86
  const invocation = currentInvocationTier();
75
87
  const override = invocation === undefined
76
- ? options?.serviceTier : invocation;
88
+ ? options?.serviceTier
89
+ : invocation;
77
90
  if (override === null) {
78
91
  if (options === undefined)
79
92
  return undefined;
@@ -86,7 +99,9 @@ export function withServiceTiers(profiles, current, sourceFor = routeIdentity) {
86
99
  return options;
87
100
  // Codex rejects literal auto; omission delegates to the account without changing the saved choice.
88
101
  const wireTier = model.api === CODEX_TIER_API && tier === AUTO_TIER ? undefined : tier;
89
- return Object.assign({}, options, { serviceTier: wireTier, onPayload: async (payload, wireModel) => {
102
+ return Object.assign({}, options, {
103
+ serviceTier: wireTier,
104
+ onPayload: async (payload, wireModel) => {
90
105
  // pi-ai's simple stream drops serviceTier, but retains this final serialization hook.
91
106
  const replacement = await options?.onPayload?.(payload, wireModel);
92
107
  const body = replacement === undefined ? payload : replacement;
@@ -98,13 +113,20 @@ export function withServiceTiers(profiles, current, sourceFor = routeIdentity) {
98
113
  else
99
114
  forwarded.service_tier = wireTier;
100
115
  return forwarded;
101
- } });
116
+ },
117
+ });
102
118
  };
103
- return [id, { ...profile, piProvider: {
119
+ return [
120
+ id,
121
+ {
122
+ ...profile,
123
+ piProvider: {
104
124
  ...source,
105
125
  stream: (model, context, options) => source.stream(model, context, optionsFor(model, options)),
106
126
  streamSimple: (model, context, options) => source.streamSimple(model, context, optionsFor(model, options)),
107
- } }];
127
+ },
128
+ },
129
+ ];
108
130
  }));
109
131
  }
110
132
  /** Plugin-owned records share selections without editing or reconciling profile configuration. */
@@ -114,7 +136,7 @@ export function mountServiceTiers(ctx, config, profiles, sourceFor = routeIdenti
114
136
  const service = createTierSelection((provider, model) => {
115
137
  const tier = store.read(provider, model);
116
138
  return tier === undefined
117
- ? config.serviceTierSelections?.find(entry => entry.provider === provider && entry.model === model)?.tier
139
+ ? config.serviceTierSelections?.find((entry) => entry.provider === provider && entry.model === model)?.tier
118
140
  : tier;
119
141
  }, store.write, profiles, sourceFor);
120
142
  ctx.provide(SERVICE_NAME, service);
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Sign-in entry: mounts the Models-page authorization transport.
3
+ *
4
+ * A separate Loader entry, rather than a line in the route plugin, keeps the
5
+ * transport independent of the catalog and additive route modes: the bundle
6
+ * patch activates it once and it never competes with route registration.
7
+ *
8
+ * @module @sagmans/dsh-provider-extra/sign-in
9
+ */
10
+ import { mountProviderAuth } from './auth-remote.js';
11
+ /** Loader identity for the sign-in entry. */
12
+ export const name = 'dsh-provider-extra-sign-in';
13
+ /**
14
+ * Mount the transport beside whatever routes this plugin serves.
15
+ * @param ctx - Host context; the transport parks itself when the harness has
16
+ * no authorization seam or Typert registry.
17
+ */
18
+ export function apply(ctx) {
19
+ mountProviderAuth(ctx);
20
+ }