@zhafron/opencode-kiro-auth 1.6.0 → 1.6.1

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.
@@ -1,5 +1,6 @@
1
1
  import type { CodeWhispererMessage } from '../../plugin/types';
2
- export declare function buildHistory(msgs: any[], resolved: string, system: string | undefined, toolResultLimit: number): CodeWhispererMessage[];
2
+ export declare function buildHistory(msgs: any[], resolved: string, toolResultLimit: number): CodeWhispererMessage[];
3
+ export declare function injectSystemPrompt(history: CodeWhispererMessage[], system: string | undefined, resolved: string): CodeWhispererMessage[];
3
4
  export declare function truncateHistory(history: CodeWhispererMessage[], historyLimit: number): CodeWhispererMessage[];
4
5
  export declare function historyHasToolCalling(history: CodeWhispererMessage[]): boolean;
5
6
  export declare function extractToolNamesFromHistory(history: CodeWhispererMessage[]): Set<string>;
@@ -2,38 +2,8 @@ import { KIRO_CONSTANTS } from '../../constants.js';
2
2
  import { convertImagesToKiroFormat, extractAllImages, extractTextFromParts } from '../../plugin/image-handler.js';
3
3
  import { getContentText, sanitizeHistory, truncate } from './message-transformer.js';
4
4
  import { deduplicateToolResults } from './tool-transformer.js';
5
- export function buildHistory(msgs, resolved, system, toolResultLimit) {
5
+ export function buildHistory(msgs, resolved, toolResultLimit) {
6
6
  let history = [];
7
- let firstUserIndex = -1;
8
- for (let i = 0; i < msgs.length; i++) {
9
- if (msgs[i].role === 'user') {
10
- firstUserIndex = i;
11
- break;
12
- }
13
- }
14
- if (system) {
15
- if (firstUserIndex !== -1) {
16
- const m = msgs[firstUserIndex];
17
- const oldContent = getContentText(m);
18
- if (Array.isArray(m.content)) {
19
- m.content = [
20
- { type: 'text', text: `${system}\n\n${oldContent}` },
21
- ...m.content.filter((p) => p.type !== 'text')
22
- ];
23
- }
24
- else
25
- m.content = `${system}\n\n${oldContent}`;
26
- }
27
- else {
28
- history.push({
29
- userInputMessage: {
30
- content: system,
31
- modelId: resolved,
32
- origin: KIRO_CONSTANTS.ORIGIN_AI_EDITOR
33
- }
34
- });
35
- }
36
- }
37
7
  for (let i = 0; i < msgs.length - 1; i++) {
38
8
  const m = msgs[i];
39
9
  if (!m)
@@ -137,6 +107,25 @@ export function buildHistory(msgs, resolved, system, toolResultLimit) {
137
107
  }
138
108
  return history;
139
109
  }
110
+ export function injectSystemPrompt(history, system, resolved) {
111
+ if (!system)
112
+ return history;
113
+ const firstUserMsg = history.find((h) => !!h.userInputMessage);
114
+ if (firstUserMsg && firstUserMsg.userInputMessage) {
115
+ const oldContent = firstUserMsg.userInputMessage.content || '';
116
+ firstUserMsg.userInputMessage.content = `${system}\n\n${oldContent}`;
117
+ }
118
+ else {
119
+ history.unshift({
120
+ userInputMessage: {
121
+ content: system,
122
+ modelId: resolved,
123
+ origin: KIRO_CONSTANTS.ORIGIN_AI_EDITOR
124
+ }
125
+ });
126
+ }
127
+ return history;
128
+ }
140
129
  export function truncateHistory(history, historyLimit) {
141
130
  let sanitized = sanitizeHistory(history);
142
131
  let historySize = JSON.stringify(sanitized).length;
@@ -1,7 +1,7 @@
1
1
  import * as crypto from 'crypto';
2
2
  import * as os from 'os';
3
3
  import { KIRO_CONSTANTS } from '../constants.js';
4
- import { buildHistory, extractToolNamesFromHistory, historyHasToolCalling, truncateHistory } from '../infrastructure/transformers/history-builder.js';
4
+ import { buildHistory, extractToolNamesFromHistory, historyHasToolCalling, injectSystemPrompt, truncateHistory } from '../infrastructure/transformers/history-builder.js';
5
5
  import { findOriginalToolCall, getContentText, mergeAdjacentMessages, truncate } from '../infrastructure/transformers/message-transformer.js';
6
6
  import { convertToolsToCodeWhisperer, deduplicateToolResults } from '../infrastructure/transformers/tool-transformer.js';
7
7
  import { convertImagesToKiroFormat, extractAllImages, extractTextFromParts } from './image-handler.js';
@@ -13,20 +13,27 @@ export function transformToCodeWhisperer(url, body, model, auth, think = false,
13
13
  if (!messages || messages.length === 0)
14
14
  throw new Error('No messages');
15
15
  const resolved = resolveKiroModel(model);
16
+ const systemMsgs = messages.filter((m) => m.role === 'system');
17
+ const otherMsgs = messages.filter((m) => m.role !== 'system');
16
18
  let sys = system || '';
19
+ if (systemMsgs.length > 0) {
20
+ const extractedSystem = systemMsgs.map((m) => getContentText(m)).join('\n\n');
21
+ sys = sys ? `${sys}\n\n${extractedSystem}` : extractedSystem;
22
+ }
17
23
  if (think) {
18
24
  const pfx = `<thinking_mode>enabled</thinking_mode><max_thinking_length>${budget}</max_thinking_length>`;
19
25
  sys = sys.includes('<thinking_mode>') ? sys : sys ? `${pfx}\n${sys}` : pfx;
20
26
  }
21
- const msgs = mergeAdjacentMessages([...messages]);
27
+ const msgs = mergeAdjacentMessages([...otherMsgs]);
22
28
  const lastMsg = msgs[msgs.length - 1];
23
29
  if (lastMsg && lastMsg.role === 'assistant' && getContentText(lastMsg) === '{')
24
30
  msgs.pop();
25
31
  const cwTools = tools ? convertToolsToCodeWhisperer(tools) : [];
26
32
  const toolResultLimit = Math.floor(250000 * reductionFactor);
27
- let history = buildHistory(msgs, resolved, sys, toolResultLimit);
33
+ let history = buildHistory(msgs, resolved, toolResultLimit);
28
34
  const historyLimit = Math.floor(850000 * reductionFactor);
29
35
  history = truncateHistory(history, historyLimit);
36
+ history = injectSystemPrompt(history, sys, resolved);
30
37
  const curMsg = msgs[msgs.length - 1];
31
38
  if (!curMsg)
32
39
  throw new Error('Empty');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhafron/opencode-kiro-auth",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "OpenCode plugin for AWS Kiro (CodeWhisperer) providing access to Claude models",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",