@independo/inderun-web 0.3.3 → 0.4.0-rc.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.
Files changed (52) hide show
  1. package/README.md +46 -9
  2. package/dist/core/engine.d.ts +26 -2
  3. package/dist/core/engine.d.ts.map +1 -1
  4. package/dist/core/engine.js +78 -34
  5. package/dist/core/errors.d.ts +13 -1
  6. package/dist/core/errors.d.ts.map +1 -1
  7. package/dist/core/errors.js +32 -0
  8. package/dist/core/model-profiles.d.ts +34 -0
  9. package/dist/core/model-profiles.d.ts.map +1 -0
  10. package/dist/core/model-profiles.js +49 -0
  11. package/dist/core/provenance.d.ts +32 -0
  12. package/dist/core/provenance.d.ts.map +1 -0
  13. package/dist/core/provenance.js +71 -0
  14. package/dist/core/provider.d.ts +13 -0
  15. package/dist/core/provider.d.ts.map +1 -1
  16. package/dist/core/route-planner.d.ts +1 -1
  17. package/dist/core/route-planner.d.ts.map +1 -1
  18. package/dist/core/route-planner.js +2 -1
  19. package/dist/core/router.d.ts +4 -3
  20. package/dist/core/router.d.ts.map +1 -1
  21. package/dist/core/router.js +4 -3
  22. package/dist/facade/text.d.ts +123 -0
  23. package/dist/facade/text.d.ts.map +1 -0
  24. package/dist/facade/text.js +234 -0
  25. package/dist/index.d.ts +36 -45
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +28 -38
  28. package/dist/provider.d.ts +34 -0
  29. package/dist/provider.d.ts.map +1 -0
  30. package/dist/provider.js +33 -0
  31. package/dist/providers/onnx/provider.d.ts.map +1 -1
  32. package/dist/providers/onnx/provider.js +1 -0
  33. package/dist/providers/openai/provider.d.ts +41 -11
  34. package/dist/providers/openai/provider.d.ts.map +1 -1
  35. package/dist/providers/openai/provider.js +168 -58
  36. package/dist/web-sdk.d.ts +21 -3
  37. package/dist/web-sdk.d.ts.map +1 -1
  38. package/dist/web-sdk.js +23 -6
  39. package/package.json +7 -3
  40. package/src/core/engine.ts +114 -35
  41. package/src/core/errors.ts +35 -1
  42. package/src/core/model-profiles.ts +70 -0
  43. package/src/core/provenance.ts +90 -0
  44. package/src/core/provider.ts +13 -0
  45. package/src/core/route-planner.ts +3 -1
  46. package/src/core/router.ts +12 -3
  47. package/src/facade/text.ts +349 -0
  48. package/src/index.ts +49 -95
  49. package/src/provider.ts +77 -0
  50. package/src/providers/onnx/provider.ts +1 -0
  51. package/src/providers/openai/provider.ts +232 -68
  52. package/src/web-sdk.ts +47 -10
@@ -0,0 +1,32 @@
1
+ import type { ExecutionProvenance, IndeRunErrorClass } from "@independo/inderun-contracts";
2
+ import type { ProviderDescriptor } from "./provider.js";
3
+ import type { SharedPlannerRoutePlan } from "./route-planner.js";
4
+ type ExecutionProvenanceAttempt = ExecutionProvenance["attempts"][number];
5
+ /**
6
+ * Accumulates what one execution did -- each attempt and how it ended -- and renders it as the
7
+ * normalized {@link ExecutionProvenance} carried on results, terminal outcomes, and errors.
8
+ *
9
+ * Everything recorded is either observed by the engine (attempts, their outcomes) or declared or
10
+ * reported by a provider (descriptor type and model, the model a response names). Nothing is
11
+ * inferred: an unknown is left absent or reported as `unknown`.
12
+ */
13
+ export declare class ProvenanceRecorder {
14
+ private readonly modelProfile;
15
+ private readonly attempts;
16
+ private routeRejections;
17
+ constructor(modelProfile?: string);
18
+ /** Records the planner's rejections; codes only, never the provider-supplied messages. */
19
+ routed(routePlan: SharedPlannerRoutePlan): void;
20
+ /** Records the rejections a routing failure carried out on its exception's details. */
21
+ routingFailed(details: Record<string, unknown> | undefined): void;
22
+ /** Records how one provider attempt ended. */
23
+ attempted(providerId: string, outcome: ExecutionProvenanceAttempt["outcome"], errorClass?: IndeRunErrorClass): void;
24
+ /**
25
+ * Renders the record. `attributedTo` is the provider the outcome belongs to -- the one that
26
+ * produced the result, the one a stream was committed to, or the one running at cancellation --
27
+ * and is absent when no provider was reached or every attempt failed before a commit.
28
+ */
29
+ build(attributedTo?: ProviderDescriptor, reportedModel?: string): ExecutionProvenance;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=provenance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provenance.d.ts","sourceRoot":"","sources":["../../src/core/provenance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAC3F,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAGjE,KAAK,0BAA0B,GAAG,mBAAmB,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAG1E;;;;;;;GAOG;AACH,qBAAa,kBAAkB;IAIjB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAHzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoC;IAC7D,OAAO,CAAC,eAAe,CAAwB;gBAElB,YAAY,GAAE,MAA2B;IAEtE,0FAA0F;IAC1F,MAAM,CAAC,SAAS,EAAE,sBAAsB,GAAG,IAAI;IAI/C,uFAAuF;IACvF,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,IAAI;IASjE,8CAA8C;IAC9C,SAAS,CACP,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,0BAA0B,CAAC,SAAS,CAAC,EAC9C,UAAU,CAAC,EAAE,iBAAiB,GAC7B,IAAI;IAQP;;;;OAIG;IACH,KAAK,CAAC,YAAY,CAAC,EAAE,kBAAkB,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,mBAAmB;CActF"}
@@ -0,0 +1,71 @@
1
+ import { AUTO_MODEL_PROFILE } from "./model-profiles.js";
2
+ /**
3
+ * Accumulates what one execution did -- each attempt and how it ended -- and renders it as the
4
+ * normalized {@link ExecutionProvenance} carried on results, terminal outcomes, and errors.
5
+ *
6
+ * Everything recorded is either observed by the engine (attempts, their outcomes) or declared or
7
+ * reported by a provider (descriptor type and model, the model a response names). Nothing is
8
+ * inferred: an unknown is left absent or reported as `unknown`.
9
+ */
10
+ export class ProvenanceRecorder {
11
+ modelProfile;
12
+ attempts = [];
13
+ routeRejections = [];
14
+ constructor(modelProfile = AUTO_MODEL_PROFILE) {
15
+ this.modelProfile = modelProfile;
16
+ }
17
+ /** Records the planner's rejections; codes only, never the provider-supplied messages. */
18
+ routed(routePlan) {
19
+ this.routeRejections = routeRejectionsFrom(routePlan.rejectedProviders);
20
+ }
21
+ /** Records the rejections a routing failure carried out on its exception's details. */
22
+ routingFailed(details) {
23
+ const rejected = details?.rejectedProviders;
24
+ if (Array.isArray(rejected)) {
25
+ this.routeRejections = routeRejectionsFrom(rejected);
26
+ }
27
+ }
28
+ /** Records how one provider attempt ended. */
29
+ attempted(providerId, outcome, errorClass) {
30
+ this.attempts.push({
31
+ providerId,
32
+ outcome,
33
+ ...(errorClass !== undefined ? { errorClass } : {})
34
+ });
35
+ }
36
+ /**
37
+ * Renders the record. `attributedTo` is the provider the outcome belongs to -- the one that
38
+ * produced the result, the one a stream was committed to, or the one running at cancellation --
39
+ * and is absent when no provider was reached or every attempt failed before a commit.
40
+ */
41
+ build(attributedTo, reportedModel) {
42
+ return {
43
+ schemaVersion: "1.0",
44
+ modelProfile: this.modelProfile,
45
+ ...(attributedTo !== undefined ? { providerId: attributedTo.id } : {}),
46
+ locality: attributedTo !== undefined ? localityOf(attributedTo) : "unknown",
47
+ ...(attributedTo?.model !== undefined ? { configuredModel: attributedTo.model } : {}),
48
+ ...(attributedTo !== undefined && reportedModel !== undefined ? { reportedModel } : {}),
49
+ attemptCount: this.attempts.length,
50
+ fallbackOccurred: this.attempts.length > 1,
51
+ attempts: [...this.attempts],
52
+ routeRejections: [...this.routeRejections]
53
+ };
54
+ }
55
+ }
56
+ function localityOf(descriptor) {
57
+ switch (descriptor.type) {
58
+ case "local":
59
+ return "on_device";
60
+ case "edge":
61
+ return "edge";
62
+ case "cloud":
63
+ return "cloud";
64
+ }
65
+ }
66
+ function routeRejectionsFrom(rejected) {
67
+ return rejected.map((provider) => ({
68
+ providerId: provider.providerId,
69
+ codes: provider.reasons.map((reason) => reason.code)
70
+ }));
71
+ }
@@ -13,6 +13,14 @@ export interface ProviderDescriptor {
13
13
  * Execution target type: 'local' (on-device), 'edge', or 'cloud'.
14
14
  */
15
15
  type: "local" | "edge" | "cloud";
16
+ /**
17
+ * The model this provider instance is bound to, when it can name one. A provider
18
+ * instance bound to one model is a *target* that model profiles list by `id`.
19
+ * Cloud providers report the model id they send; platform-managed models report
20
+ * an opaque platform alias rather than a guessed weight name. Absent means the
21
+ * provider cannot name its model.
22
+ */
23
+ model?: string;
16
24
  /**
17
25
  * Connection transport layer protocol.
18
26
  */
@@ -175,6 +183,11 @@ export type ProviderStreamEvent = {
175
183
  */
176
184
  finishReason?: TaskResult["finishReason"];
177
185
  usage?: TaskResult["usage"];
186
+ /**
187
+ * The model identifier the provider's backend reported for this run, when it reports
188
+ * one. Carried into the terminal outcome's provenance; never inferred from configuration.
189
+ */
190
+ reportedModel?: string;
178
191
  } | {
179
192
  kind: "error";
180
193
  error: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/core/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,WAAW,EACX,UAAU,EACX,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;IACjC;;OAEG;IACH,SAAS,EAAE,YAAY,GAAG,gBAAgB,GAAG,MAAM,GAAG,KAAK,GAAG,UAAU,CAAC;IACzE;;OAEG;IACH,cAAc,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,WAAW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ACE,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,YAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC;IAC1C,KAAK,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;IAC5B;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,GACD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,QAAQ,IAAI,kBAAkB,CAAC;IAC/B;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC;IACvE;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5D;;;OAGG;IACH,MAAM,CAAC,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,qBAAqB,GAAG,aAAa,CAAC,mBAAmB,CAAC,CAAC;CAC3F;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IACnC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B"}
@@ -81,5 +81,5 @@ export declare function collectProviderRuntimeSnapshots(registryProviders: Provi
81
81
  * consumes, applying default constraints/preferences and the current network
82
82
  * state.
83
83
  */
84
- export declare function buildSharedPlannerInput(request: TaskRequest, snapshots: ProviderRuntimeSnapshot[], networkOnline: boolean, interactionMode?: InteractionMode): SharedPlannerInput;
84
+ export declare function buildSharedPlannerInput(request: TaskRequest, snapshots: ProviderRuntimeSnapshot[], networkOnline: boolean, interactionMode?: InteractionMode, modelProfile?: SharedPlannerInput["modelProfile"]): SharedPlannerInput;
85
85
  //# sourceMappingURL=route-planner.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"route-planner.d.ts","sourceRoot":"","sources":["../../src/core/route-planner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,IAAI,sBAAsB,EACnC,iBAAiB,IAAI,kBAAkB,EACvC,WAAW,EACZ,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,KAAK,EACV,eAAe,EAGhB,MAAM,eAAe,CAAC;AAEvB,YAAY,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,CAAC;AAE3D;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,kBAAkB,CAAC,iBAAiB,CAAC,CAAC,CAAC;AAEjF;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAChE;AAED;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAC/B,eAAe,GAAG,sBAAsB,GAAG,aAAa,GAAG,aAAa,CAAC;AAE3E;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,sBAAsB,GAAG,IAAI,CAAC;IACzC,iBAAiB,CAAC,EAAE,qBAAqB,CAAC;CAC3C;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,SAAS,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAC/D;AAED;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,QAAQ,EAAE,eAAe,CAAC;IAC1B,UAAU,EAAE,kBAAkB,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;IAClE,YAAY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EAAE,OAAO,EACtB,eAAe,GAAE,eAAuB,EACxC,YAAY,CAAC,EAAE,kBAAkB,CAAC,cAAc,CAAC,GAChD,kBAAkB,CAwBpB"}
@@ -94,7 +94,7 @@ export async function collectProviderRuntimeSnapshots(registryProviders, hostSer
94
94
  * consumes, applying default constraints/preferences and the current network
95
95
  * state.
96
96
  */
97
- export function buildSharedPlannerInput(request, snapshots, networkOnline, interactionMode = "run") {
97
+ export function buildSharedPlannerInput(request, snapshots, networkOnline, interactionMode = "run", modelProfile) {
98
98
  const constraints = request.constraints ?? {};
99
99
  const preferences = request.preferences ?? {};
100
100
  return {
@@ -111,6 +111,7 @@ export function buildSharedPlannerInput(request, snapshots, networkOnline, inter
111
111
  preferences: {
112
112
  optimizeFor: preferences.optimizeFor ?? "balanced"
113
113
  },
114
+ ...(modelProfile ? { modelProfile } : {}),
114
115
  providers: snapshots.map((snapshot) => ({
115
116
  descriptor: snapshot.descriptor,
116
117
  capabilities: snapshot.capabilities
@@ -2,7 +2,7 @@ import type { TaskRequest } from "@independo/inderun-contracts";
2
2
  import type { HostServices } from "./host.js";
3
3
  import type { ProviderAdapter } from "./provider.js";
4
4
  import type { ProviderRegistry } from "./registry.js";
5
- import { type InteractionMode, type RoutePlanner, type SharedPlannerRoutePlan } from "./route-planner.js";
5
+ import { type InteractionMode, type RoutePlanner, type SharedPlannerInput, type SharedPlannerRoutePlan } from "./route-planner.js";
6
6
  /**
7
7
  * Output structure from the Router containing the chosen provider and a route selection description.
8
8
  */
@@ -45,9 +45,10 @@ export declare class Router {
45
45
  *
46
46
  * The requested `interactionMode` is a planning input, not a post-filter: the
47
47
  * shared planner rejects providers that cannot satisfy it and says why, so the
48
- * whole returned chain is mode-compatible.
48
+ * whole returned chain is mode-compatible. The resolved model profile is a
49
+ * planning input in the same way: the planner rejects targets outside it.
49
50
  */
50
- selectRoute(request: TaskRequest, hostServices: HostServices, interactionMode?: InteractionMode): Promise<RouteSelection>;
51
+ selectRoute(request: TaskRequest, hostServices: HostServices, interactionMode?: InteractionMode, modelProfile?: SharedPlannerInput["modelProfile"]): Promise<RouteSelection>;
51
52
  private selectFromSharedPlan;
52
53
  private buildSelectionFromRoutePlan;
53
54
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/core/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAOtD,OAAO,EAGL,KAAK,eAAe,EAEpB,KAAK,YAAY,EACjB,KAAK,sBAAsB,EAE5B,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,QAAQ,EAAE,eAAe,CAAC;IAC1B;;OAEG;IACH,iBAAiB,EAAE,eAAe,EAAE,CAAC;IACrC;;OAEG;IACH,SAAS,EAAE,sBAAsB,CAAC;IAClC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,qBAAa,MAAM;IAEf,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,OAAO;gBADP,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,GAAE,YAAqC;IAGxD;;;;;;;;;;;;;OAaG;IACG,WAAW,CACf,OAAO,EAAE,WAAW,EACpB,YAAY,EAAE,YAAY,EAC1B,eAAe,GAAE,eAAuB,GACvC,OAAO,CAAC,cAAc,CAAC;IAoB1B,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,2BAA2B;IAuBnC;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;CAkBzB"}
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/core/router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAOtD,OAAO,EAGL,KAAK,eAAe,EAEpB,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAE5B,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,QAAQ,EAAE,eAAe,CAAC;IAC1B;;OAEG;IACH,iBAAiB,EAAE,eAAe,EAAE,CAAC;IACrC;;OAEG;IACH,SAAS,EAAE,sBAAsB,CAAC;IAClC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,qBAAa,MAAM;IAEf,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,OAAO;gBADP,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,GAAE,YAAqC;IAGxD;;;;;;;;;;;;;;OAcG;IACG,WAAW,CACf,OAAO,EAAE,WAAW,EACpB,YAAY,EAAE,YAAY,EAC1B,eAAe,GAAE,eAAuB,EACxC,YAAY,CAAC,EAAE,kBAAkB,CAAC,cAAc,CAAC,GAChD,OAAO,CAAC,cAAc,CAAC;IA0B1B,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,2BAA2B;IAuBnC;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;CAkBzB"}
@@ -24,12 +24,13 @@ export class Router {
24
24
  *
25
25
  * The requested `interactionMode` is a planning input, not a post-filter: the
26
26
  * shared planner rejects providers that cannot satisfy it and says why, so the
27
- * whole returned chain is mode-compatible.
27
+ * whole returned chain is mode-compatible. The resolved model profile is a
28
+ * planning input in the same way: the planner rejects targets outside it.
28
29
  */
29
- async selectRoute(request, hostServices, interactionMode = "run") {
30
+ async selectRoute(request, hostServices, interactionMode = "run", modelProfile) {
30
31
  const online = await hostServices.connectivity.isOnline();
31
32
  const snapshots = await collectProviderRuntimeSnapshots(this.registry.list(), hostServices);
32
- const planInput = buildSharedPlannerInput(request, snapshots, online, interactionMode);
33
+ const planInput = buildSharedPlannerInput(request, snapshots, online, interactionMode, modelProfile);
33
34
  const outcome = await this.planner.planRoute(planInput);
34
35
  if (!outcome.routePlan) {
35
36
  // There is no second planner to degrade to: routing is the shared Rust
@@ -0,0 +1,123 @@
1
+ import type { ExecutionProvenance, StreamEvent, StreamTerminalOutcome, TaskRequest, TaskResult } from "@independo/inderun-contracts";
2
+ import type { IndeRunApi } from "../core/generated/inderun-api.js";
3
+ /**
4
+ * The primary text API: `generateText()` and `streamText()`.
5
+ *
6
+ * Both are translation only. Each call compiles to exactly one canonical `TaskRequest`
7
+ * ({@link toTaskRequest}) and calls `run()` or `stream()` on the `IndeRunApi` it is given, so
8
+ * routing, fallback, cancellation, credentials, telemetry, and error normalization are the
9
+ * engine's and identical to a direct call. Written against the generated interface rather than
10
+ * the engine class, so anything that implements `IndeRunApi` -- a bridge included -- gets the
11
+ * same facade. See `docs/architecture/api-layering.md`.
12
+ */
13
+ type Message = NonNullable<TaskRequest["messages"]>[number];
14
+ /**
15
+ * Compact execution policy. Reuses the canonical enum values, so moving a call between the
16
+ * facade and `run()` never changes what a constraint means.
17
+ */
18
+ export interface TextExecutionOptions {
19
+ /** `constraints.privacy`: how strict placement is, e.g. `"local_required"`. */
20
+ privacy?: NonNullable<TaskRequest["constraints"]>["privacy"];
21
+ /** `constraints.cloud`: `"forbidden"`, `"allowed"`, or `"required"`. */
22
+ cloud?: NonNullable<TaskRequest["constraints"]>["cloud"];
23
+ /** `constraints.timeoutMs`. */
24
+ timeoutMs?: number;
25
+ /** `preferences.optimizeFor`. */
26
+ optimizeFor?: NonNullable<TaskRequest["preferences"]>["optimizeFor"];
27
+ }
28
+ interface TextGenerationCommonOptions {
29
+ /**
30
+ * Model profile to route within, as configured on the engine. Omitted or `"auto"` routes
31
+ * across every registered target. Never a vendor model id.
32
+ */
33
+ model?: string;
34
+ /** Instructions sent as a leading `system` message. */
35
+ system?: string;
36
+ maxOutputTokens?: number;
37
+ temperature?: number;
38
+ topP?: number;
39
+ stop?: string[];
40
+ seed?: number;
41
+ execution?: TextExecutionOptions;
42
+ /** Secure-storage slot for provider credentials. Never a raw secret. */
43
+ authContextRef?: string;
44
+ telemetry?: TaskRequest["telemetry"];
45
+ /** Fixes the run id, for correlation. */
46
+ requestId?: string;
47
+ }
48
+ /** Options for {@link generateText} and {@link streamText}: a prompt or a message list. */
49
+ export type TextGenerationOptions = TextGenerationCommonOptions & ({
50
+ prompt: string;
51
+ messages?: never;
52
+ } | {
53
+ messages: Message[];
54
+ prompt?: never;
55
+ });
56
+ /** What {@link generateText} returns. */
57
+ export interface GeneratedText {
58
+ text: string;
59
+ finishReason: TaskResult["finishReason"];
60
+ usage?: TaskResult["usage"];
61
+ runId: string;
62
+ /** How the run played out; see `ExecutionProvenance`. */
63
+ provenance?: ExecutionProvenance;
64
+ /** The canonical result, for anything the summary above leaves out. */
65
+ taskResult: TaskResult;
66
+ }
67
+ /** How a {@link streamText} run ended, when it did not fail. */
68
+ export interface StreamedText {
69
+ /** The full text, or the partial text a cancelled run had delivered. */
70
+ text: string;
71
+ /** `"cancelled"` when the run was cancelled; otherwise as the provider reported it. */
72
+ finishReason?: TaskResult["finishReason"];
73
+ usage?: TaskResult["usage"];
74
+ runId: string;
75
+ provenance?: ExecutionProvenance;
76
+ /** The canonical terminal outcome. */
77
+ outcome: StreamTerminalOutcome;
78
+ }
79
+ /** What {@link streamText} returns. */
80
+ export interface TextStream {
81
+ runId: string;
82
+ /**
83
+ * Text increments, in order. Ends when the run completes or is cancelled; throws the run's
84
+ * `IndeRunException` when it fails, and {@link TextStreamRetractedError} when the provider
85
+ * replaces delivered text (use `fullStream` to render retractions).
86
+ */
87
+ textStream: AsyncIterable<string>;
88
+ /** The canonical `StreamEvent` sequence, terminal included. */
89
+ fullStream: AsyncIterable<StreamEvent>;
90
+ /**
91
+ * Settles with the run's ending: resolves on completion or cancellation, rejects with the
92
+ * run's `IndeRunException` on failure.
93
+ */
94
+ result: Promise<StreamedText>;
95
+ /** Cancels the run; idempotent. */
96
+ cancel(reason?: string): void;
97
+ }
98
+ /**
99
+ * Thrown by `textStream` when the provider replaces text it already delivered with text that
100
+ * does not extend it, which an append-only text view cannot represent.
101
+ */
102
+ export declare class TextStreamRetractedError extends Error {
103
+ readonly runId: string;
104
+ constructor(runId: string);
105
+ }
106
+ /** The canonical `TaskRequest` a facade call compiles to. */
107
+ export declare function toTaskRequest(options: TextGenerationOptions): TaskRequest;
108
+ /**
109
+ * Generates text in one call: `run()` over the canonical request. Not cancellable, for the same
110
+ * reason `run()` is not; use {@link streamText} when cancellation matters.
111
+ * @throws {IndeRunException} Exactly as `run()` does.
112
+ */
113
+ export declare function generateText(inderun: IndeRunApi, options: TextGenerationOptions): Promise<GeneratedText>;
114
+ /**
115
+ * Streams text: `stream()` over the canonical request. The run starts immediately and is read
116
+ * once, into a replay buffer, so `textStream`, `fullStream`, and `result` can each be consumed
117
+ * in any order (or not at all) without stalling the others.
118
+ * @throws {IndeRunException} Exactly as `stream()` does: validation and routing refusals reject
119
+ * here, before there is a run.
120
+ */
121
+ export declare function streamText(inderun: IndeRunApi, options: TextGenerationOptions): Promise<TextStream>;
122
+ export {};
123
+ //# sourceMappingURL=text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../src/facade/text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,WAAW,EACX,qBAAqB,EACrB,WAAW,EACX,UAAU,EACX,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAGnE;;;;;;;;;GASG;AAEH,KAAK,OAAO,GAAG,WAAW,CAAC,WAAW,CAA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O,CAAC,aAAa,CAAC,CAUxB;AAED;;;;;;GAMG;AACH,wBAAsB,UAAU,CAC9B,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,UAAU,CAAC,CA2CrB"}
@@ -0,0 +1,234 @@
1
+ import { IndeRunException } from "../core/errors.js";
2
+ import { AUTO_MODEL_PROFILE } from "../core/model-profiles.js";
3
+ /**
4
+ * Thrown by `textStream` when the provider replaces text it already delivered with text that
5
+ * does not extend it, which an append-only text view cannot represent.
6
+ */
7
+ export class TextStreamRetractedError extends Error {
8
+ runId;
9
+ constructor(runId) {
10
+ super(`Run ${runId} replaced text it had already delivered; read fullStream to render retractions.`);
11
+ this.runId = runId;
12
+ this.name = "TextStreamRetractedError";
13
+ }
14
+ }
15
+ /** The canonical `TaskRequest` a facade call compiles to. */
16
+ export function toTaskRequest(options) {
17
+ const request = { schemaVersion: "1.0", task: { kind: "text_to_text" } };
18
+ if (options.system !== undefined) {
19
+ const conversation = options.messages ??
20
+ (options.prompt !== undefined ? [{ role: "user", content: options.prompt }] : []);
21
+ request.messages = [{ role: "system", content: options.system }, ...conversation];
22
+ }
23
+ else if (options.messages !== undefined) {
24
+ // An empty list is the engine's to reject, with the same validation error as run().
25
+ request.messages = options.messages;
26
+ }
27
+ else if (options.prompt !== undefined) {
28
+ request.prompt = options.prompt;
29
+ }
30
+ if (options.model !== undefined && options.model !== AUTO_MODEL_PROFILE) {
31
+ request.modelProfile = options.model;
32
+ }
33
+ const generation = {};
34
+ if (options.maxOutputTokens !== undefined)
35
+ generation.maxOutputTokens = options.maxOutputTokens;
36
+ if (options.temperature !== undefined)
37
+ generation.temperature = options.temperature;
38
+ if (options.topP !== undefined)
39
+ generation.topP = options.topP;
40
+ if (options.stop !== undefined)
41
+ generation.stop = options.stop;
42
+ if (options.seed !== undefined)
43
+ generation.seed = options.seed;
44
+ if (Object.keys(generation).length > 0)
45
+ request.generation = generation;
46
+ const execution = options.execution ?? {};
47
+ const constraints = {};
48
+ if (execution.privacy !== undefined)
49
+ constraints.privacy = execution.privacy;
50
+ if (execution.cloud !== undefined)
51
+ constraints.cloud = execution.cloud;
52
+ if (execution.timeoutMs !== undefined)
53
+ constraints.timeoutMs = execution.timeoutMs;
54
+ if (Object.keys(constraints).length > 0)
55
+ request.constraints = constraints;
56
+ if (execution.optimizeFor !== undefined) {
57
+ request.preferences = { optimizeFor: execution.optimizeFor };
58
+ }
59
+ if (options.authContextRef !== undefined)
60
+ request.authContextRef = options.authContextRef;
61
+ if (options.telemetry !== undefined)
62
+ request.telemetry = options.telemetry;
63
+ if (options.requestId !== undefined)
64
+ request.requestId = options.requestId;
65
+ return request;
66
+ }
67
+ /**
68
+ * Generates text in one call: `run()` over the canonical request. Not cancellable, for the same
69
+ * reason `run()` is not; use {@link streamText} when cancellation matters.
70
+ * @throws {IndeRunException} Exactly as `run()` does.
71
+ */
72
+ export async function generateText(inderun, options) {
73
+ const taskResult = await inderun.run(toTaskRequest(options));
74
+ return {
75
+ text: taskResult.output.text,
76
+ finishReason: taskResult.finishReason,
77
+ ...(taskResult.usage !== undefined ? { usage: taskResult.usage } : {}),
78
+ runId: taskResult.runId,
79
+ ...(taskResult.provenance !== undefined ? { provenance: taskResult.provenance } : {}),
80
+ taskResult
81
+ };
82
+ }
83
+ /**
84
+ * Streams text: `stream()` over the canonical request. The run starts immediately and is read
85
+ * once, into a replay buffer, so `textStream`, `fullStream`, and `result` can each be consumed
86
+ * in any order (or not at all) without stalling the others.
87
+ * @throws {IndeRunException} Exactly as `stream()` does: validation and routing refusals reject
88
+ * here, before there is a run.
89
+ */
90
+ export async function streamText(inderun, options) {
91
+ const run = await inderun.stream(toTaskRequest(options));
92
+ const runId = run.handle.runId;
93
+ const replay = new ReplayBuffer();
94
+ const result = (async () => {
95
+ // The terminal event is the last one a run delivers, so it settles everything; a source
96
+ // that stays open after it (a bridge subscription, say) must not hold the result back.
97
+ // Releasing the source is not awaited for the same reason.
98
+ const events = run.events[Symbol.asyncIterator]();
99
+ try {
100
+ for (let next = await events.next(); next.done !== true; next = await events.next()) {
101
+ replay.push(next.value);
102
+ if (next.value.type === "terminal") {
103
+ void events.return?.()?.catch(() => undefined);
104
+ break;
105
+ }
106
+ }
107
+ replay.close();
108
+ }
109
+ catch (error) {
110
+ replay.fail(error);
111
+ throw error;
112
+ }
113
+ const terminal = replay.items.find((event) => event.type === "terminal");
114
+ if (terminal === undefined) {
115
+ throw new IndeRunException({
116
+ errorClass: "Internal",
117
+ message: `Run ${runId} ended without a terminal event.`,
118
+ runId
119
+ });
120
+ }
121
+ return settle(terminal.payload);
122
+ })();
123
+ // Consumers may read only one view; an unread rejection must not surface as unhandled.
124
+ result.catch(() => undefined);
125
+ return {
126
+ runId,
127
+ fullStream: { [Symbol.asyncIterator]: () => replay.iterate() },
128
+ textStream: { [Symbol.asyncIterator]: () => textIncrements(replay.iterate(), runId) },
129
+ result,
130
+ cancel: (reason) => run.cancel(reason)
131
+ };
132
+ }
133
+ function settle(outcome) {
134
+ const provenance = outcome.provenance !== undefined ? { provenance: outcome.provenance } : {};
135
+ switch (outcome.outcome) {
136
+ case "completed":
137
+ return {
138
+ text: outcome.finalText ?? "",
139
+ ...(outcome.finishReason !== undefined ? { finishReason: outcome.finishReason } : {}),
140
+ ...(outcome.usage !== undefined ? { usage: outcome.usage } : {}),
141
+ runId: outcome.runId,
142
+ ...provenance,
143
+ outcome
144
+ };
145
+ case "cancelled":
146
+ return {
147
+ text: outcome.partialText ?? "",
148
+ finishReason: "cancelled",
149
+ runId: outcome.runId,
150
+ ...provenance,
151
+ outcome
152
+ };
153
+ case "error": {
154
+ const error = outcome.error;
155
+ throw new IndeRunException({
156
+ errorClass: error.errorClass,
157
+ message: error.message,
158
+ runId: outcome.runId,
159
+ ...(error.providerId !== undefined ? { providerId: error.providerId } : {}),
160
+ ...(error.retryable !== undefined ? { retryable: error.retryable } : {}),
161
+ ...(error.retryAfterMs !== undefined ? { retryAfterMs: error.retryAfterMs } : {}),
162
+ ...(error.details !== undefined ? { details: error.details } : {}),
163
+ ...(outcome.provenance !== undefined ? { provenance: outcome.provenance } : {})
164
+ });
165
+ }
166
+ }
167
+ }
168
+ async function* textIncrements(events, runId) {
169
+ let text = "";
170
+ for await (const event of events) {
171
+ if (event.type === "content_delta") {
172
+ const delta = event.payload.text;
173
+ text += delta;
174
+ yield delta;
175
+ }
176
+ else if (event.type === "content_snapshot") {
177
+ const snapshot = event.payload.text;
178
+ if (!snapshot.startsWith(text)) {
179
+ throw new TextStreamRetractedError(runId);
180
+ }
181
+ const suffix = snapshot.slice(text.length);
182
+ text = snapshot;
183
+ if (suffix.length > 0)
184
+ yield suffix;
185
+ }
186
+ else if (event.type === "terminal") {
187
+ settle(event.payload);
188
+ return;
189
+ }
190
+ }
191
+ }
192
+ /**
193
+ * Holds every item read from a single-use source so any number of readers can iterate it from
194
+ * the start, each at its own pace. Readers that catch up wait for the next item.
195
+ */
196
+ class ReplayBuffer {
197
+ items = [];
198
+ done = false;
199
+ failure;
200
+ waiters = [];
201
+ push(item) {
202
+ this.items.push(item);
203
+ this.wake();
204
+ }
205
+ close() {
206
+ this.done = true;
207
+ this.wake();
208
+ }
209
+ fail(error) {
210
+ this.failure = { error };
211
+ this.close();
212
+ }
213
+ async *iterate() {
214
+ let index = 0;
215
+ while (true) {
216
+ if (index < this.items.length) {
217
+ yield this.items[index++];
218
+ continue;
219
+ }
220
+ if (this.done) {
221
+ if (this.failure !== undefined)
222
+ throw this.failure.error;
223
+ return;
224
+ }
225
+ await new Promise((resolve) => this.waiters.push(resolve));
226
+ }
227
+ }
228
+ wake() {
229
+ const waiters = this.waiters;
230
+ this.waiters = [];
231
+ for (const wake of waiters)
232
+ wake();
233
+ }
234
+ }