@codelia/core 0.1.26 → 0.1.28
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/index.cjs +79 -84
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +79 -84
- package/package.json +4 -4
- package/prompts/system.md +7 -0
package/dist/index.cjs
CHANGED
|
@@ -2419,6 +2419,16 @@ var isOpenAIAssistantInputContent = (value) => {
|
|
|
2419
2419
|
}
|
|
2420
2420
|
return false;
|
|
2421
2421
|
};
|
|
2422
|
+
var isReplayableOpenAIHistoryItem = (value) => {
|
|
2423
|
+
if (!value || typeof value !== "object") {
|
|
2424
|
+
return false;
|
|
2425
|
+
}
|
|
2426
|
+
const type = value.type;
|
|
2427
|
+
if (type === "reasoning") {
|
|
2428
|
+
return typeof value.id === "string";
|
|
2429
|
+
}
|
|
2430
|
+
return type === "web_search_call";
|
|
2431
|
+
};
|
|
2422
2432
|
var isReplayableOpenAIFunctionCallItem = (value) => {
|
|
2423
2433
|
if (!value || typeof value !== "object") {
|
|
2424
2434
|
return false;
|
|
@@ -2592,6 +2602,9 @@ function toResponsesInput(messages) {
|
|
|
2592
2602
|
continue;
|
|
2593
2603
|
}
|
|
2594
2604
|
if (message.role === "reasoning") {
|
|
2605
|
+
if (isReplayableOpenAIHistoryItem(message.raw_item)) {
|
|
2606
|
+
items.push(message.raw_item);
|
|
2607
|
+
}
|
|
2595
2608
|
continue;
|
|
2596
2609
|
}
|
|
2597
2610
|
if (message.role === "assistant") {
|
|
@@ -2762,8 +2775,7 @@ var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
|
2762
2775
|
|
|
2763
2776
|
// src/llm/openai/websocket-transport.ts
|
|
2764
2777
|
var import_ws = require("openai/resources/responses/ws");
|
|
2765
|
-
var
|
|
2766
|
-
var WS_RESPONSE_TIMEOUT_MS = 12e4;
|
|
2778
|
+
var WS_RESPONSE_TIMEOUT_MS = 45e3;
|
|
2767
2779
|
var WS_UNEXPECTED_RESPONSE_BODY_TIMEOUT_MS = 250;
|
|
2768
2780
|
var WS_UNEXPECTED_RESPONSE_BODY_MAX_CHARS = 2e3;
|
|
2769
2781
|
var OPENAI_BETA_HEADER = "OpenAI-Beta";
|
|
@@ -3242,7 +3254,7 @@ var OpenAiWsTransport = class {
|
|
|
3242
3254
|
};
|
|
3243
3255
|
const timeout = setTimeout(() => {
|
|
3244
3256
|
settleReject(new Error("openai websocket connect timeout"));
|
|
3245
|
-
},
|
|
3257
|
+
}, WS_RESPONSE_TIMEOUT_MS);
|
|
3246
3258
|
addSocketListener("open", () => {
|
|
3247
3259
|
settleResolve();
|
|
3248
3260
|
});
|
|
@@ -3292,9 +3304,6 @@ var getSessionIdHeaderValue = (promptCacheKey) => {
|
|
|
3292
3304
|
var WS_SESSION_IDLE_TTL_MS = 10 * 6e4;
|
|
3293
3305
|
var WS_SESSION_DISABLE_TTL_MS = 6e4;
|
|
3294
3306
|
var WS_REUSE_MAX_IDLE_MS = 3e4;
|
|
3295
|
-
var WS_ON_MODE_RETRY_MAX_ATTEMPTS = 3;
|
|
3296
|
-
var WS_ON_MODE_RETRY_BASE_BACKOFF_MS = 250;
|
|
3297
|
-
var WS_ON_MODE_RETRY_MAX_BACKOFF_MS = 2e3;
|
|
3298
3307
|
var ChatOpenAI = class {
|
|
3299
3308
|
provider = PROVIDER_NAME2;
|
|
3300
3309
|
model;
|
|
@@ -3447,7 +3456,7 @@ var ChatOpenAI = class {
|
|
|
3447
3456
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3448
3457
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3449
3458
|
let wsInputMode = "full_no_previous";
|
|
3450
|
-
if (wsState.previousResponseId) {
|
|
3459
|
+
if (Boolean(wsState.previousResponseId)) {
|
|
3451
3460
|
if (canUsePreviousResponseId) {
|
|
3452
3461
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3453
3462
|
wsInputMode = "empty";
|
|
@@ -3509,50 +3518,43 @@ var ChatOpenAI = class {
|
|
|
3509
3518
|
} catch (error) {
|
|
3510
3519
|
let failure = error;
|
|
3511
3520
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3512
|
-
const retryWsInputMode =
|
|
3521
|
+
const retryWsInputMode = Boolean(
|
|
3522
|
+
wsState.previousResponseId
|
|
3523
|
+
) ? "full_regenerated" : "full_no_previous";
|
|
3513
3524
|
this.closeWsWithoutMaskingPrimaryError(wsState.ws, "retry_reset", failure);
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
chainReset: true,
|
|
3548
|
-
wsInputMode: retryWsInputMode
|
|
3549
|
-
};
|
|
3550
|
-
} catch (retryError) {
|
|
3551
|
-
failure = retryError;
|
|
3552
|
-
if (!this.isWsReconnectRetryableError(failure)) {
|
|
3553
|
-
break;
|
|
3554
|
-
}
|
|
3555
|
-
}
|
|
3525
|
+
try {
|
|
3526
|
+
await this.debugRequestIfEnabled(
|
|
3527
|
+
args.request,
|
|
3528
|
+
args.debugSeq,
|
|
3529
|
+
args.sessionIdHeader,
|
|
3530
|
+
"ws_mode",
|
|
3531
|
+
false,
|
|
3532
|
+
true,
|
|
3533
|
+
retryWsInputMode
|
|
3534
|
+
);
|
|
3535
|
+
const { response, ws } = await this.invokeViaWs({
|
|
3536
|
+
request: args.request,
|
|
3537
|
+
signal: args.signal,
|
|
3538
|
+
sessionIdHeader: args.sessionIdHeader,
|
|
3539
|
+
ws: void 0
|
|
3540
|
+
});
|
|
3541
|
+
this.wsReconnectCount += 1;
|
|
3542
|
+
this.persistWsSessionState({
|
|
3543
|
+
sessionKey: args.sessionKey,
|
|
3544
|
+
request: args.request,
|
|
3545
|
+
requestMeta: args.requestMeta,
|
|
3546
|
+
response,
|
|
3547
|
+
ws
|
|
3548
|
+
});
|
|
3549
|
+
return {
|
|
3550
|
+
response,
|
|
3551
|
+
transport: "ws_mode",
|
|
3552
|
+
fallbackUsed: false,
|
|
3553
|
+
chainReset: true,
|
|
3554
|
+
wsInputMode: retryWsInputMode
|
|
3555
|
+
};
|
|
3556
|
+
} catch (retryError) {
|
|
3557
|
+
failure = retryError;
|
|
3556
3558
|
}
|
|
3557
3559
|
}
|
|
3558
3560
|
const wsErrorCode = this.extractWsErrorCode(failure);
|
|
@@ -3610,11 +3612,21 @@ var ChatOpenAI = class {
|
|
|
3610
3612
|
instructionsHash: args.requestMeta.instructionsHash,
|
|
3611
3613
|
toolsHash: args.requestMeta.toolsHash,
|
|
3612
3614
|
model: args.requestMeta.model,
|
|
3613
|
-
lastInput: args.request.input,
|
|
3615
|
+
lastInput: this.buildWsLastInput(args.request.input, args.response),
|
|
3614
3616
|
ws: args.ws,
|
|
3615
3617
|
lastUsedAt: Date.now()
|
|
3616
3618
|
});
|
|
3617
3619
|
}
|
|
3620
|
+
buildWsLastInput(requestInput, response) {
|
|
3621
|
+
if (!Array.isArray(requestInput)) {
|
|
3622
|
+
return requestInput;
|
|
3623
|
+
}
|
|
3624
|
+
const replayItems = toResponsesInput(toChatInvokeCompletion2(response).messages);
|
|
3625
|
+
if (replayItems.length === 0) {
|
|
3626
|
+
return requestInput;
|
|
3627
|
+
}
|
|
3628
|
+
return [...requestInput, ...replayItems];
|
|
3629
|
+
}
|
|
3618
3630
|
async invokeViaWs(args) {
|
|
3619
3631
|
return this.wsTransport.invoke(args);
|
|
3620
3632
|
}
|
|
@@ -3637,35 +3649,6 @@ var ChatOpenAI = class {
|
|
|
3637
3649
|
const message = error.message.toLowerCase();
|
|
3638
3650
|
return message.includes("response timeout") || message.includes("closed before response") || message.includes("closed before open") || message.includes("connect timeout") || message.includes("could not send data") || message.includes("websocket is not open");
|
|
3639
3651
|
}
|
|
3640
|
-
getWsReconnectRetryDelayMs(retryAttempt) {
|
|
3641
|
-
const backoff = WS_ON_MODE_RETRY_BASE_BACKOFF_MS * 2 ** Math.max(retryAttempt - 1, 0);
|
|
3642
|
-
return Math.min(backoff, WS_ON_MODE_RETRY_MAX_BACKOFF_MS);
|
|
3643
|
-
}
|
|
3644
|
-
async waitForWsReconnectRetryDelay(retryAttempt, signal) {
|
|
3645
|
-
if (signal?.aborted) {
|
|
3646
|
-
throw new Error("openai websocket request aborted");
|
|
3647
|
-
}
|
|
3648
|
-
const delayMs = this.getWsReconnectRetryDelayMs(retryAttempt);
|
|
3649
|
-
await new Promise((resolve, reject) => {
|
|
3650
|
-
const timeout = setTimeout(() => {
|
|
3651
|
-
teardown();
|
|
3652
|
-
resolve();
|
|
3653
|
-
}, delayMs);
|
|
3654
|
-
const onAbort = () => {
|
|
3655
|
-
teardown();
|
|
3656
|
-
reject(new Error("openai websocket request aborted"));
|
|
3657
|
-
};
|
|
3658
|
-
const teardown = () => {
|
|
3659
|
-
clearTimeout(timeout);
|
|
3660
|
-
if (signal) {
|
|
3661
|
-
signal.removeEventListener("abort", onAbort);
|
|
3662
|
-
}
|
|
3663
|
-
};
|
|
3664
|
-
if (signal) {
|
|
3665
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
3666
|
-
}
|
|
3667
|
-
});
|
|
3668
|
-
}
|
|
3669
3652
|
isWsConnectionReusable(ws) {
|
|
3670
3653
|
return this.wsTransport.isConnectionReusable(ws);
|
|
3671
3654
|
}
|
|
@@ -3711,7 +3694,7 @@ var ChatOpenAI = class {
|
|
|
3711
3694
|
return void 0;
|
|
3712
3695
|
}
|
|
3713
3696
|
if (!Array.isArray(previousInput) || !Array.isArray(currentInput)) {
|
|
3714
|
-
return
|
|
3697
|
+
return null;
|
|
3715
3698
|
}
|
|
3716
3699
|
if (previousInput.length > currentInput.length) {
|
|
3717
3700
|
return null;
|
|
@@ -3722,7 +3705,7 @@ var ChatOpenAI = class {
|
|
|
3722
3705
|
}
|
|
3723
3706
|
}
|
|
3724
3707
|
if (previousInput.length === currentInput.length) {
|
|
3725
|
-
return
|
|
3708
|
+
return null;
|
|
3726
3709
|
}
|
|
3727
3710
|
return currentInput.slice(previousInput.length);
|
|
3728
3711
|
}
|
|
@@ -4507,6 +4490,18 @@ var withRequiredProperties = (schema) => mapSchema(schema, (next) => {
|
|
|
4507
4490
|
}
|
|
4508
4491
|
return next;
|
|
4509
4492
|
});
|
|
4493
|
+
var withTopLevelObjectTypeForObjectUnions = (schema) => {
|
|
4494
|
+
if (schema.type !== void 0) return schema;
|
|
4495
|
+
const variants = (Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : null) ?? null;
|
|
4496
|
+
if (!variants) return schema;
|
|
4497
|
+
const allObjectVariants = variants.every((variant) => {
|
|
4498
|
+
if (!variant || typeof variant !== "object") return false;
|
|
4499
|
+
const jsonSchema = variant;
|
|
4500
|
+
return jsonSchema.type === "object";
|
|
4501
|
+
});
|
|
4502
|
+
if (!allObjectVariants) return schema;
|
|
4503
|
+
return { ...schema, type: "object" };
|
|
4504
|
+
};
|
|
4510
4505
|
function toToolResult(value) {
|
|
4511
4506
|
if (isToolResult(value)) return value;
|
|
4512
4507
|
if (typeof value === "string") return { type: "text", text: value };
|
|
@@ -4519,8 +4514,8 @@ function defineTool(toolOptions) {
|
|
|
4519
4514
|
target: "draft-07",
|
|
4520
4515
|
io: "input"
|
|
4521
4516
|
});
|
|
4522
|
-
const strictParameters =
|
|
4523
|
-
withNoAdditionalProperties(parameters)
|
|
4517
|
+
const strictParameters = withTopLevelObjectTypeForObjectUnions(
|
|
4518
|
+
withRequiredProperties(withNoAdditionalProperties(parameters))
|
|
4524
4519
|
);
|
|
4525
4520
|
return {
|
|
4526
4521
|
name: toolOptions.name,
|
package/dist/index.d.cts
CHANGED
|
@@ -689,13 +689,12 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$1, OpenAI
|
|
|
689
689
|
private invokeWithTransport;
|
|
690
690
|
private invokeHttpTransportWithDebug;
|
|
691
691
|
private persistWsSessionState;
|
|
692
|
+
private buildWsLastInput;
|
|
692
693
|
private invokeViaWs;
|
|
693
694
|
private closeWsSafely;
|
|
694
695
|
private closeWsWithoutMaskingPrimaryError;
|
|
695
696
|
private isWsConnectionReuseExpired;
|
|
696
697
|
private isWsReconnectRetryableError;
|
|
697
|
-
private getWsReconnectRetryDelayMs;
|
|
698
|
-
private waitForWsReconnectRetryDelay;
|
|
699
698
|
private isWsConnectionReusable;
|
|
700
699
|
private extractWsErrorCode;
|
|
701
700
|
private clearWsSessionState;
|
package/dist/index.d.ts
CHANGED
|
@@ -689,13 +689,12 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$1, OpenAI
|
|
|
689
689
|
private invokeWithTransport;
|
|
690
690
|
private invokeHttpTransportWithDebug;
|
|
691
691
|
private persistWsSessionState;
|
|
692
|
+
private buildWsLastInput;
|
|
692
693
|
private invokeViaWs;
|
|
693
694
|
private closeWsSafely;
|
|
694
695
|
private closeWsWithoutMaskingPrimaryError;
|
|
695
696
|
private isWsConnectionReuseExpired;
|
|
696
697
|
private isWsReconnectRetryableError;
|
|
697
|
-
private getWsReconnectRetryDelayMs;
|
|
698
|
-
private waitForWsReconnectRetryDelay;
|
|
699
698
|
private isWsConnectionReusable;
|
|
700
699
|
private extractWsErrorCode;
|
|
701
700
|
private clearWsSessionState;
|
package/dist/index.js
CHANGED
|
@@ -2362,6 +2362,16 @@ var isOpenAIAssistantInputContent = (value) => {
|
|
|
2362
2362
|
}
|
|
2363
2363
|
return false;
|
|
2364
2364
|
};
|
|
2365
|
+
var isReplayableOpenAIHistoryItem = (value) => {
|
|
2366
|
+
if (!value || typeof value !== "object") {
|
|
2367
|
+
return false;
|
|
2368
|
+
}
|
|
2369
|
+
const type = value.type;
|
|
2370
|
+
if (type === "reasoning") {
|
|
2371
|
+
return typeof value.id === "string";
|
|
2372
|
+
}
|
|
2373
|
+
return type === "web_search_call";
|
|
2374
|
+
};
|
|
2365
2375
|
var isReplayableOpenAIFunctionCallItem = (value) => {
|
|
2366
2376
|
if (!value || typeof value !== "object") {
|
|
2367
2377
|
return false;
|
|
@@ -2535,6 +2545,9 @@ function toResponsesInput(messages) {
|
|
|
2535
2545
|
continue;
|
|
2536
2546
|
}
|
|
2537
2547
|
if (message.role === "reasoning") {
|
|
2548
|
+
if (isReplayableOpenAIHistoryItem(message.raw_item)) {
|
|
2549
|
+
items.push(message.raw_item);
|
|
2550
|
+
}
|
|
2538
2551
|
continue;
|
|
2539
2552
|
}
|
|
2540
2553
|
if (message.role === "assistant") {
|
|
@@ -2705,8 +2718,7 @@ var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
|
2705
2718
|
|
|
2706
2719
|
// src/llm/openai/websocket-transport.ts
|
|
2707
2720
|
import { ResponsesWS } from "openai/resources/responses/ws";
|
|
2708
|
-
var
|
|
2709
|
-
var WS_RESPONSE_TIMEOUT_MS = 12e4;
|
|
2721
|
+
var WS_RESPONSE_TIMEOUT_MS = 45e3;
|
|
2710
2722
|
var WS_UNEXPECTED_RESPONSE_BODY_TIMEOUT_MS = 250;
|
|
2711
2723
|
var WS_UNEXPECTED_RESPONSE_BODY_MAX_CHARS = 2e3;
|
|
2712
2724
|
var OPENAI_BETA_HEADER = "OpenAI-Beta";
|
|
@@ -3185,7 +3197,7 @@ var OpenAiWsTransport = class {
|
|
|
3185
3197
|
};
|
|
3186
3198
|
const timeout = setTimeout(() => {
|
|
3187
3199
|
settleReject(new Error("openai websocket connect timeout"));
|
|
3188
|
-
},
|
|
3200
|
+
}, WS_RESPONSE_TIMEOUT_MS);
|
|
3189
3201
|
addSocketListener("open", () => {
|
|
3190
3202
|
settleResolve();
|
|
3191
3203
|
});
|
|
@@ -3235,9 +3247,6 @@ var getSessionIdHeaderValue = (promptCacheKey) => {
|
|
|
3235
3247
|
var WS_SESSION_IDLE_TTL_MS = 10 * 6e4;
|
|
3236
3248
|
var WS_SESSION_DISABLE_TTL_MS = 6e4;
|
|
3237
3249
|
var WS_REUSE_MAX_IDLE_MS = 3e4;
|
|
3238
|
-
var WS_ON_MODE_RETRY_MAX_ATTEMPTS = 3;
|
|
3239
|
-
var WS_ON_MODE_RETRY_BASE_BACKOFF_MS = 250;
|
|
3240
|
-
var WS_ON_MODE_RETRY_MAX_BACKOFF_MS = 2e3;
|
|
3241
3250
|
var ChatOpenAI = class {
|
|
3242
3251
|
provider = PROVIDER_NAME2;
|
|
3243
3252
|
model;
|
|
@@ -3390,7 +3399,7 @@ var ChatOpenAI = class {
|
|
|
3390
3399
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3391
3400
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3392
3401
|
let wsInputMode = "full_no_previous";
|
|
3393
|
-
if (wsState.previousResponseId) {
|
|
3402
|
+
if (Boolean(wsState.previousResponseId)) {
|
|
3394
3403
|
if (canUsePreviousResponseId) {
|
|
3395
3404
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3396
3405
|
wsInputMode = "empty";
|
|
@@ -3452,50 +3461,43 @@ var ChatOpenAI = class {
|
|
|
3452
3461
|
} catch (error) {
|
|
3453
3462
|
let failure = error;
|
|
3454
3463
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3455
|
-
const retryWsInputMode =
|
|
3464
|
+
const retryWsInputMode = Boolean(
|
|
3465
|
+
wsState.previousResponseId
|
|
3466
|
+
) ? "full_regenerated" : "full_no_previous";
|
|
3456
3467
|
this.closeWsWithoutMaskingPrimaryError(wsState.ws, "retry_reset", failure);
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
chainReset: true,
|
|
3491
|
-
wsInputMode: retryWsInputMode
|
|
3492
|
-
};
|
|
3493
|
-
} catch (retryError) {
|
|
3494
|
-
failure = retryError;
|
|
3495
|
-
if (!this.isWsReconnectRetryableError(failure)) {
|
|
3496
|
-
break;
|
|
3497
|
-
}
|
|
3498
|
-
}
|
|
3468
|
+
try {
|
|
3469
|
+
await this.debugRequestIfEnabled(
|
|
3470
|
+
args.request,
|
|
3471
|
+
args.debugSeq,
|
|
3472
|
+
args.sessionIdHeader,
|
|
3473
|
+
"ws_mode",
|
|
3474
|
+
false,
|
|
3475
|
+
true,
|
|
3476
|
+
retryWsInputMode
|
|
3477
|
+
);
|
|
3478
|
+
const { response, ws } = await this.invokeViaWs({
|
|
3479
|
+
request: args.request,
|
|
3480
|
+
signal: args.signal,
|
|
3481
|
+
sessionIdHeader: args.sessionIdHeader,
|
|
3482
|
+
ws: void 0
|
|
3483
|
+
});
|
|
3484
|
+
this.wsReconnectCount += 1;
|
|
3485
|
+
this.persistWsSessionState({
|
|
3486
|
+
sessionKey: args.sessionKey,
|
|
3487
|
+
request: args.request,
|
|
3488
|
+
requestMeta: args.requestMeta,
|
|
3489
|
+
response,
|
|
3490
|
+
ws
|
|
3491
|
+
});
|
|
3492
|
+
return {
|
|
3493
|
+
response,
|
|
3494
|
+
transport: "ws_mode",
|
|
3495
|
+
fallbackUsed: false,
|
|
3496
|
+
chainReset: true,
|
|
3497
|
+
wsInputMode: retryWsInputMode
|
|
3498
|
+
};
|
|
3499
|
+
} catch (retryError) {
|
|
3500
|
+
failure = retryError;
|
|
3499
3501
|
}
|
|
3500
3502
|
}
|
|
3501
3503
|
const wsErrorCode = this.extractWsErrorCode(failure);
|
|
@@ -3553,11 +3555,21 @@ var ChatOpenAI = class {
|
|
|
3553
3555
|
instructionsHash: args.requestMeta.instructionsHash,
|
|
3554
3556
|
toolsHash: args.requestMeta.toolsHash,
|
|
3555
3557
|
model: args.requestMeta.model,
|
|
3556
|
-
lastInput: args.request.input,
|
|
3558
|
+
lastInput: this.buildWsLastInput(args.request.input, args.response),
|
|
3557
3559
|
ws: args.ws,
|
|
3558
3560
|
lastUsedAt: Date.now()
|
|
3559
3561
|
});
|
|
3560
3562
|
}
|
|
3563
|
+
buildWsLastInput(requestInput, response) {
|
|
3564
|
+
if (!Array.isArray(requestInput)) {
|
|
3565
|
+
return requestInput;
|
|
3566
|
+
}
|
|
3567
|
+
const replayItems = toResponsesInput(toChatInvokeCompletion2(response).messages);
|
|
3568
|
+
if (replayItems.length === 0) {
|
|
3569
|
+
return requestInput;
|
|
3570
|
+
}
|
|
3571
|
+
return [...requestInput, ...replayItems];
|
|
3572
|
+
}
|
|
3561
3573
|
async invokeViaWs(args) {
|
|
3562
3574
|
return this.wsTransport.invoke(args);
|
|
3563
3575
|
}
|
|
@@ -3580,35 +3592,6 @@ var ChatOpenAI = class {
|
|
|
3580
3592
|
const message = error.message.toLowerCase();
|
|
3581
3593
|
return message.includes("response timeout") || message.includes("closed before response") || message.includes("closed before open") || message.includes("connect timeout") || message.includes("could not send data") || message.includes("websocket is not open");
|
|
3582
3594
|
}
|
|
3583
|
-
getWsReconnectRetryDelayMs(retryAttempt) {
|
|
3584
|
-
const backoff = WS_ON_MODE_RETRY_BASE_BACKOFF_MS * 2 ** Math.max(retryAttempt - 1, 0);
|
|
3585
|
-
return Math.min(backoff, WS_ON_MODE_RETRY_MAX_BACKOFF_MS);
|
|
3586
|
-
}
|
|
3587
|
-
async waitForWsReconnectRetryDelay(retryAttempt, signal) {
|
|
3588
|
-
if (signal?.aborted) {
|
|
3589
|
-
throw new Error("openai websocket request aborted");
|
|
3590
|
-
}
|
|
3591
|
-
const delayMs = this.getWsReconnectRetryDelayMs(retryAttempt);
|
|
3592
|
-
await new Promise((resolve, reject) => {
|
|
3593
|
-
const timeout = setTimeout(() => {
|
|
3594
|
-
teardown();
|
|
3595
|
-
resolve();
|
|
3596
|
-
}, delayMs);
|
|
3597
|
-
const onAbort = () => {
|
|
3598
|
-
teardown();
|
|
3599
|
-
reject(new Error("openai websocket request aborted"));
|
|
3600
|
-
};
|
|
3601
|
-
const teardown = () => {
|
|
3602
|
-
clearTimeout(timeout);
|
|
3603
|
-
if (signal) {
|
|
3604
|
-
signal.removeEventListener("abort", onAbort);
|
|
3605
|
-
}
|
|
3606
|
-
};
|
|
3607
|
-
if (signal) {
|
|
3608
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
3609
|
-
}
|
|
3610
|
-
});
|
|
3611
|
-
}
|
|
3612
3595
|
isWsConnectionReusable(ws) {
|
|
3613
3596
|
return this.wsTransport.isConnectionReusable(ws);
|
|
3614
3597
|
}
|
|
@@ -3654,7 +3637,7 @@ var ChatOpenAI = class {
|
|
|
3654
3637
|
return void 0;
|
|
3655
3638
|
}
|
|
3656
3639
|
if (!Array.isArray(previousInput) || !Array.isArray(currentInput)) {
|
|
3657
|
-
return
|
|
3640
|
+
return null;
|
|
3658
3641
|
}
|
|
3659
3642
|
if (previousInput.length > currentInput.length) {
|
|
3660
3643
|
return null;
|
|
@@ -3665,7 +3648,7 @@ var ChatOpenAI = class {
|
|
|
3665
3648
|
}
|
|
3666
3649
|
}
|
|
3667
3650
|
if (previousInput.length === currentInput.length) {
|
|
3668
|
-
return
|
|
3651
|
+
return null;
|
|
3669
3652
|
}
|
|
3670
3653
|
return currentInput.slice(previousInput.length);
|
|
3671
3654
|
}
|
|
@@ -4449,6 +4432,18 @@ var withRequiredProperties = (schema) => mapSchema(schema, (next) => {
|
|
|
4449
4432
|
}
|
|
4450
4433
|
return next;
|
|
4451
4434
|
});
|
|
4435
|
+
var withTopLevelObjectTypeForObjectUnions = (schema) => {
|
|
4436
|
+
if (schema.type !== void 0) return schema;
|
|
4437
|
+
const variants = (Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : null) ?? null;
|
|
4438
|
+
if (!variants) return schema;
|
|
4439
|
+
const allObjectVariants = variants.every((variant) => {
|
|
4440
|
+
if (!variant || typeof variant !== "object") return false;
|
|
4441
|
+
const jsonSchema = variant;
|
|
4442
|
+
return jsonSchema.type === "object";
|
|
4443
|
+
});
|
|
4444
|
+
if (!allObjectVariants) return schema;
|
|
4445
|
+
return { ...schema, type: "object" };
|
|
4446
|
+
};
|
|
4452
4447
|
function toToolResult(value) {
|
|
4453
4448
|
if (isToolResult(value)) return value;
|
|
4454
4449
|
if (typeof value === "string") return { type: "text", text: value };
|
|
@@ -4461,8 +4456,8 @@ function defineTool(toolOptions) {
|
|
|
4461
4456
|
target: "draft-07",
|
|
4462
4457
|
io: "input"
|
|
4463
4458
|
});
|
|
4464
|
-
const strictParameters =
|
|
4465
|
-
withNoAdditionalProperties(parameters)
|
|
4459
|
+
const strictParameters = withTopLevelObjectTypeForObjectUnions(
|
|
4460
|
+
withRequiredProperties(withNoAdditionalProperties(parameters))
|
|
4466
4461
|
);
|
|
4467
4462
|
return {
|
|
4468
4463
|
name: toolOptions.name,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codelia/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.28",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@anthropic-ai/sdk": "^0.78.0",
|
|
28
|
-
"@codelia/config": "0.1.
|
|
29
|
-
"@codelia/shared-types": "0.1.
|
|
28
|
+
"@codelia/config": "0.1.28",
|
|
29
|
+
"@codelia/shared-types": "0.1.28",
|
|
30
30
|
"@google-cloud/vertexai": "^1.10.0",
|
|
31
31
|
"@google/genai": "^1.37.0",
|
|
32
32
|
"json-schema": "^0.4.0",
|
|
33
|
-
"openai": "^6.
|
|
33
|
+
"openai": "^6.25.0",
|
|
34
34
|
"zod": "^4.3.5"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/prompts/system.md
CHANGED
|
@@ -52,6 +52,9 @@ Assume:
|
|
|
52
52
|
- Keep types tight in typed languages; avoid escaping the type system when safer narrowing is possible.
|
|
53
53
|
- Prefer the smallest correct edit. Avoid broad refactors unless explicitly requested.
|
|
54
54
|
- When editing, prefer `edit` for targeted patches; prefer `write` only when replacing an entire file is simpler/safer.
|
|
55
|
+
- Treat `edit` misses (for example `String not found in <path>`) as hard failures, not partial success.
|
|
56
|
+
- After an `edit`, verify the intended change (e.g. re-read target lines or inspect diff) before proceeding to follow-up edits.
|
|
57
|
+
- If an `edit` misses repeatedly on the same target, stop and re-locate the exact current text instead of retrying the same patch blindly.
|
|
55
58
|
|
|
56
59
|
## Workflow expectations
|
|
57
60
|
|
|
@@ -89,6 +92,10 @@ When implementing features/changes:
|
|
|
89
92
|
- Create a short plan before execution for non-trivial tasks.
|
|
90
93
|
- Keep the plan short, ordered, and update it when scope or facts change.
|
|
91
94
|
- Skip formal plans for straightforward tasks; do not make single-step plans.
|
|
95
|
+
- For non-trivial work, maintain the plan with `todo_write` / `todo_read` instead of keeping it only in free-form text.
|
|
96
|
+
- Keep at most one todo item in `in_progress`; complete or reprioritize it before starting another.
|
|
97
|
+
- Use `todo_write` modes intentionally: `new` for initial/restart planning, `append` for newly discovered tasks, `patch` for progress/status updates by id, and `clear` when the plan should be reset.
|
|
98
|
+
- Before final response on non-trivial work, check `todo_read` and either finish pending work or explicitly report what remains.
|
|
92
99
|
|
|
93
100
|
## Special user requests
|
|
94
101
|
|