@nimiplatform/sdk 0.11.0 → 0.12.0
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/dist/core/ai/index.d.ts +1 -0
- package/dist/core/ai/index.d.ts.map +1 -1
- package/dist/core/ai/index.js +1 -0
- package/dist/core/ai/index.js.map +1 -1
- package/dist/core/ai/local-app-model.d.ts +8 -0
- package/dist/core/ai/local-app-model.d.ts.map +1 -0
- package/dist/core/ai/local-app-model.js +117 -0
- package/dist/core/ai/local-app-model.js.map +1 -0
- package/dist/core/app/index.d.ts +7 -0
- package/dist/core/app/index.d.ts.map +1 -1
- package/dist/core/app/index.js +5 -0
- package/dist/core/app/index.js.map +1 -1
- package/dist/core/app/local-app-runtime-platform-ai.d.ts +28 -3
- package/dist/core/app/local-app-runtime-platform-ai.d.ts.map +1 -1
- package/dist/core/app/local-app-runtime-platform-ai.js +112 -74
- package/dist/core/app/local-app-runtime-platform-ai.js.map +1 -1
- package/dist/core/app/local-app-runtime-platform-direct-realtime-shared.d.ts +1 -1
- package/dist/core/app/local-app-runtime-platform-validation.d.ts +1 -1
- package/dist/core/app/local-app-runtime-platform-validation.d.ts.map +1 -1
- package/dist/core/app/local-app-runtime-platform-validation.js +4 -4
- package/dist/core/app/local-app-runtime-platform-validation.js.map +1 -1
- package/dist/core/app/local-app-runtime-platform-world-creator.d.ts +50 -0
- package/dist/core/app/local-app-runtime-platform-world-creator.d.ts.map +1 -0
- package/dist/core/app/local-app-runtime-platform-world-creator.js +246 -0
- package/dist/core/app/local-app-runtime-platform-world-creator.js.map +1 -0
- package/dist/core/app/local-app-runtime-platform.d.ts +4 -9
- package/dist/core/app/local-app-runtime-platform.d.ts.map +1 -1
- package/dist/core/app/local-app-runtime-platform.js +4 -2
- package/dist/core/app/local-app-runtime-platform.js.map +1 -1
- package/dist/core/app/local-app-text.d.ts +51 -0
- package/dist/core/app/local-app-text.d.ts.map +1 -0
- package/dist/core/app/local-app-text.js +228 -0
- package/dist/core/app/local-app-text.js.map +1 -0
- package/dist/core-generated/realm-client.js +6 -6
- package/dist/core-generated/realm-client.js.map +1 -1
- package/dist/core-generated/realm-typed-client.d.ts +12 -12
- package/dist/core-generated/realm-typed-client.d.ts.map +1 -1
- package/dist/core-generated/realm-typed-client.js +13 -13
- package/dist/core-generated/realm-typed-client.js.map +1 -1
- package/dist/core-generated/runtime-client.d.ts.map +1 -1
- package/dist/core-generated/runtime-client.js +40 -0
- package/dist/core-generated/runtime-client.js.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/ai.d.ts +140 -3
- package/dist/core-generated/runtime-protobuf/runtime/v1/ai.d.ts.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/ai.js +273 -7
- package/dist/core-generated/runtime-protobuf/runtime/v1/ai.js.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/app.d.ts +394 -0
- package/dist/core-generated/runtime-protobuf/runtime/v1/app.d.ts.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/app.js +877 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/app.js.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/common.d.ts +4 -0
- package/dist/core-generated/runtime-protobuf/runtime/v1/common.d.ts.map +1 -1
- package/dist/core-generated/runtime-protobuf/runtime/v1/common.js +4 -0
- package/dist/core-generated/runtime-protobuf/runtime/v1/common.js.map +1 -1
- package/dist/core-generated/runtime-public-client.d.ts.map +1 -1
- package/dist/core-generated/runtime-public-client.js +40 -0
- package/dist/core-generated/runtime-public-client.js.map +1 -1
- package/dist/core-generated/runtime-public-rpc-auth-posture.d.ts.map +1 -1
- package/dist/core-generated/runtime-public-rpc-auth-posture.js +5 -0
- package/dist/core-generated/runtime-public-rpc-auth-posture.js.map +1 -1
- package/dist/core-generated/runtime-public-wire-codecs.d.ts +6 -1
- package/dist/core-generated/runtime-public-wire-codecs.d.ts.map +1 -1
- package/dist/core-generated/runtime-public-wire-codecs.js +6 -1
- package/dist/core-generated/runtime-public-wire-codecs.js.map +1 -1
- package/dist/core-generated/runtime-rpc-auth-posture.d.ts.map +1 -1
- package/dist/core-generated/runtime-rpc-auth-posture.js +5 -0
- package/dist/core-generated/runtime-rpc-auth-posture.js.map +1 -1
- package/dist/core-generated/runtime-typed-client.d.ts +8 -3
- package/dist/core-generated/runtime-typed-client.d.ts.map +1 -1
- package/dist/core-generated/runtime-typed-client.js +50 -0
- package/dist/core-generated/runtime-typed-client.js.map +1 -1
- package/dist/core-generated/runtime-wire-codecs.d.ts +6 -1
- package/dist/core-generated/runtime-wire-codecs.d.ts.map +1 -1
- package/dist/core-generated/runtime-wire-codecs.js +6 -1
- package/dist/core-generated/runtime-wire-codecs.js.map +1 -1
- package/dist/realm/index.d.ts +1 -1
- package/dist/realm/index.d.ts.map +1 -1
- package/dist/realm/index.js +1 -1
- package/dist/realm/index.js.map +1 -1
- package/dist/runtime/desktop-first-party-runtime.d.ts +1 -1
- package/dist/runtime/desktop-first-party-runtime.d.ts.map +1 -1
- package/dist/runtime/desktop-first-party-runtime.js +5 -0
- package/dist/runtime/desktop-first-party-runtime.js.map +1 -1
- package/dist/runtime/first-party-protected-runtime-profiles.generated.d.ts +21 -1
- package/dist/runtime/first-party-protected-runtime-profiles.generated.d.ts.map +1 -1
- package/dist/runtime/first-party-protected-runtime-profiles.generated.js +6 -1
- package/dist/runtime/first-party-protected-runtime-profiles.generated.js.map +1 -1
- package/dist/runtime/wire-types/ai-scenario-types.d.ts +2 -2
- package/dist/runtime/wire-types/ai-scenario-types.d.ts.map +1 -1
- package/dist/runtime/wire-types/identity-app-types.d.ts +2 -2
- package/dist/runtime/wire-types/identity-app-types.d.ts.map +1 -1
- package/dist/runtime/wire-types/reason-code-enums.d.ts +1 -0
- package/dist/runtime/wire-types/reason-code-enums.d.ts.map +1 -1
- package/dist/runtime/wire-types/reason-code-enums.js +1 -0
- package/dist/runtime/wire-types/reason-code-enums.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { ExecutionMode, FaceSwapNoFacePolicy, FinishReason, ReasonCode as RuntimeReasonCode, RoutePolicy, ScenarioJobEventType, ScenarioJobStatus, ScenarioType, SpeechTimingMode, VideoContentRole, VideoContentType, VideoMode, VoiceAssetStatus, VoiceCreationSource, VoiceReferenceKind, VisionLocateGeometry, } from '../../core-generated/runtime-typed-client.js';
|
|
1
|
+
import { ExecutionMode, FaceSwapNoFacePolicy, FinishReason, ResponseFormatKind, ToolChoiceMode, ReasonCode as RuntimeReasonCode, RoutePolicy, ScenarioJobEventType, ScenarioJobStatus, ScenarioType, SpeechTimingMode, VideoContentRole, VideoContentType, VideoMode, VoiceAssetStatus, VoiceCreationSource, VoiceReferenceKind, VisionLocateGeometry, } from '../../core-generated/runtime-typed-client.js';
|
|
2
|
+
import { toRuntimeMessages, toRuntimeTools, toRuntimeStruct, toNimiToolCall, toNimiTextOutputItems } from '../ai/runtime-model-text-projection.js';
|
|
3
|
+
import { validateLocalAppTextInput, projectLocalAppToolCall, projectLocalAppTextItems, projectLocalAppContinuity, modelTextOutputToLocalApp, localAppTextOutputToModel, } from './local-app-text.js';
|
|
2
4
|
import { asRecord, assertExactKeys, assertExactProjectionKeys, assertNoAuthorityMaterial, assertSafeProjection, localAppError, localAppProjectionError, projectionText, requireText, } from './local-app-runtime-platform-validation.js';
|
|
3
5
|
import { projectVisionLocateResult, localVisionLocateFromRuntime, runtimeVisionLocateFromLocal, localLocateGeometry, projectLocalExecutionInterruption, localInterruptionFromRuntime, runtimeInterruptionFromLocal, } from './local-app-runtime-platform-vision.js';
|
|
4
6
|
const MAX_RESULT_BYTES = 256 * 1024;
|
|
@@ -11,15 +13,43 @@ export function createNimiLocalAppAIConsumptionClient(shell) {
|
|
|
11
13
|
const client = {
|
|
12
14
|
text: Object.freeze({
|
|
13
15
|
streamTurn: async (input) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
const stream = projectSubscription(await shell.text.streamTurn(validateLocalAppTextInput(input)), projectTextTurnEvent);
|
|
17
|
+
let canceled = false;
|
|
18
|
+
let closing;
|
|
19
|
+
const cancel = () => { canceled = true; return closing ??= stream.cancel(); };
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
async *[Symbol.asyncIterator]() {
|
|
22
|
+
let sequence = 0n;
|
|
23
|
+
let terminal = false;
|
|
24
|
+
let hasPrimary = false;
|
|
25
|
+
const toolIds = new Set();
|
|
26
|
+
try {
|
|
27
|
+
for await (const event of stream) {
|
|
28
|
+
if (canceled)
|
|
29
|
+
break;
|
|
30
|
+
if (terminal || BigInt(event.sequence) !== ++sequence)
|
|
31
|
+
localAppProjectionError('text-turn event sequence');
|
|
32
|
+
if (event.type === 'delta')
|
|
33
|
+
hasPrimary = true;
|
|
34
|
+
if (event.type === 'tool-call') {
|
|
35
|
+
hasPrimary = true;
|
|
36
|
+
if (toolIds.has(event.toolCall.id))
|
|
37
|
+
localAppProjectionError('duplicate text tool call');
|
|
38
|
+
toolIds.add(event.toolCall.id);
|
|
39
|
+
}
|
|
40
|
+
if (event.type === 'completed' && !hasPrimary)
|
|
41
|
+
localAppProjectionError('missing primary text output');
|
|
42
|
+
terminal = event.type === 'completed' || event.type === 'failed';
|
|
43
|
+
yield event;
|
|
44
|
+
}
|
|
45
|
+
if (!terminal && !canceled)
|
|
46
|
+
localAppProjectionError('text-turn missing terminal event');
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await cancel();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
cancel,
|
|
23
53
|
});
|
|
24
54
|
},
|
|
25
55
|
}),
|
|
@@ -236,63 +266,16 @@ export function createNimiLocalAppRuntimeScenarioJobClient(ai) {
|
|
|
236
266
|
};
|
|
237
267
|
return Object.freeze(client);
|
|
238
268
|
}
|
|
239
|
-
function validateTextTurnInput(input) {
|
|
240
|
-
assertExactKeys(input, [
|
|
241
|
-
'messages', 'temperature', 'topP', 'maxTokens', 'topK',
|
|
242
|
-
'presencePenalty', 'frequencyPenalty', 'stop', 'seed',
|
|
243
|
-
], 'text-turn input');
|
|
244
|
-
assertNoAuthorityMaterial(input);
|
|
245
|
-
if (!Array.isArray(input.messages) || input.messages.length === 0 || input.messages.length > 8) {
|
|
246
|
-
invalidAIInput('text-turn messages are invalid');
|
|
247
|
-
}
|
|
248
|
-
let totalBytes = 0;
|
|
249
|
-
let sawSystem = false;
|
|
250
|
-
let sawUser = false;
|
|
251
|
-
const messages = input.messages.map((message, index) => {
|
|
252
|
-
assertExactKeys(message, ['role', 'text'], `text-turn message ${index}`);
|
|
253
|
-
if (message.role === 'system') {
|
|
254
|
-
if (sawSystem || sawUser)
|
|
255
|
-
invalidAIInput('text-turn system message order is invalid');
|
|
256
|
-
sawSystem = true;
|
|
257
|
-
}
|
|
258
|
-
else if (message.role === 'user')
|
|
259
|
-
sawUser = true;
|
|
260
|
-
else
|
|
261
|
-
invalidAIInput(`text-turn message ${index} role is invalid`);
|
|
262
|
-
const text = boundedContent(message.text, `text-turn message ${index}`, 32 * 1024);
|
|
263
|
-
totalBytes += utf8Length(message.role) + utf8Length(text);
|
|
264
|
-
if (totalBytes > 64 * 1024)
|
|
265
|
-
invalidAIInput('text-turn prompt is too large');
|
|
266
|
-
return Object.freeze({ role: message.role, text });
|
|
267
|
-
});
|
|
268
|
-
if (!sawUser)
|
|
269
|
-
invalidAIInput('text-turn requires a user message');
|
|
270
|
-
optionalBoundedNumber(input.temperature, 'text-turn temperature', 0, 2);
|
|
271
|
-
optionalBoundedNumber(input.topP, 'text-turn topP', 0, 1);
|
|
272
|
-
optionalBoundedInteger(input.maxTokens, 'text-turn maxTokens', 0, 4096);
|
|
273
|
-
optionalBoundedInteger(input.topK, 'text-turn topK', 0, Number.MAX_SAFE_INTEGER);
|
|
274
|
-
optionalBoundedNumber(input.presencePenalty, 'text-turn presencePenalty', -2, 2);
|
|
275
|
-
optionalBoundedNumber(input.frequencyPenalty, 'text-turn frequencyPenalty', -2, 2);
|
|
276
|
-
optionalBoundedInteger(input.seed, 'text-turn seed', Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
277
|
-
if (input.stop !== undefined && (!Array.isArray(input.stop)
|
|
278
|
-
|| input.stop.some((entry) => typeof entry !== 'string' || !entry.trim())))
|
|
279
|
-
invalidAIInput('text-turn stop is invalid');
|
|
280
|
-
return Object.freeze({
|
|
281
|
-
messages: Object.freeze(messages),
|
|
282
|
-
...(input.temperature !== undefined ? { temperature: input.temperature } : {}),
|
|
283
|
-
...(input.topP !== undefined ? { topP: input.topP } : {}),
|
|
284
|
-
...(input.maxTokens !== undefined ? { maxTokens: input.maxTokens } : {}),
|
|
285
|
-
...(input.topK !== undefined ? { topK: input.topK } : {}),
|
|
286
|
-
...(input.presencePenalty !== undefined ? { presencePenalty: input.presencePenalty } : {}),
|
|
287
|
-
...(input.frequencyPenalty !== undefined ? { frequencyPenalty: input.frequencyPenalty } : {}),
|
|
288
|
-
...(input.stop !== undefined ? { stop: Object.freeze([...input.stop]) } : {}),
|
|
289
|
-
...(input.seed !== undefined ? { seed: input.seed } : {}),
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
269
|
function validateScenarioSpec(spec, execute) {
|
|
293
270
|
const record = asRecord(spec);
|
|
294
271
|
if (!record || typeof record.type !== 'string')
|
|
295
272
|
invalidAIInput('scenario spec is invalid');
|
|
273
|
+
if (record.type === 'text-generate') {
|
|
274
|
+
if (!execute)
|
|
275
|
+
invalidAIInput('text-generate is a single synchronous model step');
|
|
276
|
+
const { type: _type, ...input } = record;
|
|
277
|
+
return Object.freeze({ type: 'text-generate', ...validateLocalAppTextInput(input) });
|
|
278
|
+
}
|
|
296
279
|
assertNoAuthorityMaterial(record);
|
|
297
280
|
switch (record.type) {
|
|
298
281
|
// @nimi-authority: rule.nimi.runtime.ai-provider.face-swap-video-job
|
|
@@ -562,31 +545,49 @@ function projectTextTurnEvent(value) {
|
|
|
562
545
|
localAppProjectionError('text-turn event');
|
|
563
546
|
const base = { sequence: String(record.sequence), traceId: boundedProjectionText(record.traceId, 'text-turn traceId', 512) };
|
|
564
547
|
if (record.type === 'delta') {
|
|
565
|
-
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'text'], 'text-turn delta');
|
|
566
|
-
return Object.freeze({ ...base, type: 'delta', text: boundedProjectionContent(record.text, 'text-turn text', 64 * 1024) });
|
|
548
|
+
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'text', 'itemIndex'], 'text-turn delta');
|
|
549
|
+
return Object.freeze({ ...base, type: 'delta', text: boundedProjectionContent(record.text, 'text-turn text', 64 * 1024), itemIndex: projectionInteger(record.itemIndex, 'text item index', 0, 4_294_967_295) });
|
|
550
|
+
}
|
|
551
|
+
if (record.type === 'tool-call') {
|
|
552
|
+
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'itemIndex', 'toolCall'], 'text tool call');
|
|
553
|
+
return Object.freeze({ ...base, type: 'tool-call', itemIndex: projectionInteger(record.itemIndex, 'tool item index', 0, 4_294_967_295), toolCall: projectLocalAppToolCall(record.toolCall) });
|
|
554
|
+
}
|
|
555
|
+
if (record.type === 'reasoning-continuity') {
|
|
556
|
+
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'itemIndex', 'carrier'], 'text continuity');
|
|
557
|
+
return Object.freeze({ ...base, type: 'reasoning-continuity', itemIndex: projectionInteger(record.itemIndex, 'continuity item index', 0, 4_294_967_295), carrier: projectLocalAppContinuity(record.carrier) });
|
|
567
558
|
}
|
|
568
559
|
if (record.type === 'completed') {
|
|
569
560
|
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'finishReason'], 'text-turn completed');
|
|
570
|
-
if (!['stop', 'length', 'content-filter'].includes(String(record.finishReason)))
|
|
561
|
+
if (!['stop', 'length', 'tool-calls', 'content-filter'].includes(String(record.finishReason)))
|
|
571
562
|
localAppProjectionError('text-turn finishReason');
|
|
572
563
|
return Object.freeze({ ...base, type: 'completed', finishReason: record.finishReason });
|
|
573
564
|
}
|
|
574
565
|
if (record.type === 'failed') {
|
|
575
|
-
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'reasonCode', 'actionHint'], 'text-turn failed');
|
|
576
|
-
|
|
566
|
+
assertExactProjectionKeys(record, ['type', 'sequence', 'traceId', 'reasonCode', 'actionHint', ...(Object.hasOwn(record, 'interruption') ? ['interruption'] : [])], 'text-turn failed');
|
|
567
|
+
const interruption = projectLocalExecutionInterruption(record.interruption ?? undefined, record.reasonCode, 'failed');
|
|
568
|
+
return Object.freeze({ ...base, type: 'failed', reasonCode: boundedProjectionText(record.reasonCode, 'text-turn reasonCode', 128), actionHint: optionalProjectionText(record.actionHint, 'text-turn actionHint', 512), ...(interruption ? { interruption } : {}) });
|
|
577
569
|
}
|
|
578
570
|
return localAppProjectionError('text-turn event type');
|
|
579
571
|
}
|
|
580
572
|
function projectScenarioExecute(value) {
|
|
581
573
|
const record = asRecord(value);
|
|
582
574
|
assertExactProjectionKeys(record, ['output', 'traceId'], 'scenario execute');
|
|
583
|
-
assertSafeProjection(record);
|
|
584
575
|
const output = asRecord(record.output);
|
|
585
576
|
if (!output)
|
|
586
577
|
localAppProjectionError('scenario execute output');
|
|
587
578
|
const traceId = boundedProjectionText(record.traceId, 'scenario execute traceId', 512);
|
|
579
|
+
if (output.type === 'text-generate') {
|
|
580
|
+
assertExactProjectionKeys(output, ['type', 'items', 'finishReason'], 'text generate output');
|
|
581
|
+
const items = projectLocalAppTextItems(output.items);
|
|
582
|
+
if (!['stop', 'length', 'tool-calls', 'content-filter'].includes(String(output.finishReason))
|
|
583
|
+
|| (output.finishReason === 'tool-calls' && !items.some((item) => item.type === 'tool-call')))
|
|
584
|
+
localAppProjectionError('text finish reason');
|
|
585
|
+
return Object.freeze({ output: Object.freeze({ type: 'text-generate', items, finishReason: output.finishReason }), traceId });
|
|
586
|
+
}
|
|
587
|
+
assertSafeProjection(record);
|
|
588
588
|
if (output.type === 'text-embed') {
|
|
589
|
-
assertExactProjectionKeys(output, ['type', 'vectors'], 'text embed output');
|
|
589
|
+
assertExactProjectionKeys(output, ['type', 'vectors', 'spaceId'], 'text embed output');
|
|
590
|
+
const spaceId = boundedProjectionText(output.spaceId, 'embedding spaceId', 128);
|
|
590
591
|
if (!Array.isArray(output.vectors) || output.vectors.length === 0 || output.vectors.length > 16)
|
|
591
592
|
localAppProjectionError('text embed vectors');
|
|
592
593
|
const vectors = output.vectors.map((vector) => {
|
|
@@ -595,7 +596,7 @@ function projectScenarioExecute(value) {
|
|
|
595
596
|
localAppProjectionError('text embed vector');
|
|
596
597
|
return Object.freeze([...vector]);
|
|
597
598
|
});
|
|
598
|
-
return Object.freeze({ output: Object.freeze({ type: 'text-embed', vectors: Object.freeze(vectors) }), traceId });
|
|
599
|
+
return Object.freeze({ output: Object.freeze({ type: 'text-embed', vectors: Object.freeze(vectors), spaceId }), traceId });
|
|
599
600
|
}
|
|
600
601
|
if (output.type === 'image-generate') {
|
|
601
602
|
assertExactProjectionKeys(output, ['type', 'artifacts'], 'image execute output');
|
|
@@ -727,7 +728,7 @@ function projectArtifactUpload(value, expectedSize, expectedMimeType) {
|
|
|
727
728
|
return Object.freeze({ artifactId, sizeBytes: record.sizeBytes, mimeType: record.mimeType });
|
|
728
729
|
}
|
|
729
730
|
function isArtifactUploadMime(value) {
|
|
730
|
-
return value === 'image/png' || value === 'image/jpeg' || value === 'image/webp' || value === 'image/gif' || value === 'video/mp4';
|
|
731
|
+
return value === 'image/png' || value === 'image/jpeg' || value === 'image/webp' || value === 'image/gif' || value === 'audio/wav' || value === 'audio/mpeg' || value === 'video/mp4';
|
|
731
732
|
}
|
|
732
733
|
function projectArtifactRead(value) {
|
|
733
734
|
const record = asRecord(value);
|
|
@@ -795,8 +796,26 @@ function projectRuntimeLocalAppVoiceAsset(asset) {
|
|
|
795
796
|
};
|
|
796
797
|
}
|
|
797
798
|
function runtimeTextTurnRequest(input) {
|
|
799
|
+
const messages = toRuntimeMessages(input.messages.map((message) => ({
|
|
800
|
+
role: message.role,
|
|
801
|
+
content: message.turnItems?.length || message.role === 'assistant' ? [] : [{ type: 'text', text: message.text }],
|
|
802
|
+
turnItems: message.turnItems?.length ? message.turnItems.map((item) => item.type === 'output'
|
|
803
|
+
? { ...item, output: localAppTextOutputToModel(item.output) } : item) : message.role === 'assistant'
|
|
804
|
+
? [{ type: 'output', output: { type: 'text', text: message.text } }] : undefined,
|
|
805
|
+
})));
|
|
806
|
+
const choice = input.toolChoice;
|
|
807
|
+
const format = input.responseFormat;
|
|
798
808
|
return {
|
|
799
|
-
messages:
|
|
809
|
+
messages: messages.map((message) => ({ role: message.role, text: message.content, turnItems: message.turnItems })),
|
|
810
|
+
tools: toRuntimeTools(input.tools),
|
|
811
|
+
toolChoice: choice === 'none' ? ToolChoiceMode.NONE : choice === 'auto' ? ToolChoiceMode.AUTO
|
|
812
|
+
: choice === 'required' ? ToolChoiceMode.REQUIRED : choice ? ToolChoiceMode.TOOL : ToolChoiceMode.UNSPECIFIED,
|
|
813
|
+
toolChoiceName: typeof choice === 'object' ? choice.name : '',
|
|
814
|
+
responseFormat: format ? {
|
|
815
|
+
kind: format.type === 'json-schema' ? ResponseFormatKind.JSON_SCHEMA : format.type === 'json-object' ? ResponseFormatKind.JSON_OBJECT : ResponseFormatKind.TEXT,
|
|
816
|
+
jsonSchema: format.schema ? toRuntimeStruct(format.schema) : undefined,
|
|
817
|
+
schemaName: format.name ?? '', schemaDescription: format.description ?? '', strict: format.strict ?? false,
|
|
818
|
+
} : undefined,
|
|
800
819
|
...(input.temperature === undefined ? {} : { temperature: input.temperature }),
|
|
801
820
|
...(input.topP === undefined ? {} : { topP: input.topP }),
|
|
802
821
|
...(input.maxTokens === undefined ? {} : { maxTokens: input.maxTokens }),
|
|
@@ -808,6 +827,8 @@ function runtimeTextTurnRequest(input) {
|
|
|
808
827
|
};
|
|
809
828
|
}
|
|
810
829
|
function runtimeExecuteRequest(spec) {
|
|
830
|
+
if (spec.type === 'text-generate')
|
|
831
|
+
return { spec: { oneofKind: 'textGenerate', textGenerate: runtimeTextTurnRequest(spec) } };
|
|
811
832
|
if (spec.type === 'text-embed') {
|
|
812
833
|
return { spec: { oneofKind: 'textEmbed', textEmbed: { inputs: [...spec.inputs] } } };
|
|
813
834
|
}
|
|
@@ -995,8 +1016,19 @@ function runtimeVoiceReference(value) {
|
|
|
995
1016
|
function projectRuntimeTextTurnEvent(event) {
|
|
996
1017
|
const base = { sequence: event.sequence, traceId: event.traceId };
|
|
997
1018
|
switch (event.payload.oneofKind) {
|
|
1019
|
+
case 'reasoningContinuity': {
|
|
1020
|
+
const carrier = event.payload.reasoningContinuity.carrier;
|
|
1021
|
+
if (!carrier)
|
|
1022
|
+
return localAppProjectionError('Runtime continuity carrier');
|
|
1023
|
+
return { ...base, type: 'reasoning-continuity', itemIndex: event.payload.reasoningContinuity.itemIndex,
|
|
1024
|
+
carrier: { ...carrier, payload: Array.from(carrier.payload) } };
|
|
1025
|
+
}
|
|
1026
|
+
case 'toolCall':
|
|
1027
|
+
if (!event.payload.toolCall.toolCall)
|
|
1028
|
+
return localAppProjectionError('Runtime tool call');
|
|
1029
|
+
return { ...base, type: 'tool-call', itemIndex: event.payload.toolCall.itemIndex, toolCall: toNimiToolCall(event.payload.toolCall.toolCall) };
|
|
998
1030
|
case 'delta':
|
|
999
|
-
return { ...base, type: 'delta', text: event.payload.delta.text };
|
|
1031
|
+
return { ...base, type: 'delta', text: event.payload.delta.text, itemIndex: event.payload.delta.itemIndex };
|
|
1000
1032
|
case 'completed':
|
|
1001
1033
|
return {
|
|
1002
1034
|
...base,
|
|
@@ -1009,6 +1041,7 @@ function projectRuntimeTextTurnEvent(event) {
|
|
|
1009
1041
|
type: 'failed',
|
|
1010
1042
|
reasonCode: runtimeReasonToken(event.payload.failed.reasonCode),
|
|
1011
1043
|
actionHint: event.payload.failed.actionHint,
|
|
1044
|
+
...(event.payload.failed.interruption ? { interruption: localInterruptionFromRuntime(event.payload.failed.interruption) } : {}),
|
|
1012
1045
|
};
|
|
1013
1046
|
default:
|
|
1014
1047
|
return localAppProjectionError('text-turn Runtime event');
|
|
@@ -1016,11 +1049,14 @@ function projectRuntimeTextTurnEvent(event) {
|
|
|
1016
1049
|
}
|
|
1017
1050
|
function projectRuntimeScenarioExecuteResponse(response) {
|
|
1018
1051
|
switch (response.output.oneofKind) {
|
|
1052
|
+
case 'textGenerate':
|
|
1053
|
+
return { output: { type: 'text-generate', items: toNimiTextOutputItems(response.output.textGenerate.items).map(modelTextOutputToLocalApp), finishReason: runtimeFinishReason(response.output.textGenerate.finishReason) }, traceId: response.traceId };
|
|
1019
1054
|
case 'textEmbed':
|
|
1020
1055
|
return {
|
|
1021
1056
|
output: {
|
|
1022
1057
|
type: 'text-embed',
|
|
1023
1058
|
vectors: response.output.textEmbed.vectors.map((vector) => [...vector.values]),
|
|
1059
|
+
spaceId: response.output.textEmbed.spaceId,
|
|
1024
1060
|
},
|
|
1025
1061
|
traceId: response.traceId,
|
|
1026
1062
|
};
|
|
@@ -1127,6 +1163,8 @@ function runtimeJobEventTypeName(value) {
|
|
|
1127
1163
|
return types[value] ?? localAppProjectionError('scenario Runtime event type');
|
|
1128
1164
|
}
|
|
1129
1165
|
function runtimeFinishReason(value) {
|
|
1166
|
+
if (value === FinishReason.TOOL_CALL)
|
|
1167
|
+
return 'tool-calls';
|
|
1130
1168
|
if (value === FinishReason.STOP)
|
|
1131
1169
|
return 'stop';
|
|
1132
1170
|
if (value === FinishReason.LENGTH)
|