@fgv/ts-extras 5.1.0-33 → 5.1.0-35

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 (108) hide show
  1. package/dist/packlets/ai-assist/apiClient.js +58 -112
  2. package/dist/packlets/ai-assist/apiClient.js.map +1 -1
  3. package/dist/packlets/ai-assist/chatRequestBuilders.js +131 -35
  4. package/dist/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  5. package/dist/packlets/ai-assist/converters.js +31 -1
  6. package/dist/packlets/ai-assist/converters.js.map +1 -1
  7. package/dist/packlets/ai-assist/embeddingClient.js +346 -0
  8. package/dist/packlets/ai-assist/embeddingClient.js.map +1 -0
  9. package/dist/packlets/ai-assist/http.js +75 -0
  10. package/dist/packlets/ai-assist/http.js.map +1 -0
  11. package/dist/packlets/ai-assist/index.js +6 -4
  12. package/dist/packlets/ai-assist/index.js.map +1 -1
  13. package/dist/packlets/ai-assist/jsonCompletion.js +6 -8
  14. package/dist/packlets/ai-assist/jsonCompletion.js.map +1 -1
  15. package/dist/packlets/ai-assist/model.js +36 -1
  16. package/dist/packlets/ai-assist/model.js.map +1 -1
  17. package/dist/packlets/ai-assist/registry.js +77 -7
  18. package/dist/packlets/ai-assist/registry.js.map +1 -1
  19. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js +176 -32
  20. package/dist/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  21. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +528 -0
  22. package/dist/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -0
  23. package/dist/packlets/ai-assist/streamingAdapters/common.js +95 -0
  24. package/dist/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  25. package/dist/packlets/ai-assist/streamingAdapters/gemini.js +34 -10
  26. package/dist/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  27. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js +215 -15
  28. package/dist/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  29. package/dist/packlets/ai-assist/streamingAdapters/proxy.js +15 -8
  30. package/dist/packlets/ai-assist/streamingAdapters/proxy.js.map +1 -1
  31. package/dist/packlets/ai-assist/streamingClient.js +29 -5
  32. package/dist/packlets/ai-assist/streamingClient.js.map +1 -1
  33. package/dist/packlets/ai-assist/thinkingOptionsResolver.js +23 -0
  34. package/dist/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  35. package/dist/packlets/ai-assist/toolFormats.js +106 -10
  36. package/dist/packlets/ai-assist/toolFormats.js.map +1 -1
  37. package/dist/ts-extras.d.ts +682 -48
  38. package/lib/packlets/ai-assist/apiClient.d.ts +24 -34
  39. package/lib/packlets/ai-assist/apiClient.d.ts.map +1 -1
  40. package/lib/packlets/ai-assist/apiClient.js +67 -121
  41. package/lib/packlets/ai-assist/apiClient.js.map +1 -1
  42. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts +82 -22
  43. package/lib/packlets/ai-assist/chatRequestBuilders.d.ts.map +1 -1
  44. package/lib/packlets/ai-assist/chatRequestBuilders.js +132 -34
  45. package/lib/packlets/ai-assist/chatRequestBuilders.js.map +1 -1
  46. package/lib/packlets/ai-assist/converters.d.ts +9 -1
  47. package/lib/packlets/ai-assist/converters.d.ts.map +1 -1
  48. package/lib/packlets/ai-assist/converters.js +31 -1
  49. package/lib/packlets/ai-assist/converters.js.map +1 -1
  50. package/lib/packlets/ai-assist/embeddingClient.d.ts +69 -0
  51. package/lib/packlets/ai-assist/embeddingClient.d.ts.map +1 -0
  52. package/lib/packlets/ai-assist/embeddingClient.js +350 -0
  53. package/lib/packlets/ai-assist/embeddingClient.js.map +1 -0
  54. package/lib/packlets/ai-assist/http.d.ts +24 -0
  55. package/lib/packlets/ai-assist/http.d.ts.map +1 -0
  56. package/lib/packlets/ai-assist/http.js +78 -0
  57. package/lib/packlets/ai-assist/http.js.map +1 -0
  58. package/lib/packlets/ai-assist/index.d.ts +6 -4
  59. package/lib/packlets/ai-assist/index.d.ts.map +1 -1
  60. package/lib/packlets/ai-assist/index.js +11 -1
  61. package/lib/packlets/ai-assist/index.js.map +1 -1
  62. package/lib/packlets/ai-assist/jsonCompletion.d.ts.map +1 -1
  63. package/lib/packlets/ai-assist/jsonCompletion.js +6 -8
  64. package/lib/packlets/ai-assist/jsonCompletion.js.map +1 -1
  65. package/lib/packlets/ai-assist/model.d.ts +377 -5
  66. package/lib/packlets/ai-assist/model.d.ts.map +1 -1
  67. package/lib/packlets/ai-assist/model.js +37 -2
  68. package/lib/packlets/ai-assist/model.js.map +1 -1
  69. package/lib/packlets/ai-assist/registry.d.ts +23 -1
  70. package/lib/packlets/ai-assist/registry.d.ts.map +1 -1
  71. package/lib/packlets/ai-assist/registry.js +79 -7
  72. package/lib/packlets/ai-assist/registry.js.map +1 -1
  73. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts +58 -5
  74. package/lib/packlets/ai-assist/streamingAdapters/anthropic.d.ts.map +1 -1
  75. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js +175 -31
  76. package/lib/packlets/ai-assist/streamingAdapters/anthropic.js.map +1 -1
  77. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts +172 -0
  78. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.d.ts.map +1 -0
  79. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js +534 -0
  80. package/lib/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.js.map +1 -0
  81. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts +59 -11
  82. package/lib/packlets/ai-assist/streamingAdapters/common.d.ts.map +1 -1
  83. package/lib/packlets/ai-assist/streamingAdapters/common.js +97 -0
  84. package/lib/packlets/ai-assist/streamingAdapters/common.js.map +1 -1
  85. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts +16 -2
  86. package/lib/packlets/ai-assist/streamingAdapters/gemini.d.ts.map +1 -1
  87. package/lib/packlets/ai-assist/streamingAdapters/gemini.js +34 -10
  88. package/lib/packlets/ai-assist/streamingAdapters/gemini.js.map +1 -1
  89. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts +15 -2
  90. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.d.ts.map +1 -1
  91. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js +214 -14
  92. package/lib/packlets/ai-assist/streamingAdapters/openaiResponses.js.map +1 -1
  93. package/lib/packlets/ai-assist/streamingAdapters/proxy.d.ts.map +1 -1
  94. package/lib/packlets/ai-assist/streamingAdapters/proxy.js +14 -7
  95. package/lib/packlets/ai-assist/streamingAdapters/proxy.js.map +1 -1
  96. package/lib/packlets/ai-assist/streamingClient.d.ts +17 -0
  97. package/lib/packlets/ai-assist/streamingClient.d.ts.map +1 -1
  98. package/lib/packlets/ai-assist/streamingClient.js +31 -6
  99. package/lib/packlets/ai-assist/streamingClient.js.map +1 -1
  100. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts +18 -2
  101. package/lib/packlets/ai-assist/thinkingOptionsResolver.d.ts.map +1 -1
  102. package/lib/packlets/ai-assist/thinkingOptionsResolver.js +24 -0
  103. package/lib/packlets/ai-assist/thinkingOptionsResolver.js.map +1 -1
  104. package/lib/packlets/ai-assist/toolFormats.d.ts +40 -9
  105. package/lib/packlets/ai-assist/toolFormats.d.ts.map +1 -1
  106. package/lib/packlets/ai-assist/toolFormats.js +107 -10
  107. package/lib/packlets/ai-assist/toolFormats.js.map +1 -1
  108. package/package.json +7 -7
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Per-provider continuation message builders and the `executeClientToolTurn`
3
+ * helper for orchestrating a single client-tool round-trip.
4
+ *
5
+ * Each provider requires a different wire format for the follow-up request:
6
+ * - **Anthropic**: assistant turn reconstructed from the ordered accumulation
7
+ * buffer (thinking / redacted_thinking / text / tool_use in original stream
8
+ * order) + user turn with `tool_result` blocks. When thinking is active, the
9
+ * follow-up request must NOT set `tool_choice: { type: 'any' }` or
10
+ * `tool_choice: { type: 'tool', ... }` (E3 / §5.4 of b4-spike-findings).
11
+ * - **OpenAI / xAI Responses API**: `function_call` input items +
12
+ * `function_call_output` input items.
13
+ * - **Gemini**: model turn with `functionCall` parts + user turn with
14
+ * `functionResponse` parts (correlation by tool name).
15
+ *
16
+ * @packageDocumentation
17
+ */
18
+ import { type Logging, Result } from '@fgv/ts-utils';
19
+ import { type JsonObject } from '@fgv/ts-json-base';
20
+ import { type AiServerToolConfig, type IAiClientTool, type IAiClientToolContinuation, type IAiClientToolTurnResult, type IAiStreamEvent, type IChatRequest, type IAiProviderDescriptor } from '../model';
21
+ import { type IResolvedThinkingConfig } from '../thinkingOptionsResolver';
22
+ import { type IAccumulatedBlock } from './anthropic';
23
+ import { type IAccumulatedFunctionCall } from './openaiResponses';
24
+ import { type IAccumulatedGeminiFunctionCall } from './gemini';
25
+ /**
26
+ * Accumulated result for a single tool call, collected during stream iteration.
27
+ * @internal
28
+ */
29
+ interface IToolCallResult {
30
+ readonly toolName: string;
31
+ readonly callId?: string;
32
+ readonly args: JsonObject;
33
+ readonly result: string;
34
+ readonly isError: boolean;
35
+ }
36
+ /**
37
+ * Builds the Anthropic follow-up messages for a client-tool round-trip.
38
+ *
39
+ * Reconstructs the assistant turn from the ordered accumulation buffer
40
+ * (all block types in original stream order) and appends a user turn
41
+ * with `tool_result` blocks for each executed tool call.
42
+ *
43
+ * **Constraint (E3):** The returned continuation does NOT include a forced
44
+ * `tool_choice` field. When thinking is active, Anthropic rejects
45
+ * `tool_choice: { type: 'any' }` and `tool_choice: { type: 'tool', ... }`
46
+ * with an HTTP 400 error. Only `tool_choice: { type: 'auto' }` (the default,
47
+ * i.e. omitted) is compatible with extended thinking.
48
+ *
49
+ * @internal
50
+ */
51
+ export declare function buildAnthropicContinuation(accBuffer: Map<number, IAccumulatedBlock>, toolResults: IToolCallResult[]): IAiClientToolContinuation;
52
+ /**
53
+ * Builds the OpenAI / xAI Responses API follow-up input items for a
54
+ * client-tool round-trip.
55
+ *
56
+ * Emits `function_call` items (the model's call) followed by
57
+ * `function_call_output` items (the harness execution result), one pair
58
+ * per executed tool call.
59
+ *
60
+ * @internal
61
+ */
62
+ export declare function buildOpenAiContinuation(calls: Map<string, IAccumulatedFunctionCall>, toolResults: IToolCallResult[]): IAiClientToolContinuation;
63
+ /**
64
+ * Builds the Gemini follow-up contents for a client-tool round-trip.
65
+ *
66
+ * Emits a model turn with `functionCall` parts (one per tool call) and a
67
+ * user turn with `functionResponse` parts (correlation by tool name, since
68
+ * Gemini does not assign call IDs).
69
+ *
70
+ * @internal
71
+ */
72
+ export declare function buildGeminiContinuation(calls: IAccumulatedGeminiFunctionCall[], toolResults: IToolCallResult[]): IAiClientToolContinuation;
73
+ /**
74
+ * Parameters for {@link AiAssist.executeClientToolTurn}.
75
+ *
76
+ * @remarks
77
+ * Carries the unified {@link AiAssist.IChatRequest} shape (`system?` + ordered
78
+ * `messages`): the last message is the current user turn and the preceding
79
+ * messages are history, linearized before the current turn — identically to the
80
+ * completion and streaming paths. {@link IExecuteClientToolTurnParams.continuationMessages}
81
+ * remains a distinct post-current-turn axis (see below).
82
+ *
83
+ * @public
84
+ */
85
+ export interface IExecuteClientToolTurnParams extends IChatRequest {
86
+ /** The provider descriptor for routing (Anthropic / OpenAI / Gemini). */
87
+ readonly descriptor: IAiProviderDescriptor;
88
+ /** API key for authentication. */
89
+ readonly apiKey: string;
90
+ /**
91
+ * Provider-specific continuation messages to append after the current user
92
+ * message. Used to supply the output of {@link AiAssist.IAiClientToolContinuation}'s
93
+ * `messages` field from a prior turn back to the provider in the follow-up request.
94
+ *
95
+ * Each provider applies its own shape guard to the supplied wire objects:
96
+ * - Anthropic: projects each entry to `{ role, content }` (sufficient for
97
+ * thinking blocks and `tool_result` arrays).
98
+ * - OpenAI / xAI Responses: passes each item verbatim (`function_call` /
99
+ * `function_call_output` items carry distinct fields per `type`); only guards
100
+ * that each entry is a JSON object.
101
+ * - Gemini: projects each entry to `{ role, parts }`.
102
+ *
103
+ * Entries that fail their provider's shape check are silently skipped.
104
+ */
105
+ readonly continuationMessages?: ReadonlyArray<JsonObject>;
106
+ /** Temperature (default: 0.7). */
107
+ readonly temperature?: number;
108
+ /** Server-side tools to include. */
109
+ readonly tools?: ReadonlyArray<AiServerToolConfig>;
110
+ /** Client-defined tools available for the model to call. */
111
+ readonly clientTools: ReadonlyArray<IAiClientTool>;
112
+ /** Optional abort signal. */
113
+ readonly signal?: AbortSignal;
114
+ /**
115
+ * Optional override of the descriptor's default base URL. Same semantics as
116
+ * the non-streaming completion path and `callProviderCompletionStream`: a
117
+ * well-formed `http`/`https` URL is substituted for `descriptor.baseUrl`
118
+ * when composing the per-format request, with the per-format suffix appended
119
+ * unchanged. Validated at the dispatcher; auth shape is unaffected. Use this
120
+ * to point a client-tool turn at a local / LAN OpenAI-compatible server
121
+ * (Ollama, LM Studio, llama.cpp).
122
+ */
123
+ readonly endpoint?: string;
124
+ /** Optional logger for diagnostics. */
125
+ readonly logger?: Logging.ILogger;
126
+ /** Optional resolved thinking config (pre-resolved by the caller). */
127
+ readonly resolvedThinking?: IResolvedThinkingConfig;
128
+ /** Resolved model string (pre-resolved by the caller). When omitted, uses the descriptor's default model. */
129
+ readonly model?: string;
130
+ }
131
+ /**
132
+ * Return value of {@link AiAssist.executeClientToolTurn}.
133
+ * @public
134
+ */
135
+ export interface IExecuteClientToolTurnResult {
136
+ /**
137
+ * The unified-event iterable. Callers iterate this to drive the streaming UI.
138
+ * The iterable forwards `text-delta`, `tool-event`, `client-tool-call-start`,
139
+ * `client-tool-call-done`, and `client-tool-result` events through.
140
+ */
141
+ readonly events: AsyncIterable<IAiStreamEvent>;
142
+ /**
143
+ * Resolves when the stream terminates. On success, carries the
144
+ * {@link AiAssist.IAiClientToolTurnResult} with the optional continuation for the
145
+ * next round. On failure, carries the error message.
146
+ */
147
+ readonly nextTurn: Promise<Result<IAiClientToolTurnResult>>;
148
+ }
149
+ /**
150
+ * Orchestrates a single client-tool streaming turn for any supported provider.
151
+ *
152
+ * Starts a streaming request, iterates the underlying provider stream, and:
153
+ * - Forwards `text-delta`, `tool-event`, `client-tool-call-start`, and
154
+ * `client-tool-call-done` events through to the consumer.
155
+ * - For each `client-tool-call-done` event: validates the raw args against the
156
+ * tool's `parametersSchema`, invokes `execute(typedArgs)`, and emits a
157
+ * `client-tool-result` event.
158
+ * - After stream completion: builds the per-provider continuation (or
159
+ * `{ continuation: undefined }` when no tool calls occurred) and resolves
160
+ * `nextTurn`.
161
+ *
162
+ * **Anthropic constraint (E3):** The continuation for Anthropic does not set
163
+ * a forced `tool_choice`. Only `tool_choice: 'auto'` (the default, i.e.
164
+ * omitted) is compatible with extended thinking.
165
+ *
166
+ * @param params - Turn parameters
167
+ * @returns `{ events, nextTurn }` — stream iterable + completion promise
168
+ * @public
169
+ */
170
+ export declare function executeClientToolTurn(params: IExecuteClientToolTurnParams): Result<IExecuteClientToolTurnResult>;
171
+ export {};
172
+ //# sourceMappingURL=clientToolContinuationBuilder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clientToolContinuationBuilder.d.ts","sourceRoot":"","sources":["../../../../src/packlets/ai-assist/streamingAdapters/clientToolContinuationBuilder.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAA4B,KAAK,OAAO,EAAE,MAAM,EAAW,MAAM,eAAe,CAAC;AACxF,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpE,OAAO,EACL,KAAK,kBAAkB,EAEvB,KAAK,aAAa,EAClB,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAC5B,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,qBAAqB,EAE3B,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAG1E,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,aAAa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